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
;
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
>>();
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
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
;
42 this.direction
= sorts
.get(i
).getDirection();
46 this.order
= Order
.ARBITRARY
;
48 this.direction
= null;
52 * Adds a set of filters that are then applied to {@link Query}s generated
53 * by {@link MultiQueryBuilder} (in order if order = {@link
56 public void addFilters(FilterPredicate
... filters
) {
57 this.filters
.add(Arrays
.asList(filters
));
60 public List
<List
<FilterPredicate
>> getFilters() {
64 public Order
getOrder() {
68 public int getSortIndex() {
72 public SortDirection
getDirection() {
77 public int compareTo(QuerySplitComponent o
) {
78 if (!order
.equals(o
.order
)) {
79 return order
.compareTo(o
.order
);
81 return sortIndex
- o
.sortIndex
;
86 public int hashCode() {
87 return Arrays
.hashCode(new Object
[] {direction
, filters
, order
, propertyName
, sortIndex
});
91 public boolean equals(Object obj
) {
94 } else if (!(obj
instanceof QuerySplitComponent
)) {
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
);
110 public String
toString() {
111 String result
= "QuerySplitComponent [filters=" + filters
;
112 if (direction
!= null) {
113 result
+= ", direction=" + direction
+ ", " + "sortIndex=" + sortIndex
;