/src/main/java/com/smartcodellc/gomon/Criteria.java

https://github.com/rburton/gomon · Java · 224 lines · 107 code · 27 blank · 90 comment · 8 complexity · 101d86d920c7ff0d19bb702855b6617a MD5 · raw file

  1. package com.smartcodellc.gomon;
  2. import com.mongodb.BasicDBObject;
  3. import com.mongodb.DBObject;
  4. import static com.smartcodellc.gomon.Operators.*;
  5. import java.util.regex.Pattern;
  6. /**
  7. * Based upon http://github.com/julsonlim/mongo-java-driver
  8. * @author Richard L. Burton III - SmartCode LLC
  9. */
  10. public class Criteria {
  11. private static final class NullObject {}
  12. private DBObject query;
  13. private String key;
  14. public Criteria() {
  15. query = new BasicDBObject();
  16. }
  17. /**
  18. * Creates a new query with a document key
  19. * @param key MongoDB document key
  20. * @return Returns a new QueryBuilder
  21. */
  22. public static Criteria start(String key) {
  23. return (new Criteria()).put(key);
  24. }
  25. /**
  26. * Adds a new key to the query or sets an existing key to as current for chaining
  27. * @param key MongoDB document key
  28. * @return Returns the current QueryBuilder with an appended key operand
  29. */
  30. public Criteria put(String key) {
  31. this.key = key;
  32. if(query.get(key) == null) {
  33. query.put(key, new NullObject());
  34. }
  35. return this;
  36. }
  37. /**
  38. * Equivalent to <code>QueryBuilder.put(key)</code>. Intended for compound query chains to be more readable
  39. * Example: QueryBuilder.start("a").greaterThan(1).and("b").lessThan(3)
  40. * @param key MongoDB document key
  41. * @return Returns the current QueryBuilder with an appended key operand
  42. */
  43. public Criteria and(String key) {
  44. return put(key);
  45. }
  46. /**
  47. * Equivalent to the $gt operator
  48. * @param object Value to query
  49. * @return Returns the current QueryBuilder with an appended "greater than" query
  50. */
  51. public Criteria greaterThan(Object object) {
  52. addOperand(GT, object);
  53. return this;
  54. }
  55. /**
  56. * Equivalent to the $gte operator
  57. * @param object Value to query
  58. * @return Returns the current QueryBuilder with an appended "greater than or equals" query
  59. */
  60. public Criteria greaterThanEquals(Object object) {
  61. addOperand(GTE, object);
  62. return this;
  63. }
  64. /**
  65. * Equivalent to the $lt operand
  66. * @param object Value to query
  67. * @return Returns the current QueryBuilder with an appended "less than" query
  68. */
  69. public Criteria lessThan(Object object) {
  70. addOperand(LT, object);
  71. return this;
  72. }
  73. /**
  74. * Equivalent to the $lte operand
  75. * @param object Value to query
  76. * @return Returns the current QueryBuilder with an appended "less than or equals" query
  77. */
  78. public Criteria lessThanEquals(Object object) {
  79. addOperand(LTE, object);
  80. return this;
  81. }
  82. /**
  83. * Equivalent of the find({key:value})
  84. * @param object Value to query
  85. * @return Returns the current QueryBuilder with an appended equality query
  86. */
  87. public Criteria is(Object object) {
  88. addOperand(null, object);
  89. return this;
  90. }
  91. /**
  92. * Equivalent of the $ne operand
  93. * @param object Value to query
  94. * @return Returns the current QueryBuilder with an appended inequality query
  95. */
  96. public Criteria notEquals(Object object) {
  97. addOperand(NE, object);
  98. return this;
  99. }
  100. /**
  101. * Equivalent of the $in operand
  102. * @param object Value to query
  103. * @return Returns the current QueryBuilder with an appended "in array" query
  104. */
  105. public Criteria in(Object object) {
  106. addOperand(IN, object);
  107. return this;
  108. }
  109. /**
  110. * Equivalent of the $nin operand
  111. * @param object Value to query
  112. * @return Returns the current QueryBuilder with an appended "not in array" query
  113. */
  114. public Criteria notIn(Object object) {
  115. addOperand(NIN, object);
  116. return this;
  117. }
  118. /**
  119. * Equivalent of the $mod operand
  120. * @param object Value to query
  121. * @return Returns the current QueryBuilder with an appended modulo query
  122. */
  123. public Criteria mod(Object object) {
  124. addOperand(MOD, object);
  125. return this;
  126. }
  127. /**
  128. * Equivalent of the $all operand
  129. * @param object Value to query
  130. * @return Returns the current QueryBuilder with an appended "matches all array contents" query
  131. */
  132. public Criteria all(Object object) {
  133. addOperand(ALL, object);
  134. return this;
  135. }
  136. /**
  137. * Equivalent of the $size operand
  138. * @param object Value to query
  139. * @return Returns the current QueryBuilder with an appended size operator
  140. */
  141. public Criteria size(Object object) {
  142. addOperand(SIZE, object);
  143. return this;
  144. }
  145. /**
  146. * Equivalent of the $exists operand
  147. * @param object Value to query
  148. * @return Returns the current QueryBuilder with an appended exists operator
  149. */
  150. public Criteria exists(Object object) {
  151. addOperand(EXISTS, object);
  152. return this;
  153. }
  154. /**
  155. * Passes a regular expression for a query
  156. * @param regex Regex pattern object
  157. * @return Returns the current QueryBuilder with an appended regex query
  158. */
  159. public Criteria regex(Pattern regex) {
  160. addOperand(null, regex);
  161. return this;
  162. }
  163. /**
  164. * Creates a <code>DBObject</code> query to be used for the driver's find operations
  165. * @return Returns a DBObject query instance
  166. * @throws RuntimeException if a key does not have a matching operand
  167. */
  168. public DBObject get() {
  169. for(String key : query.keySet()) {
  170. if(query.get(key) instanceof NullObject) {
  171. throw new QueryBuilderException("No operand for key:" + key);
  172. }
  173. }
  174. return query;
  175. }
  176. private void addOperand(String op, Object value) {
  177. if(op == null) {
  178. query.put(key, value);
  179. return;
  180. }
  181. Object storedValue = query.get(key);
  182. BasicDBObject operand;
  183. if(!(storedValue instanceof DBObject)) {
  184. operand = new BasicDBObject();
  185. query.put(key, operand);
  186. } else {
  187. operand = (BasicDBObject)query.get(key);
  188. }
  189. operand.put(op, value);
  190. }
  191. @SuppressWarnings("serial")
  192. static class QueryBuilderException extends RuntimeException {
  193. QueryBuilderException(String message) {
  194. super(message);
  195. }
  196. }
  197. }