From 35259f4e122fe33ddeddcf75c46f377f9db9b527 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sat, 7 May 2011 15:50:15 +0200 Subject: [PATCH] Reapplied old formatting to avoid merge conflicts (yet again). --- .../data/document/mongodb/query/Criteria.java | 670 +++++++++--------- 1 file changed, 338 insertions(+), 332 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java index d555b5d15..05cedac8a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Criteria.java @@ -30,371 +30,377 @@ import org.springframework.util.Assert; public class Criteria implements CriteriaDefinition { - private String key; + private String key; - private List criteriaChain; + private List criteriaChain; - private LinkedHashMap criteria = new LinkedHashMap(); + private LinkedHashMap criteria = new LinkedHashMap(); - private Object isValue = null; + private Object isValue = null; - public Criteria(String key) { - this.criteriaChain = new ArrayList(); - this.criteriaChain.add(this); - this.key = key; - } + public Criteria(String key) { + this.criteriaChain = new ArrayList(); + this.criteriaChain.add(this); + this.key = key; + } - protected Criteria(List criteriaChain, String key) { - this.criteriaChain = criteriaChain; - this.criteriaChain.add(this); - this.key = key; - } + protected Criteria(List criteriaChain, String key) { + this.criteriaChain = criteriaChain; + this.criteriaChain.add(this); + this.key = key; + } - /** - * Static factory method to create a Criteria using the provided key - * - * @param key - * @return - */ - public static Criteria where(String key) { - return new Criteria(key); - } + /** + * Static factory method to create a Criteria using the provided key + * + * @param key + * @return + */ + public static Criteria where(String key) { + return new Criteria(key); + } - public static Criteria whereId() { - return new Criteria("id"); - } + public static Criteria whereId() { + return new Criteria("id"); + } - /** - * Static factory method to create a Criteria using the provided key - * - * @param key - * @return - */ - public Criteria and(String key) { - return new Criteria(this.criteriaChain, key); - } + /** + * Static factory method to create a Criteria using the provided key + * + * @param key + * @return + */ + public Criteria and(String key) { + return new Criteria(this.criteriaChain, key); + } - /** - * Creates a criterion using equality - * - * @param o - * @return - */ - public Criteria is(Object o) { - if (isValue != null) { - throw new InvalidDocumentStoreApiUsageException("Multiple 'is' values declared. You need to use 'and' with multiple criteria"); - } - if ( this.criteria.size() > 0 && "$not".equals(this.criteria.keySet().toArray()[this.criteria.size() - 1])) { - throw new InvalidDocumentStoreApiUsageException("Invalid query: 'not' can't be used with 'is' - use 'ne' instead."); - } - this.isValue = o; - return this; - } + /** + * Creates a criterion using equality + * + * @param o + * @return + */ + public Criteria is(Object o) { + if (isValue != null) { + throw new InvalidDocumentStoreApiUsageException( + "Multiple 'is' values declared. You need to use 'and' with multiple criteria"); + } + if (this.criteria.size() > 0 && "$not".equals(this.criteria.keySet().toArray()[this.criteria.size() - 1])) { + throw new InvalidDocumentStoreApiUsageException( + "Invalid query: 'not' can't be used with 'is' - use 'ne' instead."); + } + this.isValue = o; + return this; + } - /** - * Creates a criterion using the $ne operator - * - * @param o - * @return - */ - public Criteria ne(Object o) { - criteria.put("$ne", o); - return this; - } + /** + * Creates a criterion using the $ne operator + * + * @param o + * @return + */ + public Criteria ne(Object o) { + criteria.put("$ne", o); + return this; + } - /** - * Creates a criterion using the $lt operator - * - * @param o - * @return - */ - public Criteria lt(Object o) { - criteria.put("$lt", o); - return this; - } + /** + * Creates a criterion using the $lt operator + * + * @param o + * @return + */ + public Criteria lt(Object o) { + criteria.put("$lt", o); + return this; + } - /** - * Creates a criterion using the $lte operator - * - * @param o - * @return - */ - public Criteria lte(Object o) { - criteria.put("$lte", o); - return this; - } + /** + * Creates a criterion using the $lte operator + * + * @param o + * @return + */ + public Criteria lte(Object o) { + criteria.put("$lte", o); + return this; + } - /** - * Creates a criterion using the $gt operator - * - * @param o - * @return - */ - public Criteria gt(Object o) { - criteria.put("$gt", o); - return this; - } + /** + * Creates a criterion using the $gt operator + * + * @param o + * @return + */ + public Criteria gt(Object o) { + criteria.put("$gt", o); + return this; + } - /** - * Creates a criterion using the $gte operator - * - * @param o - * @return - */ - public Criteria gte(Object o) { - criteria.put("$gte", o); - return this; - } + /** + * Creates a criterion using the $gte operator + * + * @param o + * @return + */ + public Criteria gte(Object o) { + criteria.put("$gte", o); + return this; + } - /** - * Creates a criterion using the $in operator - * - * @param o - * @return - */ - public Criteria in(Object... o) { - criteria.put("$in", o); - return this; - } + /** + * Creates a criterion using the $in operator + * + * @param o + * @return + */ + public Criteria in(Object... o) { + criteria.put("$in", o); + return this; + } - /** - * Creates a criterion using the $nin operator - * - * @param o - * @return - */ - public Criteria nin(Object... o) { - criteria.put("$nin", o); - return this; - } + /** + * Creates a criterion using the $nin operator + * + * @param o + * @return + */ + public Criteria nin(Object... o) { + criteria.put("$nin", o); + return this; + } - /** - * Creates a criterion using the $mod operator - * - * @param value - * @param remainder - * @return - */ - public Criteria mod(Number value, Number remainder) { - List l = new ArrayList(); - l.add(value); - l.add(remainder); - criteria.put("$mod", l); - return this; - } + /** + * Creates a criterion using the $mod operator + * + * @param value + * @param remainder + * @return + */ + public Criteria mod(Number value, Number remainder) { + List l = new ArrayList(); + l.add(value); + l.add(remainder); + criteria.put("$mod", l); + return this; + } - /** - * Creates a criterion using the $all operator - * - * @param o - * @return - */ - public Criteria all(Object... o) { - criteria.put("$all", o); - return this; - } + /** + * Creates a criterion using the $all operator + * + * @param o + * @return + */ + public Criteria all(Object... o) { + criteria.put("$all", o); + return this; + } - /** - * Creates a criterion using the $size operator - * - * @param s - * @return - */ - public Criteria size(int s) { - criteria.put("$size", s); - return this; - } + /** + * Creates a criterion using the $size operator + * + * @param s + * @return + */ + public Criteria size(int s) { + criteria.put("$size", s); + return this; + } - /** - * Creates a criterion using the $exists operator - * - * @param b - * @return - */ - public Criteria exists(boolean b) { - criteria.put("$exists", b); - return this; - } + /** + * Creates a criterion using the $exists operator + * + * @param b + * @return + */ + public Criteria exists(boolean b) { + criteria.put("$exists", b); + return this; + } - /** - * Creates a criterion using the $type operator - * - * @param t - * @return - */ - public Criteria type(int t) { - criteria.put("$type", t); - return this; - } + /** + * Creates a criterion using the $type operator + * + * @param t + * @return + */ + public Criteria type(int t) { + criteria.put("$type", t); + return this; + } - /** - * Creates a criterion using the $not meta operator which affects the clause directly following - * - * @return - */ - public Criteria not() { - criteria.put("$not", null); - return this; - } + /** + * Creates a criterion using the $not meta operator which affects the clause directly following + * + * @return + */ + public Criteria not() { + criteria.put("$not", null); + return this; + } - /** - * Creates a criterion using a $regex - * - * @param re - * @return - */ - public Criteria regex(String re) { - criteria.put("$regex", re); - return this; - } + /** + * Creates a criterion using a $regex + * + * @param re + * @return + */ + public Criteria regex(String re) { + criteria.put("$regex", re); + return this; + } - /** - * Creates a geospatial criterion using a $within $center operation - * - * @param circle must not be {@literal null} - * @return - */ - public Criteria withinCenter(Circle circle) { - Assert.notNull(circle); - LinkedList list = new LinkedList(); - list.addLast(circle.getCenter().asArray()); - list.add(circle.getRadius()); - criteria.put("$within", new BasicDBObject("$center", list)); - return this; - } + /** + * Creates a geospatial criterion using a $within $center operation + * + * @param circle + * must not be {@literal null} + * @return + */ + public Criteria withinCenter(Circle circle) { + Assert.notNull(circle); + LinkedList list = new LinkedList(); + list.addLast(circle.getCenter().asArray()); + list.add(circle.getRadius()); + criteria.put("$within", new BasicDBObject("$center", list)); + return this; + } - /** - * Creates a geospatial criterion using a $within $center operation. This is only available for Mongo 1.7 and higher. - * - * @param circle must not be {@literal null} - * @return - */ - public Criteria withinCenterSphere(Circle circle) { - Assert.notNull(circle); - LinkedList list = new LinkedList(); - list.addLast(circle.getCenter().asArray()); - list.add(circle.getRadius()); - criteria.put("$within", new BasicDBObject("$centerSphere", list)); - return this; - } + /** + * Creates a geospatial criterion using a $within $center operation. This is only available for Mongo 1.7 and higher. + * + * @param circle + * must not be {@literal null} + * @return + */ + public Criteria withinCenterSphere(Circle circle) { + Assert.notNull(circle); + LinkedList list = new LinkedList(); + list.addLast(circle.getCenter().asArray()); + list.add(circle.getRadius()); + criteria.put("$within", new BasicDBObject("$centerSphere", list)); + return this; + } - /** - * Creates a geospatial criterion using a $within $box operation - * - * @param box - * @return - */ - public Criteria withinBox(Box box) { - Assert.notNull(box); - LinkedList list = new LinkedList(); - list.addLast(box.getLowerLeft().asArray()); - list.addLast(box.getUpperRight().asArray()); - criteria.put("$within", new BasicDBObject("$box", list)); - return this; - } + /** + * Creates a geospatial criterion using a $within $box operation + * + * @param box + * @return + */ + public Criteria withinBox(Box box) { + Assert.notNull(box); + LinkedList list = new LinkedList(); + list.addLast(box.getLowerLeft().asArray()); + list.addLast(box.getUpperRight().asArray()); + criteria.put("$within", new BasicDBObject("$box", list)); + return this; + } - /** - * Creates a geospatial criterion using a $near operation - * - * @param point must not be {@literal null} - * @return - */ - public Criteria near(Point point) { - Assert.notNull(point); - criteria.put("$near", point.asArray()); - return this; - } + /** + * Creates a geospatial criterion using a $near operation + * + * @param point + * must not be {@literal null} + * @return + */ + public Criteria near(Point point) { + Assert.notNull(point); + criteria.put("$near", point.asArray()); + return this; + } - /** - * Creates a geospatial criterion using a $nearSphere operation. This is only available for Mongo 1.7 and higher. - * - * @param point must not be {@literal null} - * @return - */ - public Criteria nearSphere(Point point) { - Assert.notNull(point); - criteria.put("$nearSphere", point.asArray()); - return this; - } + /** + * Creates a geospatial criterion using a $nearSphere operation. This is only available for Mongo 1.7 and higher. + * + * @param point + * must not be {@literal null} + * @return + */ + public Criteria nearSphere(Point point) { + Assert.notNull(point); + criteria.put("$nearSphere", point.asArray()); + return this; + } - /** - * Creates a geospatical criterion using a $maxDistance operation, for use with $near - * - * @param maxDistance - * @return - */ - public Criteria maxDistance(double maxDistance) { - criteria.put("$maxDistance", maxDistance); - return this; - } + /** + * Creates a geospatical criterion using a $maxDistance operation, for use with $near + * + * @param maxDistance + * @return + */ + public Criteria maxDistance(double maxDistance) { + criteria.put("$maxDistance", maxDistance); + return this; + } - /** - * Creates a criterion using the $elemMatch operator - * - * @param c - * @return - */ - public Criteria elemMatch(Criteria c) { - criteria.put("$elemMatch", c.getCriteriaObject()); - return this; - } + /** + * Creates a criterion using the $elemMatch operator + * + * @param c + * @return + */ + public Criteria elemMatch(Criteria c) { + criteria.put("$elemMatch", c.getCriteriaObject()); + return this; + } - /** - * Creates an or query using the $or operator for all of the provided queries - * - * @param queries - */ - public void or(List queries) { - criteria.put("$or", queries); - } + /** + * Creates an or query using the $or operator for all of the provided queries + * + * @param queries + */ + public void or(List queries) { + criteria.put("$or", queries); + } - public String getKey() { - return this.key; - } + public String getKey() { + return this.key; + } - /* - * (non-Javadoc) - * - * @see org.springframework.datastore.document.mongodb.query.Criteria# - * getCriteriaObject(java.lang.String) - */ - public DBObject getCriteriaObject() { - if (this.criteriaChain.size() == 1) { - return criteriaChain.get(0).getSingleCriteriaObject(); - } else { - DBObject criteriaObject = new BasicDBObject(); - for (Criteria c : this.criteriaChain) { - criteriaObject.putAll(c.getSingleCriteriaObject()); - } - return criteriaObject; - } - } + /* + * (non-Javadoc) + * + * @see org.springframework.datastore.document.mongodb.query.Criteria# + * getCriteriaObject(java.lang.String) + */ + public DBObject getCriteriaObject() { + if (this.criteriaChain.size() == 1) { + return criteriaChain.get(0).getSingleCriteriaObject(); + } else { + DBObject criteriaObject = new BasicDBObject(); + for (Criteria c : this.criteriaChain) { + criteriaObject.putAll(c.getSingleCriteriaObject()); + } + return criteriaObject; + } + } - protected DBObject getSingleCriteriaObject() { - DBObject dbo = new BasicDBObject(); - boolean not = false; - for (String k : this.criteria.keySet()) { - if (not) { - DBObject notDbo = new BasicDBObject(); - notDbo.put(k, this.criteria.get(k)); - dbo.put("$not", notDbo); - not = false; - } else { - if ("$not".equals(k)) { - not = true; - } else { - dbo.put(k, this.criteria.get(k)); - } - } - } - DBObject queryCriteria = new BasicDBObject(); - if (isValue != null) { - queryCriteria.put(this.key, this.isValue); - queryCriteria.putAll(dbo); - } else { - queryCriteria.put(this.key, dbo); - } - return queryCriteria; - } + protected DBObject getSingleCriteriaObject() { + DBObject dbo = new BasicDBObject(); + boolean not = false; + for (String k : this.criteria.keySet()) { + if (not) { + DBObject notDbo = new BasicDBObject(); + notDbo.put(k, this.criteria.get(k)); + dbo.put("$not", notDbo); + not = false; + } else { + if ("$not".equals(k)) { + not = true; + } else { + dbo.put(k, this.criteria.get(k)); + } + } + } + DBObject queryCriteria = new BasicDBObject(); + if (isValue != null) { + queryCriteria.put(this.key, this.isValue); + queryCriteria.putAll(dbo); + } else { + queryCriteria.put(this.key, dbo); + } + return queryCriteria; + } }