24
24
import static org .junit .Assert .assertNull ;
25
25
26
26
import com .google .common .collect .ImmutableList ;
27
+ import com .google .common .collect .ImmutableMap ;
27
28
import com .google .common .collect .ImmutableSet ;
28
29
import com .google .common .collect .Iterators ;
29
30
import com .google .gcloud .datastore .Cursor ;
49
50
import com .google .gcloud .datastore .StructuredQuery .Projection ;
50
51
import com .google .gcloud .datastore .StructuredQuery .PropertyFilter ;
51
52
import com .google .gcloud .datastore .Transaction ;
52
- import com .google .gcloud .datastore .Value ;
53
53
import com .google .gcloud .datastore .testing .LocalGcdHelper ;
54
54
55
55
import org .junit .AfterClass ;
@@ -715,9 +715,8 @@ private Cursor cursorPaging(int pageSize, Cursor pageCursor) {
715
715
queryBuilder .startCursor (pageCursor );
716
716
}
717
717
QueryResults <Entity > tasks = datastore .run (queryBuilder .build ());
718
- Entity task ;
719
718
while (tasks .hasNext ()) {
720
- task = tasks .next ();
719
+ Entity task = tasks .next ();
721
720
// do something with the task
722
721
}
723
722
Cursor nextPageCursor = tasks .cursorAfter ();
@@ -919,17 +918,16 @@ public void testPropertyRunQuery() {
919
918
Key property = results .next ();
920
919
String kind = property .ancestors ().get (property .ancestors ().size () - 1 ).name ();
921
920
String propertyName = property .name ();
922
- if (!propertiesByKind .containsKey (kind )) {
923
- propertiesByKind .put (kind , new HashSet <String >());
921
+ Collection <String > properties = propertiesByKind .get (kind );
922
+ if (properties == null ) {
923
+ properties = new HashSet <>();
924
+ propertiesByKind .put (kind , properties );
924
925
}
925
- propertiesByKind . get ( kind ) .add (propertyName );
926
+ properties .add (propertyName );
926
927
}
927
928
// [END property_run_query]
928
- Map <String , Collection <String >> expected = new HashMap <>();
929
- expected .put (
930
- "Task" ,
931
- ImmutableSet .of (
932
- "done" , "type" , "done" , "completed" , "priority" , "created" , "percent_complete" , "tag" ));
929
+ Map <String , ImmutableSet <String >> expected = ImmutableMap .of ("Task" , ImmutableSet .of (
930
+ "done" , "type" , "done" , "completed" , "priority" , "created" , "percent_complete" , "tag" ));
933
931
assertEquals (expected , propertiesByKind );
934
932
}
935
933
@@ -940,30 +938,34 @@ public void testPropertyByKindRunQuery() {
940
938
Key key = datastore .newKeyFactory ().kind ("__kind__" ).newKey ("Task" );
941
939
Query <Entity > query = Query .entityQueryBuilder ()
942
940
.kind ("__property__" )
943
- .filter (PropertyFilter .hasAncestor (key ))
941
+ .filter (PropertyFilter .hasAncestor (key ))
944
942
.build ();
945
943
QueryResults <Entity > results = datastore .run (query );
946
944
Map <String , Collection <String >> representationsByProperty = new HashMap <>();
947
945
while (results .hasNext ()) {
948
946
Entity property = results .next ();
949
947
String propertyName = property .key ().name ();
950
- List <? extends Value <?>> representations = property .getList ("property_representation" );
951
- if (!representationsByProperty .containsKey (propertyName )) {
952
- representationsByProperty .put (propertyName , new HashSet <String >());
948
+ List <StringValue > representations =
949
+ (List <StringValue >) property .getList ("property_representation" );
950
+ Collection <String > currentRepresentations = representationsByProperty .get (propertyName );
951
+ if (currentRepresentations == null ) {
952
+ currentRepresentations = new HashSet <>();
953
+ representationsByProperty .put (propertyName , currentRepresentations );
953
954
}
954
- for (Value <?> value : representations ) {
955
- representationsByProperty . get ( propertyName ). add (( String ) value .get ());
955
+ for (StringValue value : representations ) {
956
+ currentRepresentations . add (value .get ());
956
957
}
957
958
}
958
959
// [END property_by_kind_run_query]
959
- Map <String , Collection <String >> expected = new HashMap <>();
960
- expected .put ("type" , Collections .singleton ("STRING" ));
961
- expected .put ("done" , Collections .singleton ("BOOLEAN" ));
962
- expected .put ("completed" , Collections .singleton ("BOOLEAN" ));
963
- expected .put ("priority" , Collections .singleton ("INT64" ));
964
- expected .put ("created" , Collections .singleton ("INT64" ));
965
- expected .put ("percent_complete" , Collections .singleton ("DOUBLE" ));
966
- expected .put ("tag" , Collections .singleton ("STRING" ));
960
+ Map <String , Collection <String >> expected = ImmutableMap .<String , Collection <String >>builder ()
961
+ .put ("type" , Collections .singleton ("STRING" ))
962
+ .put ("done" , Collections .singleton ("BOOLEAN" ))
963
+ .put ("completed" , Collections .singleton ("BOOLEAN" ))
964
+ .put ("priority" , Collections .singleton ("INT64" ))
965
+ .put ("created" , Collections .singleton ("INT64" ))
966
+ .put ("percent_complete" , Collections .singleton ("DOUBLE" ))
967
+ .put ("tag" , Collections .singleton ("STRING" ))
968
+ .build ();
967
969
assertEquals (expected , representationsByProperty );
968
970
}
969
971
@@ -986,14 +988,16 @@ public void testPropertyFilteringRunQuery() {
986
988
Key property = results .next ();
987
989
String kind = property .ancestors ().get (property .ancestors ().size () - 1 ).name ();
988
990
String propertyName = property .name ();
989
- if (!propertiesByKind .containsKey (kind )) {
990
- propertiesByKind .put (kind , new HashSet <String >());
991
+ Collection <String > properties = propertiesByKind .get (kind );
992
+ if (properties == null ) {
993
+ properties = new HashSet <String >();
994
+ propertiesByKind .put (kind , properties );
991
995
}
992
- propertiesByKind . get ( kind ) .add (propertyName );
996
+ properties .add (propertyName );
993
997
}
994
998
// [END property_filtering_run_query]
995
- Map <String , Collection <String >> expected = new HashMap <>();
996
- expected . put ("Task" , ImmutableSet .of ("priority" , "tag" , "type" ));
999
+ Map <String , ImmutableSet <String >> expected =
1000
+ ImmutableMap . of ("Task" , ImmutableSet .of ("priority" , "tag" , "type" ));
997
1001
assertEquals (expected , propertiesByKind );
998
1002
}
999
1003
0 commit comments