View Javadoc
1   /*
2    * #%L
3    * BatchedWriteCommand.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.message;
21  
22  import com.allanbank.mongodb.ReadPreference;
23  import com.allanbank.mongodb.bson.Document;
24  import com.allanbank.mongodb.builder.BatchedWrite;
25  import com.allanbank.mongodb.client.VersionRange;
26  
27  /**
28   * BatchedWriteCommand provides a container to hold the batched write command
29   * and the operations that it was created from.
30   * 
31   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
32   *         mutated in incompatible ways between any two releases of the driver.
33   * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved
34   */
35  public class BatchedWriteCommand extends Command {
36  
37      /** The required server version range for the {@link BatchedWrite} command. */
38      public static final VersionRange REQUIRED_VERSION_RANGE = VersionRange
39              .minimum(BatchedWrite.REQUIRED_VERSION);
40  
41      /** The bundle for the batched write. */
42      private final BatchedWrite.Bundle myBundle;
43  
44      /**
45       * Create a new BatchedWriteCommand.
46       * 
47       * @param databaseName
48       *            The name of the database.
49       * @param collectionName
50       *            The name of the collection the command is using. This should
51       *            be the real collection and not
52       *            {@link Command#COMMAND_COLLECTION $cmd} if the real collection
53       *            is known.
54       * @param bundle
55       *            The bundle for the batched write.
56       */
57      public BatchedWriteCommand(final String databaseName,
58              final String collectionName, final BatchedWrite.Bundle bundle) {
59          super(databaseName, collectionName, bundle.getCommand(),
60                  ReadPreference.PRIMARY, REQUIRED_VERSION_RANGE);
61  
62          myBundle = bundle;
63      }
64  
65      /**
66       * Creates a new BatchedWriteCommand. This constructor is provided for tests
67       * that do not want to create a
68       * {@link com.allanbank.mongodb.builder.BatchedWrite.Bundle}
69       * 
70       * @param databaseName
71       *            The name of the database.
72       * @param collectionName
73       *            The name of the collection the command is using. This should
74       *            be the real collection and not
75       *            {@link Command#COMMAND_COLLECTION $cmd} if the real collection
76       *            is known.
77       * @param commandDocument
78       *            The batch command document.
79       */
80      public BatchedWriteCommand(final String databaseName,
81              final String collectionName, final Document commandDocument) {
82          super(databaseName, collectionName, commandDocument,
83                  ReadPreference.PRIMARY, REQUIRED_VERSION_RANGE);
84          myBundle = null;
85      }
86  
87      /**
88       * Determines if the passed object is of this same type as this object and
89       * if so that its fields are equal.
90       * 
91       * @param object
92       *            The object to compare to.
93       * 
94       * @see java.lang.Object#equals(java.lang.Object)
95       */
96      @Override
97      public boolean equals(final Object object) {
98          boolean result = false;
99          if (this == object) {
100             result = true;
101         }
102         else if ((object != null) && (getClass() == object.getClass())) {
103             result = super.equals(object);
104         }
105         return result;
106     }
107 
108     /**
109      * Returns the bundle for the batched write.
110      * 
111      * @return The bundle for the batched write.
112      */
113     public BatchedWrite.Bundle getBundle() {
114         return myBundle;
115     }
116 
117     /**
118      * Computes a reasonable hash code.
119      * 
120      * @return The hash code value.
121      */
122     @Override
123     public int hashCode() {
124         int result = 1;
125         result = (31 * result) + super.hashCode();
126         return result;
127     }
128 }