Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / Query.java
blob13fca977bdc07429396fa1269c1a9d080894b4d5
1 // Copyright 2007 Google Inc. All rights reserved.
3 package com.google.appengine.api.datastore;
5 import static com.google.common.base.Preconditions.checkNotNull;
7 import com.google.appengine.api.NamespaceManager;
8 import com.google.common.base.Joiner;
9 import com.google.common.base.Preconditions;
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.Iterables;
12 import com.google.common.collect.Lists;
13 import com.google.common.collect.Maps;
15 import java.io.IOException;
16 import java.io.ObjectInputStream;
17 import java.io.Serializable;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Objects;
25 /**
26 * {@link Query} encapsulates a request for zero or more {@link Entity} objects
27 * out of the datastore. It supports querying on zero or more properties,
28 * querying by ancestor, and sorting. {@link Entity} objects which match the
29 * query can be retrieved in a single list, or with an unbounded iterator.
32 public final class Query implements Serializable {
34 @Deprecated
35 public static final String KIND_METADATA_KIND = Entities.KIND_METADATA_KIND;
37 @Deprecated
38 public static final String PROPERTY_METADATA_KIND = Entities.PROPERTY_METADATA_KIND;
40 @Deprecated
41 public static final String NAMESPACE_METADATA_KIND = Entities.NAMESPACE_METADATA_KIND;
43 static final long serialVersionUID = 7090652715949085374L;
45 /**
46 * SortDirection controls the order of a sort.
48 public enum SortDirection {
49 ASCENDING,
50 DESCENDING
53 /**
54 * Operators supported by {@link FilterPredicate}.
56 public enum FilterOperator {
57 LESS_THAN("<"),
58 LESS_THAN_OR_EQUAL("<="),
59 GREATER_THAN(">"),
60 GREATER_THAN_OR_EQUAL(">="),
61 EQUAL("="),
62 NOT_EQUAL("!="),
63 IN("IN");
65 private final String shortName;
66 private FilterOperator(String shortName) {
67 this.shortName = shortName;
70 @Override
71 public String toString() {
72 return shortName;
75 public FilterPredicate of(String propertyName, Object value) {
76 return new FilterPredicate(propertyName, this, value);
80 /**
81 * Operators supported by {@link CompositeFilter}.
83 public enum CompositeFilterOperator {
84 AND,
85 OR;
87 public static CompositeFilter and(Filter... subFilters) {
88 return and(Arrays.asList(subFilters));
91 public static CompositeFilter and(Collection<Filter> subFilters) {
92 return new CompositeFilter(AND, subFilters);
95 public static CompositeFilter or(Filter... subFilters) {
96 return or(Arrays.asList(subFilters));
99 public static CompositeFilter or(Collection<Filter> subFilters) {
100 return new CompositeFilter(OR, subFilters);
103 public CompositeFilter of(Filter... subFilters) {
104 return new CompositeFilter(this, Arrays.asList(subFilters));
107 public CompositeFilter of(Collection<Filter> subFilters) {
108 return new CompositeFilter(this, subFilters);
112 private final String kind;
113 private final List<SortPredicate> sortPredicates = Lists.newArrayList();
114 private final List<FilterPredicate> filterPredicates = Lists.newArrayList();
115 private Filter filter;
116 private Key ancestor;
117 private boolean keysOnly;
118 private final Map<String, Projection> projectionMap = Maps.newLinkedHashMap();
119 private boolean distinct;
120 private AppIdNamespace appIdNamespace;
123 * Create a new kindless {@link Query} that finds {@link Entity} objects.
124 * Note that kindless queries are not yet supported in the Java dev
125 * appserver.
127 * Currently the only operations supported on a kindless query are filter by
128 * __key__, ancestor, and order by __key__ ascending.
130 public Query() {
131 this(null, null);
135 * Create a new {@link Query} that finds {@link Entity} objects with
136 * the specified {@code kind}. Note that kindless queries are not yet
137 * supported in the Java dev appserver.
139 * @param kind the kind or null to create a kindless query
141 public Query(String kind) {
142 this(kind, null);
146 * Copy constructor that performs a deep copy of the provided Query.
148 * @param query The Query to copy.
150 Query(Query query) {
151 this(query.kind, query.ancestor, query.sortPredicates, query.filter, query.filterPredicates,
152 query.keysOnly, query.appIdNamespace, query.projectionMap.values(), query.distinct);
155 Query(String kind, Key ancestor, Filter filter, boolean keysOnly, AppIdNamespace appIdNamespace,
156 boolean distinct) {
157 this.kind = kind;
158 this.keysOnly = keysOnly;
159 this.appIdNamespace = appIdNamespace;
160 this.distinct = distinct;
161 this.filter = filter;
162 if (ancestor != null) {
163 setAncestor(ancestor);
167 Query(String kind, Key ancestor, Collection<SortPredicate> sortPreds,
168 Filter filter,
169 Collection<FilterPredicate> filterPreds, boolean keysOnly, AppIdNamespace appIdNamespace,
170 Collection<Projection> projections, boolean distinct) {
171 this(kind, ancestor, filter, keysOnly, appIdNamespace, distinct);
172 this.sortPredicates.addAll(sortPreds);
173 this.filterPredicates.addAll(filterPreds);
174 for (Projection projection : projections) {
175 addProjection(projection);
179 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
180 in.defaultReadObject();
181 if (appIdNamespace == null) {
182 if (ancestor != null) {
183 appIdNamespace = ancestor.getAppIdNamespace();
184 } else {
185 appIdNamespace = new AppIdNamespace(DatastoreApiHelper.getCurrentAppId(), "");
191 * Create a new {@link Query} that finds {@link Entity} objects with
192 * the specified {@code Key} as an ancestor.
194 * @param ancestor the ancestor key or null
195 * @throws IllegalArgumentException If ancestor is not complete.
197 public Query(Key ancestor) {
198 this(null, ancestor);
202 * Create a new {@link Query} that finds {@link Entity} objects with
203 * the specified {@code kind} and the specified {@code ancestor}. Note that
204 * kindless queries are not yet supported in the Java dev appserver.
206 * @param kind the kind or null to create a kindless query
207 * @param ancestor the ancestor key or null
208 * @throws IllegalArgumentException If the ancestor is not complete.
210 public Query(String kind, Key ancestor) {
211 this(kind, ancestor, null, false, DatastoreApiHelper.getCurrentAppIdNamespace(), false);
215 * Only {@link Entity} objects whose kind matches this value will be
216 * returned.
218 public String getKind() {
219 return kind;
223 * Returns the AppIdNamespace that is being queried.
224 * <p>The AppIdNamespace is set at construction time of this
225 * object using the {@link NamespaceManager} to retrieve the
226 * current namespace.
228 AppIdNamespace getAppIdNamespace() {
229 return appIdNamespace;
233 * Returns the appId for this {@link Query}.
235 public String getAppId() {
236 return appIdNamespace.getAppId();
240 * Returns the namespace for this {@link Query}.
242 public String getNamespace() {
243 return appIdNamespace.getNamespace();
247 * Gets the current ancestor for this query, or null if there is no
248 * ancestor specified.
250 public Key getAncestor() {
251 return ancestor;
255 * Sets an ancestor for this query.
257 * This restricts the query to only return result entities that are
258 * descended from a given entity. In other words, all of the results
259 * will have the ancestor as their parent, or parent's parent, or
260 * etc.
262 * If null is specified, unsets any previously-set ancestor. Passing
263 * {@code null} as a parameter does not query for entities without
264 * ancestors (this type of query is not currently supported).
266 * @return {@code this} (for chaining)
268 * @throws IllegalArgumentException If the ancestor key is incomplete, or if
269 * you try to unset an ancestor and have not set a kind, or if you try to
270 * unset an ancestor and have not previously set an ancestor.
272 public Query setAncestor(Key ancestor) {
273 if (ancestor != null && !ancestor.isComplete()) {
274 throw new IllegalArgumentException(ancestor + " is incomplete.");
275 } else if (ancestor == null) {
276 if (this.ancestor == null) {
277 throw new IllegalArgumentException(
278 "Cannot clear ancestor unless ancestor has already been set");
281 if (ancestor != null) {
282 if (!ancestor.getAppIdNamespace().equals(appIdNamespace)) {
283 throw new IllegalArgumentException(
284 "Namespace of ancestor key and query must match.");
287 this.ancestor = ancestor;
288 return this;
292 * @param distinct if this query should be distinct. This may only be used
293 * when the query has a projection.
294 * @return {@code this} (for chaining)
296 public Query setDistinct(boolean distinct) {
297 this.distinct = distinct;
298 return this;
302 * @return if this query is distinct
303 * @see #setDistinct(boolean)
305 public boolean getDistinct() {
306 return distinct;
310 * @param filter the filter to use for this query, or {@code null}
311 * @return {@code this} (for chaining)
312 * @see CompositeFilter
313 * @see FilterPredicate
315 public Query setFilter(Filter filter) {
316 this.filter = filter;
317 return this;
321 * @return the filter for this query or {@code null}
322 * @see #setFilter(Filter)
324 public Filter getFilter() {
325 return filter;
329 * Add a {@link FilterPredicate} on the specified property.
331 * <p>All {@link FilterPredicate}s added using this message are combined using
332 * {@link CompositeFilterOperator#AND}.
334 * <p>Cannot be used in conjunction with {@link #setFilter(Filter)} which sets
335 * a single {@link Filter} instead of many {@link FilterPredicate}s.
337 * @param propertyName The name of the property to which the filter applies.
338 * @param operator The filter operator.
339 * @param value An instance of a supported datastore type. Note that
340 * entities with multi-value properties identified by {@code propertyName}
341 * will match this filter if the multi-value property has at least one
342 * value that matches the condition expressed by {@code operator} and
343 * {@code value}. For more information on multi-value property filtering
344 * please see the
345 * <a href="http://code.google.com/appengine/docs/java/datastore">
346 * datastore documentation</a>.
348 * @return {@code this} (for chaining)
350 * @throws NullPointerException If {@code propertyName} or {@code operator}
351 * is null.
352 * @throws IllegalArgumentException If {@code value} is not of a
353 * type supported by the datastore. See
354 * {@link DataTypeUtils#isSupportedType(Class)}. Note that unlike
355 * {@link Entity#setProperty(String, Object)}, you cannot provide
356 * a {@link Collection} containing instances of supported types
357 * to this method.
358 * @deprecated Use {@link #setFilter(Filter)}
360 @Deprecated
361 public Query addFilter(String propertyName, FilterOperator operator, Object value) {
362 filterPredicates.add(operator.of(propertyName, value));
363 return this;
367 * Returns a mutable list of the current filter predicates.
369 * @see #addFilter(String, FilterOperator, Object)
370 * @deprecated Use {@link #setFilter(Filter)} and {@link #getFilter()} instead
372 @Deprecated
373 public List<FilterPredicate> getFilterPredicates() {
374 return filterPredicates;
378 * Specify how the query results should be sorted.
380 * The first call to addSort will register the property that will
381 * serve as the primary sort key. A second call to addSort will set
382 * a secondary sort key, etc.
384 * This method will always sort in ascending order. To control the
385 * order of the sort, use {@link #addSort(String,SortDirection)}.
387 * Note that entities with multi-value properties identified by
388 * {@code propertyName} will be sorted by the smallest value in the list.
389 * For more information on sorting properties with multiple values please see
390 * the <a href="http://code.google.com/appengine/docs/java/datastore">
391 * datastore documentation</a>.
393 * @return {@code this} (for chaining)
395 * @throws NullPointerException If any argument is null.
397 public Query addSort(String propertyName) {
398 return addSort(propertyName, SortDirection.ASCENDING);
402 * Specify how the query results should be sorted.
404 * The first call to addSort will register the property that will
405 * serve as the primary sort key. A second call to addSort will set
406 * a secondary sort key, etc.
408 * Note that if {@code direction} is {@link SortDirection#ASCENDING},
409 * entities with multi-value properties identified by
410 * {@code propertyName} will be sorted by the smallest value in the list. If
411 * {@code direction} is {@link SortDirection#DESCENDING}, entities with
412 * multi-value properties identified by {@code propertyName} will be sorted
413 * by the largest value in the list. For more information on sorting
414 * properties with multiple values please see
415 * the <a href="http://code.google.com/appengine/docs/java/datastore">
416 * datastore documentation</a>.
418 * @return {@code this} (for chaining)
420 * @throws NullPointerException If any argument is null.
422 public Query addSort(String propertyName, SortDirection direction) {
423 sortPredicates.add(new SortPredicate(propertyName, direction));
424 return this;
428 * Returns a mutable list of the current sort predicates.
430 public List<SortPredicate> getSortPredicates() {
431 return sortPredicates;
435 * Makes this query fetch and return only keys, not full entities.
437 * @return {@code this} (for chaining)
439 public Query setKeysOnly() {
440 keysOnly = true;
441 return this;
445 * Clears the keys only flag.
447 * @see #setKeysOnly()
448 * @return {@code this} (for chaining)
450 public Query clearKeysOnly() {
451 keysOnly = false;
452 return this;
456 * Adds a projection for this query.
458 * <p>Projections are limited in the following ways:
459 * <ul>
460 * <li>Un-indexed properties cannot be projected and attempting to do so will
461 * result in no entities being returned.
462 * <li>Projection {@link Projection#getName() names} must be unique.
463 * <li>Properties that have an equality filter on them cannot be projected. This includes
464 * the operators {@link FilterOperator#EQUAL} and {@link FilterOperator#IN}.
465 * </ul>
467 * @see #getProjections()
468 * @param projection the projection to add
469 * @return {@code this} (for chaining)
470 * @throws IllegalArgumentException if the query already contains a projection with the same name
472 public Query addProjection(Projection projection) {
473 Preconditions.checkArgument(!projectionMap.containsKey(projection.getName()),
474 "Query already contains projection with name: " + projection.getName());
475 projectionMap.put(projection.getName(), projection);
476 return this;
480 * Returns a mutable collection properties included in the projection for this query.
482 * <p>If empty, the full or keys only entities are returned. Otherwise
483 * partial entities are returned. A non-empty projection is not compatible
484 * with setting keys-only. In this case a {@link IllegalArgumentException}
485 * will be thrown when the query is {@link DatastoreService#prepare(Query) prepared}.
487 * <p>Projection queries are similar to SQL statements of the form:
488 * <pre>SELECT prop1, prop2, ...</pre>
489 * As they return partial entities, which only contain the properties
490 * specified in the projection. However, these entities will only contain
491 * a single value for any multi-valued property and, if a multi-valued
492 * property is specified in the order, an inequality property, or the
493 * projected properties, the entity will be returned multiple times.
494 * Once for each unique combination of values.
496 * <p>Specifying a projection:
497 * <ul>
498 * <li>May change the type of any property returned in a projection.
499 * <li>May change the index requirements for the given query.
500 * <li>Will cause a partial entity to be returned.
501 * <li>Will cause only entities that contain those properties to be returned.
502 * </ul>
503 * However, projection queries are significantly faster than normal queries.
505 * @return a mutable collection properties included in the projection for this query
506 * @see #addProjection(Projection)
508 public Collection<Projection> getProjections() {
509 return projectionMap.values();
513 * Returns true if this query will fetch and return keys only, false if it
514 * will fetch and return full entities.
516 public boolean isKeysOnly() {
517 return keysOnly;
521 * Creates a query sorted in the exact opposite direction as the current one.
523 * This function requires a sort order on {@link Entity#KEY_RESERVED_PROPERTY}
524 * to guarantee that each entity is uniquely identified by the set of properties
525 * used in the sort (which is required to exactly reverse the order of a query).
526 * Advanced users can reverse the sort orders manually if they know the set of
527 * sorted properties meets this requirement without a order on {@link
528 * Entity#KEY_RESERVED_PROPERTY}.
530 * The results of the reverse query will be the same as the results of the forward
531 * query but in reverse direction.
533 * {@link Cursor Cursors} from the original query may also be used in the
534 * reverse query.
536 * @return A new query with the sort order reversed.
537 * @throws IllegalStateException if the current query is not sorted by
538 * {@link Entity#KEY_RESERVED_PROPERTY}.
540 public Query reverse() {
541 List<SortPredicate> order = new ArrayList<SortPredicate>(sortPredicates.size());
542 boolean hasKeyOrder = false;
543 for (SortPredicate sort : sortPredicates) {
544 if (Entity.KEY_RESERVED_PROPERTY.equals(sort.getPropertyName())) {
545 hasKeyOrder = true;
547 if (!distinct || projectionMap.containsKey(sort.getPropertyName())) {
548 order.add(sort.reverse());
549 } else {
550 order.add(sort);
553 Preconditions.checkState(hasKeyOrder, "A sort on " + Entity.KEY_RESERVED_PROPERTY +
554 " is required to reverse a Query");
556 return new Query(kind, ancestor, order, filter, filterPredicates, keysOnly, appIdNamespace,
557 projectionMap.values(), distinct);
560 @Override
561 public boolean equals(Object o) {
562 if (this == o) {
563 return true;
565 if (o == null || getClass() != o.getClass()) {
566 return false;
569 Query query = (Query) o;
571 if (keysOnly != query.keysOnly) {
572 return false;
575 if (!Objects.equals(ancestor, query.ancestor)) {
576 return false;
578 if (!appIdNamespace.equals(query.appIdNamespace)) {
579 return false;
581 if (!Objects.equals(filter, query.filter)) {
582 return false;
584 if (!filterPredicates.equals(query.filterPredicates)) {
585 return false;
587 if (!Objects.equals(kind, query.kind)) {
588 return false;
590 if (!sortPredicates.equals(query.sortPredicates)) {
591 return false;
593 if (!projectionMap.equals(query.projectionMap)) {
594 return false;
596 if (distinct != query.distinct) {
597 return false;
600 return true;
603 @Override
604 public int hashCode() {
605 return Objects.hash(kind, sortPredicates, filterPredicates, filter, ancestor, keysOnly,
606 appIdNamespace, projectionMap, distinct);
610 * Obtains a hash code of the {@code Query} ignoring any 'value' arguments associated with
611 * any query filters.
613 int hashCodeNoFilterValues() {
614 int filterHashCode = 1;
615 for (FilterPredicate filterPred : filterPredicates) {
616 filterHashCode = 31 * filterHashCode + filterPred.hashCodeNoFilterValues();
618 filterHashCode = 31 * filterHashCode + ((filter == null) ? 0 : filter.hashCodeNoFilterValues());
619 return Objects.hash(kind, sortPredicates, filterHashCode, ancestor, keysOnly,
620 appIdNamespace, projectionMap, distinct);
624 * Outputs a SQL like string representing the query.
626 @Override
627 public String toString() {
628 StringBuilder result = new StringBuilder("SELECT ");
629 if (distinct) {
630 result.append("DISTINCT ");
632 if (!projectionMap.isEmpty()) {
633 Joiner.on(", ").appendTo(result, projectionMap.values());
634 } else if (keysOnly) {
635 result.append("__key__");
636 } else {
637 result.append('*');
640 if (kind != null) {
641 result.append(" FROM ");
642 result.append(kind);
645 if (ancestor != null || !filterPredicates.isEmpty() || filter != null) {
646 result.append(" WHERE ");
647 final String AND_SEPARATOR = " AND ";
649 if (filter != null) {
650 result.append(filter);
651 } else if (!filterPredicates.isEmpty()) {
652 Joiner.on(AND_SEPARATOR).appendTo(result, filterPredicates);
655 if (ancestor != null) {
656 if (!filterPredicates.isEmpty() || filter != null) {
657 result.append(AND_SEPARATOR);
659 result.append("__ancestor__ is ");
660 result.append(ancestor);
664 if (!sortPredicates.isEmpty()) {
665 result.append(" ORDER BY ");
666 Joiner.on(", ").appendTo(result, sortPredicates);
668 return result.toString();
672 * SortPredicate is a data container that holds a single sort
673 * predicate.
675 public static final class SortPredicate implements Serializable {
676 @SuppressWarnings("hiding")
677 static final long serialVersionUID = -623786024456258081L;
678 private final String propertyName;
679 private final SortDirection direction;
681 public SortPredicate(String propertyName, SortDirection direction) {
682 if (propertyName == null) {
683 throw new NullPointerException("Property name was null");
686 if (direction == null) {
687 throw new NullPointerException("Direction was null");
690 this.propertyName = propertyName;
691 this.direction = direction;
695 * @return A sort predicate with the direction reversed.
697 public SortPredicate reverse() {
698 return new SortPredicate(propertyName,
699 direction == SortDirection.ASCENDING ?
700 SortDirection.DESCENDING : SortDirection.ASCENDING);
704 * Gets the name of the property to sort on.
706 public String getPropertyName() {
707 return propertyName;
711 * Gets the direction of the sort.
713 public SortDirection getDirection() {
714 return direction;
717 @Override
718 public boolean equals(Object o) {
719 if (this == o) {
720 return true;
722 if (o == null || getClass() != o.getClass()) {
723 return false;
726 SortPredicate that = (SortPredicate) o;
728 if (direction != that.direction) {
729 return false;
731 if (!propertyName.equals(that.propertyName)) {
732 return false;
735 return true;
738 @Override
739 public int hashCode() {
740 int result;
741 result = propertyName.hashCode();
742 result = 31 * result + direction.hashCode();
743 return result;
746 @Override
747 public String toString() {
748 return propertyName + (direction == SortDirection.DESCENDING ? " DESC" : "");
753 * The base class for a query filter.
755 * All sub classes should be immutable.
757 public abstract static class Filter implements Serializable {
758 @SuppressWarnings("hiding")
759 static final long serialVersionUID = -845113806195204425L;
761 Filter() {}
764 * Obtains the hash code for the {@code Filter} ignoring any value parameters associated
765 * with the filter.
767 abstract int hashCodeNoFilterValues();
771 * A {@link Filter} that combines several sub filters using a {@link CompositeFilterOperator}.
773 * For example, to construct a filter of the form <code>a = 1 AND (b = 2 OR c = 3)</code> use:
774 * <pre> {@code
775 * new CompositeFilter(CompositeFilterOperator.AND, Arrays.asList(
776 * new FilterPredicate("a", FilterOperator.EQUAL, 1),
777 * new CompositeFilter(CompositeFilterOperator.OR, Arrays.<Filter>asList(
778 * new FilterPredicate("b", FilterOperator.EQUAL, 2),
779 * new FilterPredicate("c", FilterOperator.EQUAL, 3)))));
780 * }</pre>
781 * or
782 * <pre> {@code
783 * CompositeFilterOperator.and(
784 * FilterOperator.EQUAL.of("a", 1),
785 * CompositeFilterOperator.or(
786 * FilterOperator.EQUAL.of("b", 2),
787 * FilterOperator.EQUAL.of("c", 3)));
788 * }</pre>
790 public static final class CompositeFilter extends Filter {
791 @SuppressWarnings("hiding")
792 static final long serialVersionUID = 7930286402872420509L;
794 private final CompositeFilterOperator operator;
795 private final ImmutableList<Filter> subFilters;
797 public CompositeFilter(CompositeFilterOperator operator, Collection<Filter> subFilters) {
798 Preconditions.checkArgument(subFilters.size() >= 2, "At least two sub filters are required.");
799 this.operator = checkNotNull(operator);
800 this.subFilters = ImmutableList.copyOf(subFilters);
804 * @return the operator
806 public CompositeFilterOperator getOperator() {
807 return operator;
811 * @return an immutable list of sub filters
813 public List<Filter> getSubFilters() {
814 return subFilters;
817 @Override
818 public String toString() {
819 StringBuilder builder = new StringBuilder();
820 builder.append('(');
821 Joiner.on(" " + operator + " ").appendTo(builder, subFilters);
822 builder.append(')');
823 return builder.toString();
826 @Override
827 public int hashCode() {
828 return Objects.hash(operator, subFilters);
831 @Override
832 int hashCodeNoFilterValues() {
833 int result = 1;
834 result = 31 * result + operator.hashCode();
835 for (Filter filter : subFilters) {
836 result = 31 * result + filter.hashCodeNoFilterValues();
838 return result;
841 @Override
842 public boolean equals(Object obj) {
843 if (this == obj) return true;
844 if (!(obj instanceof CompositeFilter)) return false;
845 CompositeFilter other = (CompositeFilter) obj;
846 if (operator != other.operator) return false;
847 return subFilters.equals(other.subFilters);
852 * A {@link Filter} on a single property.
854 public static final class FilterPredicate extends Filter {
855 @SuppressWarnings("hiding")
856 static final long serialVersionUID = 7681475799401864259L;
857 private final String propertyName;
858 private final FilterOperator operator;
859 private final Object value;
862 * Constructs a filter predicate from the given parameters.
864 * @param propertyName the name of the property on which to filter
865 * @param operator the operator to apply
866 * @param value A single instances of a supported type or if {@code
867 * operator} is {@link FilterOperator#IN} a non-empty {@link Iterable}
868 * object containing instances of supported types.
870 * @throws IllegalArgumentException If the provided filter values are not
871 * supported.
873 * @see DataTypeUtils#isSupportedType(Class)
876 public FilterPredicate(String propertyName, FilterOperator operator, Object value) {
877 if (propertyName == null) {
878 throw new NullPointerException("Property name was null");
879 } else if (operator == null) {
880 throw new NullPointerException("Operator was null");
881 } else if (operator == FilterOperator.IN) {
882 if (!(value instanceof Collection<?>) && value instanceof Iterable<?>) {
883 List<Object> newValue = new ArrayList<Object>();
884 Iterables.addAll(newValue, (Iterable<?>) value);
885 value = newValue;
887 DataTypeUtils.checkSupportedValue(propertyName, value, true, true);
888 } else {
889 DataTypeUtils.checkSupportedValue(propertyName, value, false, false);
891 this.propertyName = propertyName;
892 this.operator = operator;
893 this.value = value;
897 * Gets the name of the property to be filtered on.
899 public String getPropertyName() {
900 return propertyName;
904 * Gets the operator describing how to apply the filter.
906 public FilterOperator getOperator() {
907 return operator;
911 * Gets the argument to the filter operator.
913 public Object getValue() {
914 return value;
917 @Override
918 public boolean equals(Object o) {
919 if (this == o) {
920 return true;
922 if (o == null || getClass() != o.getClass()) {
923 return false;
926 FilterPredicate that = (FilterPredicate) o;
928 if (operator != that.operator) {
929 return false;
931 if (!propertyName.equals(that.propertyName)) {
932 return false;
934 if (!Objects.equals(value, that.value)) {
935 return false;
938 return true;
941 @Override
942 public int hashCode() {
943 return Objects.hash(propertyName, operator, value);
946 @Override
947 int hashCodeNoFilterValues() {
948 return Objects.hash(propertyName, operator);
951 @Override
952 public String toString() {
953 return propertyName + " " + operator + " " + (value != null ? value : "NULL");