| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UpdateOperation |
|
| 1.8;1.8 |
| 1 | /* | |
| 2 | * #%L | |
| 3 | * UpdateOperation.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 | * UpdateOperation provides a container for the fields in an update 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 UpdateOperation implements WriteOperation { | |
| 36 | ||
| 37 | /** Serialization version for the class. */ | |
| 38 | private static final long serialVersionUID = 4657279430768594366L; | |
| 39 | ||
| 40 | /** If true then the update can modify multiple documents. */ | |
| 41 | private final boolean myMultiUpdate; | |
| 42 | ||
| 43 | /** The query to find the documents to update. */ | |
| 44 | private final Document myQuery; | |
| 45 | ||
| 46 | /** The update specification. */ | |
| 47 | private final Document myUpdate; | |
| 48 | ||
| 49 | /** If true then the document should be inserted if not found. */ | |
| 50 | private final boolean myUpsert; | |
| 51 | ||
| 52 | /** | |
| 53 | * Creates a new UpdateOperation. | |
| 54 | * | |
| 55 | * @param query | |
| 56 | * The query to find the documents to update. | |
| 57 | * @param update | |
| 58 | * The update specification. | |
| 59 | * @param multiUpdate | |
| 60 | * If true then the update is applied to all of the matching | |
| 61 | * documents, otherwise only the first document found is updated. | |
| 62 | * @param upsert | |
| 63 | * If true then if no document is found then a new document is | |
| 64 | * created and updated, otherwise no operation is performed. | |
| 65 | */ | |
| 66 | public UpdateOperation(final DocumentAssignable query, | |
| 67 | final DocumentAssignable update, final boolean multiUpdate, | |
| 68 | 400157 | final boolean upsert) { |
| 69 | 400157 | myQuery = query.asDocument(); |
| 70 | 400157 | myUpdate = update.asDocument(); |
| 71 | 400157 | myUpsert = upsert; |
| 72 | 400157 | myMultiUpdate = multiUpdate; |
| 73 | 400157 | } |
| 74 | ||
| 75 | /** | |
| 76 | * Determines if the passed object is of this same type as this object and | |
| 77 | * if so that its fields are equal. | |
| 78 | * | |
| 79 | * @param object | |
| 80 | * The object to compare to. | |
| 81 | * | |
| 82 | * @see Object#equals(Object) | |
| 83 | */ | |
| 84 | @Override | |
| 85 | public boolean equals(final Object object) { | |
| 86 | 167 | boolean result = false; |
| 87 | 167 | if (this == object) { |
| 88 | 20 | result = true; |
| 89 | } | |
| 90 | 147 | else if ((object != null) && (getClass() == object.getClass())) { |
| 91 | 123 | final UpdateOperation other = (UpdateOperation) object; |
| 92 | ||
| 93 | 123 | result = (myUpsert == other.myUpsert) |
| 94 | && (myMultiUpdate == other.myMultiUpdate) | |
| 95 | && myQuery.equals(other.myQuery) | |
| 96 | && myUpdate.equals(other.myUpdate); | |
| 97 | } | |
| 98 | 167 | return result; |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | * Returns the query to find the documents to update. | |
| 103 | * | |
| 104 | * @return The query to find the documents to update. | |
| 105 | */ | |
| 106 | public Document getQuery() { | |
| 107 | 1200158 | return myQuery; |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * {@inheritDoc} | |
| 112 | * <p> | |
| 113 | * Overridden to return the query for the update. | |
| 114 | * </p> | |
| 115 | */ | |
| 116 | @Override | |
| 117 | public Document getRoutingDocument() { | |
| 118 | 1 | return myQuery; |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * {@inheritDoc} | |
| 123 | * <p> | |
| 124 | * Overridden to return {@link WriteOperationType#UPDATE}. | |
| 125 | * </p> | |
| 126 | */ | |
| 127 | @Override | |
| 128 | public final WriteOperationType getType() { | |
| 129 | 1600303 | return WriteOperationType.UPDATE; |
| 130 | } | |
| 131 | ||
| 132 | /** | |
| 133 | * Returns the update specification. | |
| 134 | * | |
| 135 | * @return The update specification. | |
| 136 | */ | |
| 137 | public Document getUpdate() { | |
| 138 | 1200157 | return myUpdate; |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * Computes a reasonable hash code. | |
| 143 | * | |
| 144 | * @return The hash code value. | |
| 145 | */ | |
| 146 | @Override | |
| 147 | public int hashCode() { | |
| 148 | 156 | int result = 1; |
| 149 | 156 | result = (31 * result) + (myUpsert ? 31 : 11); |
| 150 | 156 | result = (31 * result) + (myMultiUpdate ? 31 : 11); |
| 151 | 156 | result = (31 * result) + myQuery.hashCode(); |
| 152 | 156 | result = (31 * result) + myUpdate.hashCode(); |
| 153 | 156 | return result; |
| 154 | } | |
| 155 | ||
| 156 | /** | |
| 157 | * Returns true if the update can modify multiple documents. | |
| 158 | * | |
| 159 | * @return True if the update can modify multiple documents. | |
| 160 | */ | |
| 161 | public boolean isMultiUpdate() { | |
| 162 | 400075 | return myMultiUpdate; |
| 163 | } | |
| 164 | ||
| 165 | /** | |
| 166 | * Returns true if the document should be inserted if not found. | |
| 167 | * | |
| 168 | * @return True if the document should be inserted if not found. | |
| 169 | */ | |
| 170 | public boolean isUpsert() { | |
| 171 | 400075 | return myUpsert; |
| 172 | } | |
| 173 | ||
| 174 | /** | |
| 175 | * {@inheritDoc} | |
| 176 | * <p> | |
| 177 | * Overridden to returns a representation of the update. | |
| 178 | * </p> | |
| 179 | */ | |
| 180 | @Override | |
| 181 | public String toString() { | |
| 182 | 3 | return "Update[upsert=" + myUpsert + ",multi=" + myMultiUpdate |
| 183 | + ",query=" + myQuery + ",update=" + myUpdate + "]"; | |
| 184 | } | |
| 185 | } |