/** Returns the key's ID (as {@link Long}) or name (as {@link String}). Never {@code null}. */ public Object getNameOrId() { return getLeaf().getNameOrId(); }
@Test public void testNameOrId() throws Exception { assertNull(PE_1.getNameOrId()); assertEquals("n", PE_2.getNameOrId()); assertEquals(Long.valueOf(1), PE_3.getNameOrId()); } }
static IncompleteKey fromPb(com.google.datastore.v1.Key keyPb) { String projectId = ""; String namespace = ""; if (keyPb.hasPartitionId()) { com.google.datastore.v1.PartitionId partitionIdPb = keyPb.getPartitionId(); projectId = partitionIdPb.getProjectId(); namespace = partitionIdPb.getNamespaceId(); } List<com.google.datastore.v1.Key.PathElement> pathElementsPb = keyPb.getPathList(); Preconditions.checkArgument(!pathElementsPb.isEmpty(), "Path must not be empty"); ImmutableList.Builder<PathElement> pathBuilder = ImmutableList.builder(); for (com.google.datastore.v1.Key.PathElement pathElementPb : pathElementsPb) { pathBuilder.add(PathElement.fromPb(pathElementPb)); } ImmutableList<PathElement> path = pathBuilder.build(); PathElement leaf = path.get(path.size() - 1); if (leaf.getNameOrId() != null) { return new Key(projectId, namespace, path); } return new IncompleteKey(projectId, namespace, path); }
private int comparePathElements(final PathElement here, final PathElement there) { final int result = here.getKind().compareTo(there.getKind()); if (result != 0) { return result; } else if (here.getNameOrId() == null && there.getNameOrId() == null) { return compareToWithIdentityHash(here, there); } else if (here.hasId()) { return there.hasId() ? Long.compare(here.getId(), there.getId()) : -1; } else { return there.hasId() ? 1 : here.getName().compareTo(there.getName()); } }
/** Returns the key's ID (as {@link Long}) or name (as {@link String}). Never {@code null}. */ public Object getNameOrId() { return getLeaf().getNameOrId(); }
static IncompleteKey fromPb(com.google.datastore.v1.Key keyPb) { String projectId = ""; String namespace = ""; if (keyPb.hasPartitionId()) { com.google.datastore.v1.PartitionId partitionIdPb = keyPb.getPartitionId(); projectId = partitionIdPb.getProjectId(); namespace = partitionIdPb.getNamespaceId(); } List<com.google.datastore.v1.Key.PathElement> pathElementsPb = keyPb.getPathList(); Preconditions.checkArgument(!pathElementsPb.isEmpty(), "Path must not be empty"); ImmutableList.Builder<PathElement> pathBuilder = ImmutableList.builder(); for (com.google.datastore.v1.Key.PathElement pathElementPb : pathElementsPb) { pathBuilder.add(PathElement.fromPb(pathElementPb)); } ImmutableList<PathElement> path = pathBuilder.build(); PathElement leaf = path.get(path.size() - 1); if (leaf.getNameOrId() != null) { return new Key(projectId, namespace, path); } return new IncompleteKey(projectId, namespace, path); }