View Javadoc
1   /*
2    * #%L
3    * Operation.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.allanbank.mongodb.client;
21  
22  /**
23   * Enumeration of the possible operations allowed in MongoDB messages.
24   * 
25   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
26   *         mutated in incompatible ways between any two releases of the driver.
27   * @copyright 2011-2013, Allanbank Consulting, Inc., All Rights Reserved
28   */
29  public enum Operation {
30      /** Delete documents. */
31      DELETE(2006),
32  
33      /** Get more data from a query. */
34      GET_MORE(2005),
35  
36      /** Insert new document. */
37      INSERT(2002),
38  
39      /** Tell database client is done with a cursor. */
40      KILL_CURSORS(2007),
41  
42      /** Query a collection. */
43      QUERY(2004),
44  
45      /** Reply to a client request. */
46      REPLY(1),
47  
48      /** Update a document. */
49      UPDATE(2001);
50  
51      /**
52       * Returns the {@link Operation} for the provided opCode.
53       * 
54       * @param opCode
55       *            The operation code for the {@link Operation}.
56       * @return The {@link Operation} for the operation code or <code>null</code>
57       *         if the operation code is invalid.
58       */
59      public static Operation fromCode(final int opCode) {
60          if (opCode == REPLY.getCode()) {
61              return REPLY;
62          }
63          else if (opCode == DELETE.getCode()) {
64              return DELETE;
65          }
66          else if (opCode == GET_MORE.getCode()) {
67              return GET_MORE;
68          }
69          else if (opCode == INSERT.getCode()) {
70              return INSERT;
71          }
72          else if (opCode == KILL_CURSORS.getCode()) {
73              return KILL_CURSORS;
74          }
75          else if (opCode == QUERY.getCode()) {
76              return QUERY;
77          }
78          else if (opCode == UPDATE.getCode()) {
79              return UPDATE;
80          }
81          return null;
82      }
83  
84      /** The operation code. */
85      private final int myCode;
86  
87      /**
88       * Creates a new Operation.
89       * 
90       * @param code
91       *            The operations code.
92       */
93      private Operation(final int code) {
94          myCode = code;
95      }
96  
97      /**
98       * Returns the Operation's code.
99       * 
100      * @return The operation's code.
101      */
102     public int getCode() {
103         return myCode;
104     }
105 }