Skip to content

Commit 86cbdeb

Browse files
author
Ajay Kannan
committed
---
yaml --- r: 1693 b: refs/heads/master c: 65f4f80 h: refs/heads/master i: 1691: 28724d1
1 parent 506e10e commit 86cbdeb

5 files changed

Lines changed: 45 additions & 4 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 8050cca81afee5f6a952cc7c544e3ae5aa616633
2+
refs/heads/master: 65f4f8063273b65b258aaf2be73e0609afb9583a
33
refs/heads/travis: e21ee7b88a5edc3f3d8c71f90c3fc32abf7e8dd6
44
refs/heads/gh-pages: d1b373c30c176edc08692348167bec3a244bb823
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3

trunk/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseKey.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ public String kind() {
157157
return leaf().kind();
158158
}
159159

160+
abstract BaseKey parent();
161+
160162
@Override
161163
public int hashCode() {
162164
return Objects.hash(projectId(), namespace(), path());

trunk/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/IncompleteKey.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ static IncompleteKey fromPb(DatastoreV1.Key keyPb) {
8484
return new IncompleteKey(projectId, namespace, path);
8585
}
8686

87+
/**
88+
* Returns the key's parent.
89+
*/
90+
@Override
91+
public Key parent() {
92+
List<PathElement> ancestors = ancestors();
93+
if (!ancestors.isEmpty()) {
94+
PathElement parent = ancestors.get(ancestors.size() - 1);
95+
Key.Builder keyBuilder;
96+
if (parent.hasName()) {
97+
keyBuilder = Key.builder(projectId(), parent.kind(), parent.name());
98+
} else {
99+
keyBuilder = Key.builder(projectId(), parent.kind(), parent.id());
100+
}
101+
return keyBuilder.ancestors(ancestors.subList(0, ancestors.size() - 1)).build();
102+
}
103+
return null;
104+
}
105+
87106
public static Builder builder(String projectId, String kind) {
88107
return new Builder(projectId, kind);
89108
}

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/BaseKeyTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ protected BaseKey build() {
5050
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
5151
return null;
5252
}
53+
54+
@Override
55+
protected BaseKey parent() {
56+
return null;
57+
}
5358
};
5459
}
5560
}

trunk/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/IncompleteKeyTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,30 @@
1717
package com.google.gcloud.datastore;
1818

1919
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNull;
2021
import static org.junit.Assert.assertTrue;
2122

23+
import org.junit.Before;
2224
import org.junit.Test;
2325

2426
public class IncompleteKeyTest {
2527

28+
private static IncompleteKey pk1, pk2;
29+
private static Key parent;
30+
31+
@Before
32+
public void setUp() {
33+
pk1 = IncompleteKey.builder("ds", "kind1").build();
34+
parent = Key.builder("ds", "kind2", 10).build();
35+
pk2 = IncompleteKey.builder(parent, "kind3").build();
36+
}
37+
2638
@Test
2739
public void testBuilders() throws Exception {
28-
IncompleteKey pk1 = IncompleteKey.builder("ds", "kind1").build();
2940
assertEquals("ds", pk1.projectId());
3041
assertEquals("kind1", pk1.kind());
3142
assertTrue(pk1.ancestors().isEmpty());
3243

33-
Key parent = Key.builder("ds", "kind2", 10).build();
34-
IncompleteKey pk2 = IncompleteKey.builder(parent, "kind3").build();
3544
assertEquals("ds", pk2.projectId());
3645
assertEquals("kind3", pk2.kind());
3746
assertEquals(parent.path(), pk2.ancestors());
@@ -42,4 +51,10 @@ public void testBuilders() throws Exception {
4251
assertEquals("kind4", pk3.kind());
4352
assertEquals(parent.path(), pk3.ancestors());
4453
}
54+
55+
@Test
56+
public void testParent() {
57+
assertNull(pk1.parent());
58+
assertEquals(parent, pk2.parent());
59+
}
4560
}

0 commit comments

Comments
 (0)