1.9.30 sync.
[gae.git] / java / src / main / com / google / appengine / api / datastore / QuerySplitComponent.java
blob97be7a6a7207134f05afb4b7d49f068291b69506
1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.datastore;
5 import com.google.appengine.api.datastore.Query.FilterPredicate;
6 import com.google.appengine.api.datastore.Query.SortDirection;
7 import com.google.appengine.api.datastore.Query.SortPredicate;
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.List;
13 /**
14 * A class that holds information about a given query component that will later
15 * be converted into a {@link MultiQueryComponent}.
18 class QuerySplitComponent implements Comparable<QuerySplitComponent> {
19 public enum Order {SEQUENTIAL, ARBITRARY}
21 private final Order order;
22 private final String propertyName;
23 private final int sortIndex;
24 private final SortDirection direction;
25 private final List<List<FilterPredicate>> filters =
26 new ArrayList<List<FilterPredicate>>();
28 /**
29 * Constructs a new component and uses {@code sorts} to determine how this
30 * property should be handled in {@link QuerySplitHelper}
32 * @param propertyName the name of the property to which this component
33 * applies
34 * @param sorts the sort values from the original query
36 public QuerySplitComponent(String propertyName, List<SortPredicate> sorts) {
37 this.propertyName = propertyName;
38 for (int i = 0; i < sorts.size(); ++i) {
39 if (sorts.get(i).getPropertyName().equals(propertyName)) {
40 this.order = Order.SEQUENTIAL;
41 this.sortIndex = i;
42 this.direction = sorts.get(i).getDirection();
43 return;
46 this.order = Order.ARBITRARY;
47 this.sortIndex = -1;
48 this.direction = null;
51 /**
52 * Adds a set of filters that are then applied to {@link Query}s generated
53 * by {@link MultiQueryBuilder} (in order if order = {@link
54 * Order#SEQUENTIAL}).
56 public void addFilters(FilterPredicate... filters) {
57 this.filters.add(Arrays.asList(filters));
60 public List<List<FilterPredicate>> getFilters() {
61 return filters;
64 public Order getOrder() {
65 return order;
68 public int getSortIndex() {
69 return sortIndex;
72 public SortDirection getDirection() {
73 return direction;
76 @Override
77 public int compareTo(QuerySplitComponent o) {
78 if (!order.equals(o.order)) {
79 return order.compareTo(o.order);
80 } else {
81 return sortIndex - o.sortIndex;
85 @Override
86 public int hashCode() {
87 return Arrays.hashCode(new Object[] {direction, filters, order, propertyName, sortIndex});
90 @Override
91 public boolean equals(Object obj) {
92 if (this == obj) {
93 return true;
94 } else if (!(obj instanceof QuerySplitComponent)) {
95 return false;
98 QuerySplitComponent other = (QuerySplitComponent) obj;
100 return (direction == other.direction)
101 || ((direction != null) && (direction.equals(other.direction)))
102 && (filters == other.filters) || ((filters != null) && (filters.equals(other.filters)))
103 && (order == other.order) || ((order != null) && (order.equals(other.order)))
104 && (propertyName == other.propertyName)
105 || ((propertyName != null) && (propertyName.equals(other.propertyName)))
106 && (sortIndex == other.sortIndex);
109 @Override
110 public String toString() {
111 String result = "QuerySplitComponent [filters=" + filters;
112 if (direction != null) {
113 result += ", direction=" + direction + ", " + "sortIndex=" + sortIndex;
115 return result + "]";