Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / BaseQuerySplitter.java
blob548f7bda93a0a23ecb1e60dcdb1849d644fdfd84
1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.datastore;
5 import com.google.appengine.api.datastore.Query.SortDirection;
7 import java.util.Collections;
8 import java.util.Comparator;
10 /**
11 * Base implementation of {@link QuerySplitter} with logic common to all
12 * splitters.
15 abstract class BaseQuerySplitter implements QuerySplitter {
16 protected static final Comparator<ComparableValue> VALUE_COMPARATOR_ASC =
17 new Comparator<ComparableValue>() {
18 @Override
19 public int compare(ComparableValue o1, ComparableValue o2) {
20 return EntityProtoComparators.MULTI_TYPE_COMPARATOR.compare(
21 o1.comparableValue, o2.comparableValue);
25 protected static final Comparator<ComparableValue> VALUE_COMPARATOR_DESC =
26 Collections.reverseOrder(VALUE_COMPARATOR_ASC);
28 protected BaseQuerySplitter() {
31 /**
32 * A class that allows arbitrary property values to be compared while
33 * preserving their original value.
35 * <p>This is needed because converting a value into a {@code
36 * Comparable<Object>} is potentially destructive (meaning the {@code
37 * Comparable<Object>} value cannot be used as a property value).
39 protected static class ComparableValue {
40 private final Comparable<Object> comparableValue;
41 private final Object originalValue;
43 public ComparableValue(Object value) {
44 this.comparableValue = DataTypeTranslator.getComparablePropertyValue(value);
45 this.originalValue = value;
48 public Object getValue() {
49 return originalValue;
53 protected static Comparator<ComparableValue> getValueComparator(SortDirection sortDirection) {
54 if (sortDirection == SortDirection.DESCENDING) {
55 return VALUE_COMPARATOR_DESC;
56 } else {
57 return VALUE_COMPARATOR_ASC;