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
;
9 import java
.util
.Objects
;
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
;
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}
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
);
42 public String
getName() {
47 * Returns the type specified for this projection.
49 public Class
<?
> getType() {
54 String
getPropertyName() {
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
);
70 public String
toString() {
75 public int hashCode() {
76 return Objects
.hash(propertyName
, type
);
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;
87 if (other
.type
!= null) return false;
88 } else if (!type
.equals(other
.type
)) return false;