Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / EntityComparator.java
blob29b512f2cbb83f1409f2c4eabde2d153e6f05bac
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;
15 /**
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());
25 @Override
26 List<Comparable<Object>> getComparablePropertyValues(Entity entity, String propertyName) {
27 Object prop;
28 if (propertyName.equals(Entity.KEY_RESERVED_PROPERTY)) {
29 prop = entity.getKey();
30 } else if (!entity.hasProperty(propertyName)) {
31 return null;
32 } else {
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;
45 } else {
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);