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
;
10 import java
.util
.ListIterator
;
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
;
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())) {
34 return !matcherItr
.hasPrevious();
38 public List
<Property
> preferredIndexProperties() {
39 List
<Property
> indexProps
= Lists
.newArrayListWithExpectedSize(matcherProperties
.size());
40 for (Property prop
: matcherProperties
) {
41 if (!prop
.hasDirection()) {
43 prop
.setDirection(Direction
.ASCENDING
);
52 return matcherProperties
.size();
56 public String
toString() {
57 return "OrderedIndexComponent: " + matcherProperties
;
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()));