@Test public void testMakeKey_Key() { assertEquals( Key.newBuilder() .addPath(CHILD.getPath(0)) .build(), makeKey(CHILD).build()); }
public static Key.Builder makeKey(Object... elements) { Key.Builder key = Key.newBuilder(); PartitionId partitionId = null; for (int pathIndex = 0; pathIndex < elements.length; pathIndex += 2) { PathElement.Builder pathElement = PathElement.newBuilder(); Object element = elements[pathIndex]; if (element instanceof Key) { Key subKey = (Key) element; if (partitionId == null) { partitionId = subKey.getPartitionId(); } else if (!partitionId.equals(subKey.getPartitionId())) { throw new IllegalArgumentException("Partition IDs did not match, found: " + partitionId + " and " + subKey.getPartitionId()); } key.addAllPath(((Key) element).getPathList()); pathIndex--; } else { String kind; try { kind = (String) element; } catch (ClassCastException e) { throw new IllegalArgumentException("Expected string or Key, got: " + element.getClass()); } pathElement.setKind(kind); if (pathIndex + 1 < elements.length) { Object value = elements[pathIndex + 1]; if (value instanceof String) { pathElement.setName((String) value); } else if (value instanceof Long) { pathElement.setId((Long) value); } else if (value instanceof Integer) { pathElement.setId((Integer) value); } else if (value instanceof Short) { pathElement.setId((Short) value); } else { throw new IllegalArgumentException( "Expected string or integer, got: " + value.getClass()); } } key.addPath(pathElement); } } if (partitionId != null && !partitionId.equals(PartitionId.getDefaultInstance())) { key.setPartitionId(partitionId); } return key; }
DatastoreHelper { public static Key.Builder makeKey(Object... elements) { Key.Builder key = Key.newBuilder(); PartitionId partitionId = null; for (int pathIndex = 0; pathIndex < elements.length; pathIndex += 2) { PathElement.Builder pathElement = PathElement.newBuilder(); Object element = elements[pathIndex]; if (element instanceof Key) { Key subKey = (Key) element; if (partitionId == null) { partitionId = subKey.getPartitionId(); } else if (!partitionId.equals(subKey.getPartitionId())) { throw new IllegalArgumentException("Partition IDs did not match, found: " + partitionId + " and " + subKey.getPartitionId()); } key.addAllPath(((Key) element).getPathList()); pathIndex--; } else { String kind; try { kind = (String) element; } catch (ClassCastException e) { throw new IllegalArgumentException("Expected string or Key, got: " + element.getClass()); } pathElement.setKind(kind); if (pathIndex + 1 < elements.length) { Object value = elements[pathIndex + 1]; if (value instanceof String) { pathElement.setName((String) value); } else if (value instanceof Long) { pathElement.setId((Long) value); } else if (value instanceof Integer) { pathElement.setId((Integer) value); } else if (value instanceof Short) { pathElement.setId((Short) value); } else { throw new IllegalArgumentException( "Expected string or integer, got: " + value.getClass()); } } key.addPath(pathElement); } } if (partitionId != null && !partitionId.equals(PartitionId.getDefaultInstance())) { key.setPartitionId(partitionId); } return key; } }
DatastoreHelper { public static Key.Builder makeKey(Object... elements) { Key.Builder key = Key.newBuilder(); PartitionId partitionId = null; for (int pathIndex = 0; pathIndex < elements.length; pathIndex += 2) { PathElement.Builder pathElement = PathElement.newBuilder(); Object element = elements[pathIndex]; if (element instanceof Key) { Key subKey = (Key) element; if (partitionId == null) { partitionId = subKey.getPartitionId(); } else if (!partitionId.equals(subKey.getPartitionId())) { throw new IllegalArgumentException("Partition IDs did not match, found: " + partitionId + " and " + subKey.getPartitionId()); } key.addAllPath(((Key) element).getPathList()); pathIndex--; } else { String kind; try { kind = (String) element; } catch (ClassCastException e) { throw new IllegalArgumentException("Expected string or Key, got: " + element.getClass()); } pathElement.setKind(kind); if (pathIndex + 1 < elements.length) { Object value = elements[pathIndex + 1]; if (value instanceof String) { pathElement.setName((String) value); } else if (value instanceof Long) { pathElement.setId((Long) value); } else if (value instanceof Integer) { pathElement.setId((Integer) value); } else if (value instanceof Short) { pathElement.setId((Short) value); } else { throw new IllegalArgumentException( "Expected string or integer, got: " + value.getClass()); } } key.addPath(pathElement); } } if (partitionId != null && !partitionId.equals(PartitionId.getDefaultInstance())) { key.setPartitionId(partitionId); } return key; } private  DatastoreHelper(); }
DatastoreHelper { public static Key.Builder makeKey(Object... elements) { Key.Builder key = Key.newBuilder(); PartitionId partitionId = null; for (int pathIndex = 0; pathIndex < elements.length; pathIndex += 2) { PathElement.Builder pathElement = PathElement.newBuilder(); Object element = elements[pathIndex]; if (element instanceof Key) { Key subKey = (Key) element; if (partitionId == null) { partitionId = subKey.getPartitionId(); } else if (!partitionId.equals(subKey.getPartitionId())) { throw new IllegalArgumentException("Partition IDs did not match, found: " + partitionId + " and " + subKey.getPartitionId()); } key.addAllPath(((Key) element).getPathList()); pathIndex--; } else { String kind; try { kind = (String) element; } catch (ClassCastException e) { throw new IllegalArgumentException("Expected string or Key, got: " + element.getClass()); } pathElement.setKind(kind); if (pathIndex + 1 < elements.length) { Object value = elements[pathIndex + 1]; if (value instanceof String) { pathElement.setName((String) value); } else if (value instanceof Long) { pathElement.setId((Long) value); } else if (value instanceof Integer) { pathElement.setId((Integer) value); } else if (value instanceof Short) { pathElement.setId((Short) value); } else { throw new IllegalArgumentException( "Expected string or integer, got: " + value.getClass()); } } key.addPath(pathElement); } } if (partitionId != null && !partitionId.equals(PartitionId.getDefaultInstance())) { key.setPartitionId(partitionId); } return key; } private  DatastoreHelper(); static Credential getServiceAccountCredential(String serviceAccountId,
      String privateKeyFile); static Credential getServiceAccountCredential(String serviceAccountId,
      String privateKeyFile, Collection<String> serviceAccountScopes); static Credential getServiceAccountCredential(String serviceAccountId,
      PrivateKey privateKey, Collection<String> serviceAccountScopes); static DatastoreOptions.Builder getOptionsFromEnv(); @Nullable static String getProjectIdFromComputeEngine(); static Datastore getDatastoreFromEnv(); static QuerySplitter getQuerySplitter(); static Comparator<Key> getKeyComparator(); static PropertyOrder.Builder makeOrder(String property,
      PropertyOrder.Direction direction); static Filter.Builder makeAncestorFilter(Key ancestor); static Filter.Builder makeFilter(String property, PropertyFilter.Operator operator,
      Value value); static Filter.Builder makeFilter(String property, PropertyFilter.Operator operator,
      Value.Builder value); static Filter.Builder makeAndFilter(Filter... subfilters); static Filter.Builder makeAndFilter(Iterable<Filter> subfilters); static PropertyReference.Builder makePropertyReference(String propertyName); static Value.Builder makeValue(Iterable<Value> values); static Value.Builder makeValue(Value value1, Value value2, Value... rest); static Value.Builder makeValue(Value.Builder value1, Value.Builder value2,
      Value.Builder... rest); static Value.Builder makeValue(Key key); static Value.Builder makeValue(Key.Builder key); static Value.Builder makeValue(long key); static Value.Builder makeValue(double value); static Value.Builder makeValue(boolean value); static Value.Builder makeValue(String value); static Value.Builder makeValue(Entity entity); static Value.Builder makeValue(Entity.Builder entity); static Value.Builder makeValue(ByteString blob); static Value.Builder makeValue(Date date); static Value.Builder makeValue(LatLng value); static Value.Builder makeValue(LatLng.Builder value); static Key.Builder makeKey(Object... elements); static double getDouble(Value value); static Key getKey(Value value); static ByteString getByteString(Value value); static Entity getEntity(Value value); static String getString(Value value); static boolean getBoolean(Value value); static long getLong(Value value); static long getTimestamp(Value value); static List<Value> getList(Value value); static Date toDate(Value value); static Mutation.Builder makeInsert(Entity entity); static Mutation.Builder makeUpdate(Entity entity); static Mutation.Builder makeUpsert(Entity entity); static Mutation.Builder makeDelete(Key key); }
DatastoreHelper { public static Key.Builder makeKey(Object... elements) { Key.Builder key = Key.newBuilder(); PartitionId partitionId = null; for (int pathIndex = 0; pathIndex < elements.length; pathIndex += 2) { PathElement.Builder pathElement = PathElement.newBuilder(); Object element = elements[pathIndex]; if (element instanceof Key) { Key subKey = (Key) element; if (partitionId == null) { partitionId = subKey.getPartitionId(); } else if (!partitionId.equals(subKey.getPartitionId())) { throw new IllegalArgumentException("Partition IDs did not match, found: " + partitionId + " and " + subKey.getPartitionId()); } key.addAllPath(((Key) element).getPathList()); pathIndex--; } else { String kind; try { kind = (String) element; } catch (ClassCastException e) { throw new IllegalArgumentException("Expected string or Key, got: " + element.getClass()); } pathElement.setKind(kind); if (pathIndex + 1 < elements.length) { Object value = elements[pathIndex + 1]; if (value instanceof String) { pathElement.setName((String) value); } else if (value instanceof Long) { pathElement.setId((Long) value); } else if (value instanceof Integer) { pathElement.setId((Integer) value); } else if (value instanceof Short) { pathElement.setId((Short) value); } else { throw new IllegalArgumentException( "Expected string or integer, got: " + value.getClass()); } } key.addPath(pathElement); } } if (partitionId != null && !partitionId.equals(PartitionId.getDefaultInstance())) { key.setPartitionId(partitionId); } return key; } private  DatastoreHelper(); static Credential getServiceAccountCredential(String serviceAccountId,
      String privateKeyFile); static Credential getServiceAccountCredential(String serviceAccountId,
      String privateKeyFile, Collection<String> serviceAccountScopes); static Credential getServiceAccountCredential(String serviceAccountId,
      PrivateKey privateKey, Collection<String> serviceAccountScopes); static DatastoreOptions.Builder getOptionsFromEnv(); @Nullable static String getProjectIdFromComputeEngine(); static Datastore getDatastoreFromEnv(); static QuerySplitter getQuerySplitter(); static Comparator<Key> getKeyComparator(); static PropertyOrder.Builder makeOrder(String property,
      PropertyOrder.Direction direction); static Filter.Builder makeAncestorFilter(Key ancestor); static Filter.Builder makeFilter(String property, PropertyFilter.Operator operator,
      Value value); static Filter.Builder makeFilter(String property, PropertyFilter.Operator operator,
      Value.Builder value); static Filter.Builder makeAndFilter(Filter... subfilters); static Filter.Builder makeAndFilter(Iterable<Filter> subfilters); static PropertyReference.Builder makePropertyReference(String propertyName); static Value.Builder makeValue(Iterable<Value> values); static Value.Builder makeValue(Value value1, Value value2, Value... rest); static Value.Builder makeValue(Value.Builder value1, Value.Builder value2,
      Value.Builder... rest); static Value.Builder makeValue(Key key); static Value.Builder makeValue(Key.Builder key); static Value.Builder makeValue(long key); static Value.Builder makeValue(double value); static Value.Builder makeValue(boolean value); static Value.Builder makeValue(String value); static Value.Builder makeValue(Entity entity); static Value.Builder makeValue(Entity.Builder entity); static Value.Builder makeValue(ByteString blob); static Value.Builder makeValue(Date date); static Value.Builder makeValue(LatLng value); static Value.Builder makeValue(LatLng.Builder value); static Key.Builder makeKey(Object... elements); static double getDouble(Value value); static Key getKey(Value value); static ByteString getByteString(Value value); static Entity getEntity(Value value); static String getString(Value value); static boolean getBoolean(Value value); static long getLong(Value value); static long getTimestamp(Value value); static List<Value> getList(Value value); static Date toDate(Value value); static Mutation.Builder makeInsert(Entity entity); static Mutation.Builder makeUpdate(Entity entity); static Mutation.Builder makeUpsert(Entity entity); static Mutation.Builder makeDelete(Key key); static final String SCATTER_PROPERTY_NAME; static final String KEY_PROPERTY_NAME; static final String PROJECT_ID_ENV_VAR; static final String LOCAL_HOST_ENV_VAR; static final String SERVICE_ACCOUNT_ENV_VAR; static final String PRIVATE_KEY_FILE_ENV_VAR; }