aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJake Wharton <jw@squareup.com>2015-12-14 18:07:09 -0500
committerJake Wharton <jw@squareup.com>2015-12-14 18:08:01 -0500
commit633e4ac4bb8841c8f9dc5ef95548a9ca7939e535 (patch)
tree439c1e033b5da2a7619af2db33ba0eaca72de0b2
parent251cb9a9797dabfe873ae6901fdabe8ea7c052cf (diff)
downloadjavapoet-633e4ac4bb8841c8f9dc5ef95548a9ca7939e535.tar.gz
Overload for creating a one-off name without a tag.
-rw-r--r--src/main/java/com/squareup/javapoet/NameAllocator.java15
-rw-r--r--src/test/java/com/squareup/javapoet/NameAllocatorTest.java7
2 files changed, 22 insertions, 0 deletions
diff --git a/src/main/java/com/squareup/javapoet/NameAllocator.java b/src/main/java/com/squareup/javapoet/NameAllocator.java
index bda3137..961cba5 100644
--- a/src/main/java/com/squareup/javapoet/NameAllocator.java
+++ b/src/main/java/com/squareup/javapoet/NameAllocator.java
@@ -19,6 +19,7 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
+import java.util.UUID;
import javax.lang.model.SourceVersion;
import static com.squareup.javapoet.Util.checkNotNull;
@@ -91,6 +92,19 @@ public final class NameAllocator implements Cloneable {
this.tagToName = tagToName;
}
+ /**
+ * Return a new name using {@code suggestion} that will not be a Java identifier or clash with
+ * other names.
+ */
+ public String newName(String suggestion) {
+ return newName(suggestion, UUID.randomUUID().toString());
+ }
+
+ /**
+ * Return a new name using {@code suggestion} that will not be a Java identifier or clash with
+ * other names. The returned value can be queried multiple times by passing {@code tag} to
+ * {@link #get(Object)}.
+ */
public String newName(String suggestion, Object tag) {
checkNotNull(suggestion, "suggestion");
checkNotNull(tag, "tag");
@@ -128,6 +142,7 @@ public final class NameAllocator implements Cloneable {
return result.toString();
}
+ /** Retrieve a name created with {@link #newName(String, Object)}. */
public String get(Object tag) {
String result = tagToName.get(tag);
if (result == null) {
diff --git a/src/test/java/com/squareup/javapoet/NameAllocatorTest.java b/src/test/java/com/squareup/javapoet/NameAllocatorTest.java
index 4297cdd..7fdfbc9 100644
--- a/src/test/java/com/squareup/javapoet/NameAllocatorTest.java
+++ b/src/test/java/com/squareup/javapoet/NameAllocatorTest.java
@@ -31,6 +31,13 @@ public final class NameAllocatorTest {
@Test public void nameCollision() throws Exception {
NameAllocator nameAllocator = new NameAllocator();
+ assertThat(nameAllocator.newName("foo")).isEqualTo("foo");
+ assertThat(nameAllocator.newName("foo")).isEqualTo("foo_");
+ assertThat(nameAllocator.newName("foo")).isEqualTo("foo__");
+ }
+
+ @Test public void nameCollisionWithTag() throws Exception {
+ NameAllocator nameAllocator = new NameAllocator();
assertThat(nameAllocator.newName("foo", 1)).isEqualTo("foo");
assertThat(nameAllocator.newName("foo", 2)).isEqualTo("foo_");
assertThat(nameAllocator.newName("foo", 3)).isEqualTo("foo__");