22
33import com .google .api .services .datastore .DatastoreV1 ;
44import com .google .common .collect .ImmutableSortedMap ;
5+ import com .google .gcloud .datastore .Value .Type ;
56
7+ import java .util .Arrays ;
68import java .util .HashMap ;
9+ import java .util .List ;
710import java .util .Map ;
811import java .util .Set ;
912
13+
14+ // TODO: make entity and Partial entity extends it and to tests + documentaion the option
15+ // of list and direct value set/get.
16+
1017/**
1118 * A container of properties (name and Value pairs).
1219 */
@@ -31,6 +38,7 @@ public Builder(PropertyContainer<E, B> entity) {
3138 properties = new HashMap <>(entity .properties ());
3239 }
3340
41+ @ SuppressWarnings ("unchecked" )
3442 protected B self () {
3543 return (B ) this ;
3644 }
@@ -50,18 +58,60 @@ public B setProperty(String name, Value<?, ?, ?> value) {
5058 return self ();
5159 }
5260
53- setNullProperty (String name );
54- longValue
55- booleanValue
56- doubleValue
57- dateTimeValue
58- PartialEntityValue
59- KeyValue
60- ListValue
61- blobValue
62- rawValue // should work for all....
61+ public B setNullProperty (String name ) {
62+ properties .put (name , new NullValue ());
63+ return self ();
64+ }
65+
66+ public B setStringProperty (String name , String value ) {
67+ properties .put (name , new StringValue (value ));
68+ return self ();
69+ }
70+
71+ public B setLongProperty (String name , long value ) {
72+ properties .put (name , new LongValue (value ));
73+ return self ();
74+ }
75+
76+ public B setDoubleProperty (String name , double value ) {
77+ properties .put (name , new DoubleValue (value ));
78+ return self ();
79+ }
80+
81+ public B setBooleanProperty (String name , boolean value ) {
82+ properties .put (name , new BooleanValue (value ));
83+ return self ();
84+ }
85+
86+ public B setDateAndTimeProperty (String name , DateAndTime value ) {
87+ properties .put (name , new DateAndTimeValue (value ));
88+ return self ();
89+ }
6390
91+ public B setKeyProperty (String name , Key value ) {
92+ properties .put (name , new KeyValue (value ));
93+ return self ();
94+ }
6495
96+ public B setPartialEntityProperty (String name , PartialEntity value ) {
97+ properties .put (name , new PartialEntityValue (value ));
98+ return self ();
99+ }
100+
101+ public B setListProperty (String name , List <Value <?, ?, ?>> values ) {
102+ properties .put (name , new ListValue (values ));
103+ return self ();
104+ }
105+
106+ public B setListProperty (String name , Value <?, ?, ?>... value ) {
107+ properties .put (name , new ListValue (Arrays .asList (value )));
108+ return self ();
109+ }
110+
111+ public B setBlobProperty (String name , Blob value ) {
112+ properties .put (name , new BlobValue (value ));
113+ return self ();
114+ }
65115
66116 public E build () {
67117 return build (ImmutableSortedMap .copyOf (properties ));
@@ -74,32 +124,81 @@ protected PropertyContainer(ImmutableSortedMap<String, Value<?, ?, ?>> propertie
74124 this .properties = properties ;
75125 }
76126
127+ /**
128+ * Returns {@code true} if there is such property with the given {@code name}.
129+ */
77130 public boolean hasProperty (String name ) {
78131 return properties .containsKey (name );
79132 }
80133
81- @ SuppressWarnings ("unchecked" )
134+ /**
135+ * Returns the {@link Value} of property with the given {@code name}.
136+ *
137+ * @throws DatastoreServiceException if not such property.
138+ */
82139 public <V extends Value <?, ?, ?>> V property (String name ) {
83- return (V ) properties .get (name );
140+ @ SuppressWarnings ("unchecked" )
141+ V property = (V ) properties .get (name );
142+ if (property == null ) {
143+ throw DatastoreServiceException .throwInvalidRequest ("No such property %s" , name );
144+ }
145+ return property ;
84146 }
85147
86- public boolean isNull (String name ) {
87- return properties .get (name ) instanceof NullValue ;
148+ public Type propertyType (String name ) {
149+ return property (name ).type ();
150+ }
151+
152+ public boolean isNullProperty (String name ) {
153+ return property (name ) instanceof NullValue ;
88154 }
89155
90156 public String stringProperty (String name ) {
91157 return ((StringValue ) property (name )).get ();
92158 }
93159
94- longProperty
95- booleanValue
96- doubleValue
97- dateTimeValue
98- PartialEntityValue
99- KeyValue
100- ListValue
101- blobValue
102- rawValue // should work for all....
160+ public long longProperty (String name ) {
161+ return ((LongValue ) property (name )).get ();
162+ }
163+
164+ public double doubleProperty (String name ) {
165+ return ((DoubleValue ) property (name )).get ();
166+ }
167+
168+ public boolean booleanProperty (String name ) {
169+ return ((BooleanValue ) property (name )).get ();
170+ }
171+
172+ public DateAndTime dateAndTimeProperty (String name ) {
173+ return ((DateAndTimeValue ) property (name )).get ();
174+ }
175+
176+ public Key keyProperty (String name ) {
177+ return ((KeyValue ) property (name )).get ();
178+ }
179+
180+ public PartialEntity partialEntityProperty (String name ) {
181+ return ((PartialEntityValue ) property (name )).get ();
182+ }
183+
184+ public List <? extends Value <?, ?, ?>> listProperty (String name ) {
185+ return ((ListValue ) property (name )).get ();
186+ }
187+
188+ public Blob blobProperty (String name ) {
189+ return ((BlobValue ) property (name )).get ();
190+ }
191+
192+ /**
193+ * Returns the property's value as {@code RawValue}.
194+ */
195+ public RawValue rawValueProperty (String name ) {
196+ Value <?, ?, ?> value = property (name );
197+ if (value instanceof RawValue ) {
198+ return (RawValue ) value ;
199+ }
200+ return new RawValue (value .toPb ());
201+ }
103202
104203 public Set <String > propertyNames () {
105204 return properties .keySet ();
0 commit comments