Skip to content

Commit 24d1afa

Browse files
authored
Add methods to ResourceName interface (googleapis#46)
1 parent 3266377 commit 24d1afa

4 files changed

Lines changed: 173 additions & 2 deletions

File tree

‎api-common-java/src/main/java/com/google/api/resourcenames/ResourceName.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,20 @@
3232
package com.google.api.resourcenames;
3333

3434
import com.google.api.core.BetaApi;
35+
import java.util.Map;
3536

3637
/** An interface that generated resource name types must implement. */
3738
@BetaApi
38-
public interface ResourceName {}
39+
public interface ResourceName {
40+
41+
/**
42+
* Return the map of each field name to its value.
43+
*/
44+
Map<String, String> getFieldValuesMap();
45+
46+
/**
47+
* Return the String value of the field with name fieldName. Returns null if the fieldName was not
48+
* found.
49+
*/
50+
String getFieldValue(String fieldName);
51+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2018, Google Inc.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following disclaimer
13+
* in the documentation and/or other materials provided with the
14+
* distribution.
15+
* * Neither the name of Google Inc. nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.google.api.resourcenames;
32+
33+
import com.google.api.core.BetaApi;
34+
35+
@BetaApi
36+
public interface ResourceNameFactory<T extends ResourceName> {
37+
38+
/* Create a new ResourceName from a formatted String representing a ResourceName. */
39+
T parse(String formattedString);
40+
}

‎api-common-java/src/main/java/com/google/api/resourcenames/UntypedResourceName.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
import com.google.api.core.BetaApi;
3535
import com.google.common.base.Preconditions;
36+
import com.google.common.collect.ImmutableMap;
37+
import java.util.Map;
3638

3739
/**
3840
* A class to represent a {@link ResourceName} with an unknown format. This class in intended to
@@ -44,6 +46,8 @@ public class UntypedResourceName implements ResourceName {
4446

4547
private final String rawValue;
4648

49+
private volatile Map<String, String> fieldValuesMap;
50+
4751
private UntypedResourceName(String rawValue) {
4852
this.rawValue = Preconditions.checkNotNull(rawValue);
4953
}
@@ -57,7 +61,26 @@ public static UntypedResourceName parse(String formattedString) {
5761
}
5862

5963
public static boolean isParsableFrom(String formattedString) {
60-
return true;
64+
return formattedString != null;
65+
}
66+
67+
/* Returns a map with an empty String "" as the sole key, which maps to the raw value of this ResourceName. */
68+
@Override
69+
public Map<String, String> getFieldValuesMap() {
70+
if (fieldValuesMap == null) {
71+
synchronized (this) {
72+
if (fieldValuesMap == null) {
73+
fieldValuesMap = ImmutableMap.of("", rawValue);
74+
}
75+
}
76+
}
77+
return fieldValuesMap;
78+
}
79+
80+
/* Returns the raw value of this ResourceName iff fieldName.equals(""), else returns null. */
81+
@Override
82+
public String getFieldValue(String fieldName) {
83+
return getFieldValuesMap().get("");
6184
}
6285

6386
@Override
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2018, Google Inc.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following disclaimer
13+
* in the documentation and/or other materials provided with the
14+
* distribution.
15+
* * Neither the name of Google Inc. nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
package com.google.api.resourcenames;
32+
33+
import static junit.framework.TestCase.fail;
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertTrue;
37+
38+
import java.util.Map;
39+
import org.junit.Test;
40+
import org.junit.runner.RunWith;
41+
import org.junit.runners.JUnit4;
42+
43+
/**
44+
* Tests for {@link UntypedResourceNameTest}.
45+
*/
46+
@RunWith(JUnit4.class)
47+
public class UntypedResourceNameTest {
48+
private static final String NAME_STRING = "sunshine";
49+
private static final String EMPTY_STRING = "";
50+
51+
@Test
52+
public void testGetFieldValues() {
53+
assertTrue(UntypedResourceName.isParsableFrom(NAME_STRING));
54+
UntypedResourceName fooName = UntypedResourceName.parse(NAME_STRING);
55+
56+
Map<String, String> fieldValuesMap = fooName.getFieldValuesMap();
57+
assertTrue(fieldValuesMap.containsKey(EMPTY_STRING));
58+
assertEquals(NAME_STRING, fieldValuesMap.get(EMPTY_STRING));
59+
assertEquals(1, fieldValuesMap.size());
60+
assertEquals(null, fieldValuesMap.get(NAME_STRING));
61+
}
62+
63+
@Test
64+
public void testInsertIntoFieldValuesMap() {
65+
UntypedResourceName fooName = UntypedResourceName.parse(NAME_STRING);
66+
Map<String, String> fieldValuesMap = fooName.getFieldValuesMap();
67+
68+
try {
69+
fieldValuesMap.put(EMPTY_STRING, "foo");
70+
fail("fieldValuesMap should prevent insertion into internal map. ");
71+
} catch (UnsupportedOperationException e) {
72+
}
73+
74+
try {
75+
fieldValuesMap.put(null, "foo");
76+
fail("fieldValuesMap should prevent insertion into internal map. ");
77+
} catch (UnsupportedOperationException e) {
78+
}
79+
80+
try {
81+
fieldValuesMap.put(NAME_STRING, NAME_STRING);
82+
fail("fieldValuesMap should prevent insertion into internal map. ");
83+
} catch (UnsupportedOperationException e) {
84+
}
85+
}
86+
87+
@Test
88+
public void testNullName() {
89+
assertFalse(UntypedResourceName.isParsableFrom(null));
90+
try {
91+
UntypedResourceName fooName = UntypedResourceName.parse(null);
92+
} catch (NullPointerException e) {
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)