Skip to content

Commit

Permalink
util-registry: Java-friendly API for Registry
Browse files Browse the repository at this point in the history
Problem
There is no Java-friendly API for `Registry.put`. The only way to
put a value into a registry from Java is to use the following pattern.

Solution
Use `@varargs` annotation to provide a Java-friendly version of `put`.

RB_ID=749845
  • Loading branch information
vkostyukov authored and jenkins committed Oct 5, 2015
1 parent 296e104 commit dad58d8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
3 changes: 2 additions & 1 deletion util-registry/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target(name='util-registry',

target(name='tests',
dependencies=[
'util/util-registry/src/test/scala'
'util/util-registry/src/test/scala',
'util/util-registry/src/test/java'
]
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.twitter.util.registry

import java.util.NoSuchElementException
import com.twitter.util.Local
import scala.annotation.varargs

/**
* This is an expert-level API; it is not meant for end-users.
Expand Down Expand Up @@ -36,10 +36,24 @@ trait Registry extends Iterable[Entry] {
def iterator: Iterator[Entry]

/**
* Registers a value in the registry, and returns the old value (if any).
* Registers a value in the registry, and returns the old value (if any).
*
* See `Registry.put(String*)` for the Java usage.
*/
def put(key: Seq[String], value: String): Option[String]

/**
* Registers a value (a non-empty sequence of strings) in the registry, and returns the old
* value (if any).
*
* Note: This is a Java-friendly version of `Registry.put(Seq[String, String])`.
*/
@varargs
def put(value: String*): Option[String] = {
require(value.nonEmpty)
put(value.init, value.last)
}

/**
* Removes a key from the registry, if it was registered, and returns the old value (if any).
*/
Expand Down
8 changes: 8 additions & 0 deletions util-registry/src/test/java/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
junit_tests(name='java',
dependencies=[
'3rdparty/jvm/org/scala-lang:scala-library',
'3rdparty/jvm/junit',
'util/util-registry/src/main/scala',
],
sources=rglobs('*.java')
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.twitter.util.registry;

import com.twitter.util.registry.GlobalRegistry;
import com.twitter.util.registry.Registry;
import org.junit.Test;

public class RegistryCompilationTest {

@Test
public void testPut() {
Registry registry = GlobalRegistry.get();
registry.put("foo", "bar", "baz");
registry.put("foo", "qux");
registry.put("baz");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class RegistryTest extends FunSuite {
val registry = mkRegistry()
registry.put(Seq("foo"), "bar")
registry.put(Seq("foo"), "baz")
assert((registry.toSet) == Set(Entry(Seq("foo"), "baz")))
assert(registry.toSet == Set(Entry(Seq("foo"), "baz")))
}

test(s"$name can return the old element when replacing") {
Expand All @@ -40,7 +40,7 @@ abstract class RegistryTest extends FunSuite {
val registry = mkRegistry()
registry.put(Seq("foo"), "bar")
assert(registry.remove(Seq("foo")) == Some("bar"))
assert((registry.toSet) == Set.empty)
assert(registry.toSet == Set.empty)
}

test(s"$name can remove nothing") {
Expand Down Expand Up @@ -94,4 +94,11 @@ abstract class RegistryTest extends FunSuite {
registry.put(Seq("foo", "baz"), "qux")
assert(registry.toSet == Set(Entry(Seq("foo"), "bar"), Entry(Seq("foo", "baz"), "qux")))
}

test(s"$name can support varargs API") {
val registry = mkRegistry()
registry.put("foo", "bar", "baz")
registry.put("qux")
assert(registry.toSet == Set(Entry(Seq("foo", "bar"), "baz"), Entry(Seq(), "qux")))
}
}

0 comments on commit dad58d8

Please sign in to comment.