Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / OrderedIndexComponent.java
blobe7b0ffe97007ef3dca59371fd54975d27a009871
1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.datastore;
5 import com.google.common.collect.Lists;
6 import com.google.storage.onestore.v3.OnestoreEntity.Index.Property;
7 import com.google.storage.onestore.v3.OnestoreEntity.Index.Property.Direction;
9 import java.util.List;
10 import java.util.ListIterator;
12 /**
13 * Implements an IndexComponent for an ordered list of properties.
15 * Currently, this is used for ordering constraints.
18 class OrderedIndexComponent implements IndexComponent {
19 private final List<Property> matcherProperties;
21 public OrderedIndexComponent(List<Property> orderedGroup) {
22 this.matcherProperties = orderedGroup;
25 @Override
26 public boolean matches(List<Property> indexProperties) {
27 ListIterator<Property> indexItr = indexProperties.listIterator(indexProperties.size());
28 ListIterator<Property> matcherItr = matcherProperties.listIterator(matcherProperties.size());
29 while (indexItr.hasPrevious() && matcherItr.hasPrevious()) {
30 if (!propertySatisfies(matcherItr.previous(), indexItr.previous())) {
31 return false;
34 return !matcherItr.hasPrevious();
37 @Override
38 public List<Property> preferredIndexProperties() {
39 List<Property> indexProps = Lists.newArrayListWithExpectedSize(matcherProperties.size());
40 for (Property prop : matcherProperties) {
41 if (!prop.hasDirection()) {
42 prop = prop.clone();
43 prop.setDirection(Direction.ASCENDING);
45 indexProps.add(prop);
47 return indexProps;
50 @Override
51 public int size() {
52 return matcherProperties.size();
55 @Override
56 public String toString() {
57 return "OrderedIndexComponent: " + matcherProperties;
60 /**
61 * A {@link Property} satisfies a constraint if it is equal to the constraint. However,
62 * if the constraint does not specify a direction, then the direction of the actual
63 * property does not matter.
65 private boolean propertySatisfies(Property constraint, Property property) {
66 return constraint.equals(property) ||
67 (!constraint.hasDirection() && constraint.getName().equals(property.getName()));