Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / datastore / PropertyProjection.java
blob911b1ad61a3440ebc1ee7d74f5313fdcb1c1c9fa
1 // Copyright 2012 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.datastore;
5 import static com.google.common.base.Preconditions.checkArgument;
6 import static com.google.common.base.Preconditions.checkNotNull;
8 import java.util.Map;
9 import java.util.Objects;
11 /**
12 * A property projection.
14 * If specified on a query, this will cause the query return the specified property.
16 * @see Query#getProjections()
18 public final class PropertyProjection extends Projection {
19 private final String propertyName;
20 private final Class<?> type;
22 /**
23 * Constructs a property projection.
25 * If type is specified, {@link RawValue#asType(Class)} will be used to restore
26 * the original value of the property. Otherwise instances of {@link RawValue}
27 * will be returned.
29 * @param propertyName The name of the property to project
30 * @param type The type of values stored in the projected properties or
31 * {@code null} if the type is not known or variable. If {@code null}, {@link
32 * RawValue RawValues} are returned.
34 public PropertyProjection(String propertyName, Class<?> type) {
35 checkArgument(type == null || DataTypeTranslator.getTypeMap().containsKey(type),
36 "Unsupported type: " + type);
37 this.propertyName = checkNotNull(propertyName);
38 this.type = type;
41 @Override
42 public String getName() {
43 return propertyName;
46 /**
47 * Returns the type specified for this projection.
49 public Class<?> getType() {
50 return type;
53 @Override
54 String getPropertyName() {
55 return propertyName;
58 @Override
59 Object getValue(Map<String, Object> values) {
60 checkArgument(values.containsKey(propertyName));
61 Object value = values.get(propertyName);
62 if (type != null && value != null) {
63 checkArgument(value instanceof RawValue);
64 value = ((RawValue) value).asType(type);
66 return value;
69 @Override
70 public String toString() {
71 return propertyName;
74 @Override
75 public int hashCode() {
76 return Objects.hash(propertyName, type);
79 @Override
80 public boolean equals(Object obj) {
81 if (this == obj) return true;
82 if (obj == null) return false;
83 if (getClass() != obj.getClass()) return false;
84 PropertyProjection other = (PropertyProjection) obj;
85 if (!propertyName.equals(other.propertyName)) return false;
86 if (type == null) {
87 if (other.type != null) return false;
88 } else if (!type.equals(other.type)) return false;
89 return true;