Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / EntityComparator.java
blobe722f087c09956b70d37bc8e8c90db05ea4c78e9
1 package com.google.appengine.api.datastore;
3 import static com.google.appengine.api.datastore.DataTypeTranslator.getComparablePropertyValue;
5 import com.google.apphosting.datastore.DatastoreV3Pb.Query.Filter;
6 import com.google.apphosting.datastore.DatastoreV3Pb.Query.Order;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.List;
13 /**
14 * A comparator with the same ordering as {@link EntityProtoComparators} which uses
15 * Entity objects rather than protos.
17 class EntityComparator extends BaseEntityComparator<Entity> {
19 EntityComparator(List<Order> orders) {
20 super(orders, Collections.<Filter>emptyList());
23 @Override
24 List<Comparable<Object>> getComparablePropertyValues(Entity entity, String propertyName) {
25 Object prop;
26 if (propertyName.equals(Entity.KEY_RESERVED_PROPERTY)) {
27 prop = entity.getKey();
28 } else if (!entity.hasProperty(propertyName)) {
29 return null;
30 } else {
31 prop = entity.getProperty(propertyName);
33 if (prop instanceof Collection<?>) {
34 Collection<?> props = (Collection<?>) prop;
35 if (props.isEmpty()) {
36 return Collections.singletonList(null);
38 List<Comparable<Object>> comparableProps = new ArrayList<>(props.size());
39 for (Object curProp : props) {
40 comparableProps.add(getComparablePropertyValue(curProp));
42 return comparableProps;
43 } else {
44 return Collections.singletonList(getComparablePropertyValue(prop));