Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2014 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.data.mongodb.core.query;
 
 import static org.springframework.util.ObjectUtils.*;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.springframework.dao.InvalidDataAccessApiUsageException;
 import  org.springframework.util.Assert;
 import  org.springframework.util.StringUtils;
 
Class to easily construct MongoDB update clauses.

Author(s):
Thomas Risberg
Mark Pollack
Oliver Gierke
Becca Gaspard
Christoph Strobl
Thomas Darimont
 
 public class Update {
 
 	public enum Position {
 		LAST, FIRST
 	}
 
 	private Set<StringkeysToUpdate = new HashSet<String>();
 	private Map<StringObjectmodifierOps = new LinkedHashMap<StringObject>();
Static factory method to create an Update using the provided key

Parameters:
key
Returns:
 
 	public static Update update(String keyObject value) {
 		return new Update().set(keyvalue);
 	}

Creates an Update instance from the given DBObject. Allows to explicitly exlude fields from making it into the created Update object. Note, that this will set attributes directly and not use $set. This means fields not given in the DBObject will be nulled when executing the update. To create an only-updating Update instance of a DBObject, call set(String, Object) for each value in it.

Parameters:
object the source DBObject to create the update from.
exclude the fields to exclude.
Returns:
 
 	public static Update fromDBObject(DBObject objectString... exclude) {
 
 		Update update = new Update();
 		List<StringexcludeList = Arrays.asList(exclude);
 
 		for (String key : object.keySet()) {
 
 			if (excludeList.contains(key)) {
 				continue;
 			}
 
 			Object value = object.get(key);
 			update.modifierOps.put(keyvalue);
 			if (isKeyword(key) && value instanceof DBObject) {
 				update.keysToUpdate.addAll(((DBObjectvalue).keySet());
 			} else {
 				update.keysToUpdate.add(key);
 			}
 		}
 
 		return update;
 	}

Update using the $set update modifier

Parameters:
key
value
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/set/
	public Update set(String keyObject value) {
		addMultiFieldOperation("$set"keyvalue);
		return this;
	}

Update using the $setOnInsert update modifier

Parameters:
key
value
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/setOnInsert/
	public Update setOnInsert(String keyObject value) {
		addMultiFieldOperation("$setOnInsert"keyvalue);
		return this;
	}

Update using the $unset update modifier

Parameters:
key
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/unset/
	public Update unset(String key) {
		addMultiFieldOperation("$unset"key, 1);
		return this;
	}

Update using the $inc update modifier

Parameters:
key
inc
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/inc/
	public Update inc(String keyNumber inc) {
		addMultiFieldOperation("$inc"keyinc);
		return this;
	}

Update using the $push update modifier

Parameters:
key
value
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/push/
	public Update push(String keyObject value) {
		addMultiFieldOperation("$push"keyvalue);
		return this;
	}

Update using $push modifier.
Allows creation of $push command for single or multiple (using $each) values.

Parameters:
key
Returns:
PushOperatorBuilder for given key
See also:
http://docs.mongodb.org/manual/reference/operator/update/push/
http://docs.mongodb.org/manual/reference/operator/update/each/
		}
	}

Update using the $pushAll update modifier.
Note: In mongodb 2.4 the usage of $pushAll has been deprecated in favor of $push $each. push(String)) returns a builder that can be used to populate the $each object.

Parameters:
key
values
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/pushAll/
	public Update pushAll(String keyObject[] values) {
		addMultiFieldOperation("$pushAll"key, Arrays.copyOf(valuesvalues.length));
		return this;
	}

Update using $addToSet modifier.
Allows creation of $push command for single or multiple (using $each) values

Parameters:
key
Returns:
Since:
1.5
		return new AddToSetBuilder(key);
	}

Update using the $addToSet update modifier

Parameters:
key
value
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/addToSet/
	public Update addToSet(String keyObject value) {
		addMultiFieldOperation("$addToSet"keyvalue);
		return this;
	}

Update using the $pop update modifier

Parameters:
key
pos
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/pop/
	public Update pop(String keyPosition pos) {
		addMultiFieldOperation("$pop"keypos == . ? -1 : 1);
		return this;
	}

Update using the $pull update modifier

Parameters:
key
value
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/pull/
	public Update pull(String keyObject value) {
		addMultiFieldOperation("$pull"keyvalue);
		return this;
	}

Update using the $pullAll update modifier

Parameters:
key
values
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/pullAll/
	public Update pullAll(String keyObject[] values) {
		addFieldOperation("$pullAll"key, Arrays.copyOf(valuesvalues.length));
		return this;
	}

Update using the $rename update modifier

Parameters:
oldName
newName
Returns:
See also:
http://docs.mongodb.org/manual/reference/operator/update/rename/
	public Update rename(String oldNameString newName) {
		addMultiFieldOperation("$rename"oldNamenewName);
		return this;
	}
		DBObject dbo = new BasicDBObject();
		for (String k : .keySet()) {
			dbo.put(k.get(k));
		}
		return dbo;
	}
	protected void addFieldOperation(String operatorString keyObject value) {
		Assert.hasText(key"Key/Path for update must not be null or blank.");
		.put(operatornew BasicDBObject(keyvalue));
		this..add(key);
	}
	protected void addMultiFieldOperation(String operatorString keyObject value) {
		Assert.hasText(key"Key/Path for update must not be null or blank.");
		Object existingValue = this..get(operator);
		DBObject keyValueMap;
		if (existingValue == null) {
			keyValueMap = new BasicDBObject();
			this..put(operatorkeyValueMap);
else {
			if (existingValue instanceof BasicDBObject) {
				keyValueMap = (BasicDBObjectexistingValue;
else {
				throw new InvalidDataAccessApiUsageException("Modifier Operations should be a LinkedHashMap but was "
existingValue.getClass());
			}
		}
		keyValueMap.put(keyvalue);
		this..add(key);
	}

Determine if a given key will be touched on execution.

Parameters:
key
Returns:
	public boolean modifies(String key) {
		return this..contains(key);
	}

Inspects given key for '$'.

Parameters:
key
Returns:
	private static boolean isKeyword(String key) {
		return StringUtils.startsWithIgnoreCase(key"$");
	}
	/* 
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
	}
	/* 
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
		Update that = (Updateobj;
		return this.getUpdateObject().equals(that.getUpdateObject());
	}
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return SerializationUtils.serializeToJsonSafely(getUpdateObject());
	}

Modifiers holds a distinct collection of Modifier

Author(s):
Christoph Strobl
Thomas Darimont
	public static class Modifiers {
		public Modifiers() {
		}
			return Collections.unmodifiableCollection(this..values());
		}
		public void addModifier(Modifier modifier) {
			this..put(modifier.getKey(), modifier);
		}
		/* (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		public int hashCode() {
		}
		/* (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		public boolean equals(Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null || getClass() != obj.getClass()) {
				return false;
			}
			Modifiers that = (Modifiersobj;
			return this..equals(that.modifiers);
		}
	}

Marker interface of nested commands.

Author(s):
Christoph Strobl
	public static interface Modifier {

Returns:
the command to send eg. $push

Returns:
value to be sent with command
	}

Implementation of Modifier representing $each.

Author(s):
Christoph Strobl
Thomas Darimont
	private static class Each implements Modifier {
		private Object[] values;
		public Each(Object... values) {
			this. = extractValues(values);
		}
		private Object[] extractValues(Object[] values) {
			if (values == null || values.length == 0) {
				return values;
			}
			if (values.length == 1 && values[0] instanceof Collection) {
				return ((Collection<?>) values[0]).toArray();
			}
			return Arrays.copyOf(valuesvalues.length);
		}
		/*
		 * (non-Javadoc)
		 * @see org.springframework.data.mongodb.core.query.Update.Modifier#getKey()
		 */
		public String getKey() {
			return "$each";
		}
		/*
		 * (non-Javadoc)
		 * @see org.springframework.data.mongodb.core.query.Update.Modifier#getValue()
		 */
		public Object getValue() {
			return this.;
		}
		/* 
		 * (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		public int hashCode() {
		}
		/* 
		 * (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		public boolean equals(Object that) {
			if (this == that) {
				return true;
			}
			if (that == null || getClass() != that.getClass()) {
				return false;
			}
			return nullSafeEquals(, ((Eachthat).);
		}
	}

Builder for creating $push modifiers

Author(s):
Christoph Strobl
Thomas Darimont
	public class PushOperatorBuilder {
		private final String key;
		private final Modifiers modifiers;
			this. = key;
			this. = new Modifiers();
		}

Propagates $each to $push

Parameters:
values
Returns:
		public Update each(Object... values) {
			this..addModifier(new Each(values));
			return Update.this.push(this.);
		}

Propagates value(Object) to $push

Parameters:
values
Returns:
		public Update value(Object value) {
			return Update.this.push(value);
		}
		/* 
		 * (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		public int hashCode() {
			int result = 17;
			result += 31 * result + getOuterType().hashCode();
			result += 31 * result + nullSafeHashCode();
			result += 31 * result + nullSafeHashCode();
			return result;
		}
		/* 
		 * (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		public boolean equals(Object obj) {
			if (this == obj) {
				return true;
			}
			if (obj == null || getClass() != obj.getClass()) {
				return false;
			}
			if (!getOuterType().equals(that.getOuterType())) {
				return false;
			}
			return nullSafeEquals(this.that.key) && nullSafeEquals(this.that.modifiers);
		}
		private Update getOuterType() {
			return Update.this;
		}
	}

Builder for creating $addToSet modifier.

Author(s):
Christoph Strobl
Since:
1.5
	public class AddToSetBuilder {
		private final String key;
		public AddToSetBuilder(String key) {
			this. = key;
		}

Propagates $each to $addToSet

Parameters:
values
Returns:
		public Update each(Object... values) {
			return Update.this.addToSet(this.new Each(values));
		}

Propagates value(Object) to $addToSet

Parameters:
values
Returns:
		public Update value(Object value) {
			return Update.this.addToSet(this.value);
		}
	}
New to GrepCode? Check out our FAQ X