1 /* 2 * #%L 3 * DeleteOperation.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 26 /** 27 * DeleteOperation provides a container for the fields in a delete request. 28 * 29 * @api.yes This class is part of the driver's API. Public and protected members 30 * will be deprecated for at least 1 non-bugfix release (version 31 * numbers are <major>.<minor>.<bugfix>) before being 32 * removed or modified. 33 * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved 34 */ 35 public class DeleteOperation implements WriteOperation { 36 37 /** Serialization version for the class. */ 38 private static final long serialVersionUID = 3493986989972041392L; 39 40 /** The query to find the documents to delete. */ 41 private final Document myQuery; 42 43 /** If true then the operation should only delete at most one document. */ 44 private final boolean mySingleDelete; 45 46 /** 47 * Creates a new DeleteOperation. 48 * 49 * @param query 50 * The query to find the documents to delete. 51 * @param singleDelete 52 * If true then only a single document will be deleted. If 53 * running in a sharded environment then this field must be false 54 * or the query must contain the shard key. 55 */ 56 public DeleteOperation(final DocumentAssignable query, 57 final boolean singleDelete) { 58 myQuery = query.asDocument(); 59 mySingleDelete = singleDelete; 60 } 61 62 /** 63 * Determines if the passed object is of this same type as this object and 64 * if so that its fields are equal. 65 * 66 * @param object 67 * The object to compare to. 68 * 69 * @see Object#equals(Object) 70 */ 71 @Override 72 public boolean equals(final Object object) { 73 boolean result = false; 74 if (this == object) { 75 result = true; 76 } 77 else if ((object != null) && (getClass() == object.getClass())) { 78 final DeleteOperation other = (DeleteOperation) object; 79 80 result = (mySingleDelete == other.mySingleDelete) 81 && myQuery.equals(other.myQuery); 82 } 83 return result; 84 } 85 86 /** 87 * Returns the query to find the documents to delete. 88 * 89 * @return The query to find the documents to delete. 90 */ 91 public Document getQuery() { 92 return myQuery; 93 } 94 95 /** 96 * {@inheritDoc} 97 * <p> 98 * Overridden to return the query for the delete. 99 * </p> 100 */ 101 @Override 102 public Document getRoutingDocument() { 103 return myQuery; 104 } 105 106 /** 107 * {@inheritDoc} 108 * <p> 109 * Overridden to return {@link WriteOperationType#DELETE}. 110 * </p> 111 */ 112 @Override 113 public final WriteOperationType getType() { 114 return WriteOperationType.DELETE; 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) + (mySingleDelete ? 31 : 11); 126 result = (31 * result) + myQuery.hashCode(); 127 return result; 128 } 129 130 /** 131 * Returns true if the operation should only delete at most one document. 132 * 133 * @return True if the operation should only delete at most one document. 134 */ 135 public boolean isSingleDelete() { 136 return mySingleDelete; 137 } 138 139 /** 140 * {@inheritDoc} 141 * <p> 142 * Overridden to returns a representation of the delete. 143 * </p> 144 */ 145 @Override 146 public String toString() { 147 return "Delete[singleDelete=" + mySingleDelete + ",query=" + myQuery 148 + "]"; 149 } 150 }