1 package com
.google
.appengine
.api
.datastore
;
3 import static com
.google
.appengine
.api
.datastore
.DataTypeTranslator
.getComparablePropertyValue
;
5 import com
.google
.appengine
.api
.datastore
.Query
.SortPredicate
;
6 import com
.google
.apphosting
.datastore
.DatastoreV3Pb
;
7 import com
.google
.apphosting
.datastore
.DatastoreV3Pb
.Query
.Filter
;
8 import com
.google
.apphosting
.datastore
.DatastoreV3Pb
.Query
.Order
;
10 import java
.util
.ArrayList
;
11 import java
.util
.Collection
;
12 import java
.util
.Collections
;
13 import java
.util
.List
;
16 * A comparator with the same ordering as {@link EntityProtoComparators} which uses
17 * Entity objects rather than protos.
19 class EntityComparator
extends BaseEntityComparator
<Entity
> {
21 EntityComparator(List
<Order
> orders
) {
22 super(orders
, Collections
.<Filter
>emptyList());
26 List
<Comparable
<Object
>> getComparablePropertyValues(Entity entity
, String propertyName
) {
28 if (propertyName
.equals(Entity
.KEY_RESERVED_PROPERTY
)) {
29 prop
= entity
.getKey();
30 } else if (!entity
.hasProperty(propertyName
)) {
33 prop
= entity
.getProperty(propertyName
);
35 if (prop
instanceof Collection
<?
>) {
36 Collection
<?
> props
= (Collection
<?
>) prop
;
37 if (props
.isEmpty()) {
38 return Collections
.singletonList(null);
40 List
<Comparable
<Object
>> comparableProps
= new ArrayList
<>(props
.size());
41 for (Object curProp
: props
) {
42 comparableProps
.add(getComparablePropertyValue(curProp
));
44 return comparableProps
;
46 return Collections
.singletonList(getComparablePropertyValue(prop
));
50 static EntityComparator
fromSortPredicates(List
<SortPredicate
> sortPredicates
) {
51 List
<DatastoreV3Pb
.Query
.Order
> orders
= new ArrayList
<>(sortPredicates
.size());
52 for (SortPredicate predicate
: sortPredicates
) {
53 orders
.add(QueryTranslator
.convertSortPredicateToPb(predicate
));
55 return new EntityComparator(orders
);