Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / IndexTranslator.java
blob8e9d041d0a023fa6bce477ab20a38214ccd26a7b
1 // Copyright 2013 Google Inc. All Rights Reserved.
2 package com.google.appengine.api.datastore;
4 import com.google.apphosting.api.AppEngineInternal;
5 import com.google.common.base.Function;
6 import com.google.common.collect.ImmutableList;
7 import com.google.common.collect.Iterables;
8 import com.google.storage.onestore.v3.OnestoreEntity;
10 import java.util.List;
12 /**
13 * Helper class to translate between {@link Index} to {@link
14 * com.google.storage.onestore.v3.OnestoreEntity.Index}.
17 @AppEngineInternal
18 public class IndexTranslator {
20 public static OnestoreEntity.Index convertToPb(Index index) {
21 OnestoreEntity.Index value = new OnestoreEntity.Index();
22 value.setEntityType(index.getKind());
23 value.setAncestor(index.isAncestor());
24 for (Index.Property property : index.getProperties()) {
25 value.mutablePropertys().add(convertToPb(property));
27 return value;
30 public static OnestoreEntity.Index.Property convertToPb(Index.Property property) {
31 OnestoreEntity.Index.Property value = new OnestoreEntity.Index.Property();
32 value.setName(property.getName());
33 Query.SortDirection dir = property.getDirection();
34 if (dir != null) {
35 value.setDirection(OnestoreEntity.Index.Property.Direction.valueOf(dir.name()));
37 return value;
40 public static Index convertFromPb(OnestoreEntity.CompositeIndex ci) {
41 OnestoreEntity.Index index = ci.getDefinition();
42 List<Index.Property> properties = ImmutableList.copyOf(Iterables.transform(index.propertys(),
43 new Function<OnestoreEntity.Index.Property, Index.Property>() {
44 @Override public Index.Property apply(OnestoreEntity.Index.Property property) {
45 return convertFromPb(property);
47 }));
48 return new Index(ci.getId(), index.getEntityType(), index.isAncestor(), properties);
51 public static Index.Property convertFromPb(OnestoreEntity.Index.Property property) {
52 Query.SortDirection dir =
53 property.hasDirection()
54 ? Query.SortDirection.valueOf(property.getDirectionEnum().name())
55 : null;
56 return new Index.Property(property.getName(), dir);
59 public static Index convertFromPb(OnestoreEntity.Index index) {
60 return convertFromPb(new OnestoreEntity.CompositeIndex().setId(0).setDefinition(index));