下面列出了java.util.Hashtable#putIfAbsent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected ServiceRegistration<?> registerExtension(
String name, Object... keyValues) {
TestFilter testFilter = new TestFilter();
Hashtable<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_EXTENSION, true);
properties.put(JAX_RS_NAME, name);
properties.putIfAbsent(
JAX_RS_APPLICATION_SELECT, "(osgi.jaxrs.name=*)");
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
}
ServiceRegistration<ContainerResponseFilter> serviceRegistration =
bundleContext.registerService(
ContainerResponseFilter.class, testFilter, properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
protected <T> ServiceRegistration<T> registerExtension(
Class<T> clazz, T extension, String name, Object... keyValues) {
Hashtable<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_EXTENSION, true);
properties.put(JAX_RS_NAME, name);
properties.putIfAbsent(
JAX_RS_APPLICATION_SELECT, "(osgi.jaxrs.name=*)");
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
}
ServiceRegistration<T> serviceRegistration =
bundleContext.registerService(clazz, extension, properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
protected ServiceRegistration<?> registerInvalidExtension(
String name, Object... keyValues) {
TestFilter testFilter = new TestFilter();
Hashtable<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_EXTENSION, true);
properties.put(JAX_RS_NAME, name);
properties.putIfAbsent(
JAX_RS_APPLICATION_SELECT, "(osgi.jaxrs.name=*)");
for (int i = 0; i < keyValues.length; i = i + 2) {
properties.put(keyValues[i].toString(), keyValues[i + 1]);
}
ServiceRegistration<Object> serviceRegistration =
bundleContext.registerService(
Object.class, testFilter, properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
protected ServiceRegistration<?> registerMultiExtension(
String name, String... classes) {
Hashtable<String, Object> properties = new Hashtable<>();
properties.put(JAX_RS_EXTENSION, true);
properties.put(JAX_RS_NAME, name);
properties.putIfAbsent(
JAX_RS_APPLICATION_SELECT, "(osgi.jaxrs.name=*)");
ServiceRegistration<?> serviceRegistration =
bundleContext.registerService(
classes, new TestFilterAndExceptionMapper(), properties);
_registrations.add(serviceRegistration);
return serviceRegistration;
}
@Test
public void whenPutifAbsent_thenNotRewritten() {
Hashtable<Word, String> table = new Hashtable<Word, String>();
table.put(new Word("cat"), "a small domesticated carnivorous mammal");
String definition = "an animal";
// old way
/* if (!table.containsKey(new Word("cat"))) {
table.put(new Word("cat"), definition);
}*/
// new way
table.putIfAbsent(new Word("cat"), definition);
assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
}