1 /* 2 * #%L 3 * InsertOperation.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 21 package com.allanbank.mongodb.builder.write; 22 23 import com.allanbank.mongodb.bson.Document; 24 import com.allanbank.mongodb.bson.DocumentAssignable; 25 import com.allanbank.mongodb.bson.impl.RootDocument; 26 27 /** 28 * InsertOperation provides a container for the fields in an insert request. 29 * 30 * @api.yes This class is part of the driver's API. Public and protected members 31 * will be deprecated for at least 1 non-bugfix release (version 32 * numbers are <major>.<minor>.<bugfix>) before being 33 * removed or modified. 34 * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved 35 */ 36 public class InsertOperation implements WriteOperation { 37 38 /** Serialization version for the class. */ 39 private static final long serialVersionUID = -197787616022990034L; 40 41 /** The document to insert. */ 42 private final Document myDocument; 43 44 /** 45 * Creates a new InsertOperation. 46 * 47 * @param document 48 * The document to insert. 49 */ 50 public InsertOperation(final DocumentAssignable document) { 51 myDocument = document.asDocument(); 52 if (myDocument instanceof RootDocument) { 53 ((RootDocument) myDocument).injectId(); 54 } 55 } 56 57 /** 58 * Determines if the passed object is of this same type as this object and 59 * if so that its fields are equal. 60 * 61 * @param object 62 * The object to compare to. 63 * 64 * @see Object#equals(Object) 65 */ 66 @Override 67 public boolean equals(final Object object) { 68 boolean result = false; 69 if (this == object) { 70 result = true; 71 } 72 else if ((object != null) && (getClass() == object.getClass())) { 73 final InsertOperation other = (InsertOperation) object; 74 75 result = myDocument.equals(other.myDocument); 76 } 77 return result; 78 } 79 80 /** 81 * Returns the document to insert. 82 * 83 * @return The document to insert. 84 */ 85 public Document getDocument() { 86 return myDocument; 87 } 88 89 /** 90 * {@inheritDoc} 91 * <p> 92 * Overridden to return the document to insert. 93 * </p> 94 */ 95 @Override 96 public Document getRoutingDocument() { 97 return myDocument; 98 } 99 100 /** 101 * {@inheritDoc} 102 * <p> 103 * Overridden to return {@link WriteOperationType#INSERT}. 104 * </p> 105 */ 106 @Override 107 public final WriteOperationType getType() { 108 return WriteOperationType.INSERT; 109 } 110 111 /** 112 * Computes a reasonable hash code. 113 * 114 * @return The hash code value. 115 */ 116 @Override 117 public int hashCode() { 118 return myDocument.hashCode(); 119 } 120 121 /** 122 * {@inheritDoc} 123 * <p> 124 * Overridden to returns a representation of the update. 125 * </p> 126 */ 127 @Override 128 public String toString() { 129 return "Insert[" + myDocument + "]"; 130 } 131 }