use of java.util.function.Supplier in project java-design-patterns by iluwatar.
the class Java8HolderTest method getInternalHeavyValue.
@Override
Heavy getInternalHeavyValue() throws Exception {
final Field holderField = Java8Holder.class.getDeclaredField("heavy");
holderField.setAccessible(true);
final Supplier<Heavy> supplier = (Supplier<Heavy>) holderField.get(this.holder);
final Class<? extends Supplier> supplierClass = supplier.getClass();
// The lazy holder is at first a lambda, but gets replaced with a new supplier after loading ...
if (supplierClass.isLocalClass()) {
final Field instanceField = supplierClass.getDeclaredField("heavyInstance");
instanceField.setAccessible(true);
return (Heavy) instanceField.get(supplier);
} else {
return null;
}
}
use of java.util.function.Supplier in project jersey by jersey.
the class DisposableSupplierTest method testDisposeSingletonSupplierMultiRequestScoped.
/**
* Tests that inherited request scoped is also cleaned by disposing the objects.
*/
@Test
public void testDisposeSingletonSupplierMultiRequestScoped() {
BindingTestHelper.bind(injectionManager, binder -> {
binder.bindFactory(DisposableSupplierImpl.class).to(String.class).in(RequestScoped.class);
binder.bind(new RequestScope()).to(RequestScope.class);
});
RequestScope request = injectionManager.getInstance(RequestScope.class);
AtomicReference<Supplier<String>> firstSupplier = new AtomicReference<>();
AtomicReference<Supplier<String>> secondSupplier = new AtomicReference<>();
request.runInScope(() -> {
Supplier<String> supplier1 = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
firstSupplier.set(supplier1);
Object instance1 = injectionManager.getInstance(String.class);
assertEquals("1", instance1);
request.runInScope(() -> {
// Save Singleton Supplier for later check that the instance was disposed.
Supplier<String> supplier2 = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
secondSupplier.set(supplier2);
Object instance2 = injectionManager.getInstance(String.class);
// 1-2 because the same static class is used in inherited runInScope
assertEquals("1", instance2);
});
});
Supplier<String> cleanedSupplier1 = firstSupplier.get();
Supplier<String> cleanedSupplier2 = secondSupplier.get();
// Next should be 1-1
assertEquals("1", cleanedSupplier1.get());
// 1-2 because the same static class is used but the instance is cleaned.
assertEquals("1", cleanedSupplier2.get());
}
use of java.util.function.Supplier in project jersey by jersey.
the class DisposableSupplierTest method testDisposeSingletonSupplierRequestScopedInstance.
@Test
public void testDisposeSingletonSupplierRequestScopedInstance() {
BindingTestHelper.bind(injectionManager, binder -> {
binder.bindFactory(DisposableSupplierImpl.class, Singleton.class).to(String.class).in(RequestScoped.class);
binder.bind(new RequestScope()).to(RequestScope.class);
});
RequestScope request = injectionManager.getInstance(RequestScope.class);
AtomicReference<Supplier<String>> atomicSupplier = new AtomicReference<>();
request.runInScope(() -> {
// Save Singleton Supplier for later check that the instance was disposed.
Supplier<String> supplier = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
atomicSupplier.set(supplier);
// All instances should be the same because they are request scoped.
Object instance1 = injectionManager.getInstance(String.class);
assertEquals("1", instance1);
Object instance2 = injectionManager.getInstance(String.class);
assertEquals("1", instance2);
});
Supplier<String> cleanedSupplier = atomicSupplier.get();
// Next should be 1-1
assertEquals("1", cleanedSupplier.get());
}
use of java.util.function.Supplier in project jersey by jersey.
the class DisposableSupplierTest method testDisposePerLookupSupplierRequestScopedInstance.
/**
* Tests that object created in request scope is disposing at the time of ending the scope.
*/
@Test
public void testDisposePerLookupSupplierRequestScopedInstance() {
BindingTestHelper.bind(injectionManager, binder -> {
binder.bindFactory(DisposableSupplierImpl.class).to(String.class).in(RequestScoped.class);
binder.bind(new RequestScope()).to(RequestScope.class);
});
RequestScope request = injectionManager.getInstance(RequestScope.class);
AtomicReference<Supplier<String>> atomicSupplier = new AtomicReference<>();
request.runInScope(() -> {
// Save Singleton Supplier for later check that the instance was disposed.
Supplier<String> supplier = injectionManager.getInstance(DISPOSABLE_SUPPLIER_TYPE);
atomicSupplier.set(supplier);
// All instances should be the same because they are request scoped.
Object instance1 = injectionManager.getInstance(String.class);
assertEquals("1", instance1);
Object instance2 = injectionManager.getInstance(String.class);
assertEquals("1", instance2);
});
Supplier<String> cleanedSupplier = atomicSupplier.get();
// Next should be 1
assertEquals("1", cleanedSupplier.get());
}
use of java.util.function.Supplier in project jersey by jersey.
the class ParameterValueHelper method createValueProviders.
/**
* Create list of parameter value providers for the given {@link Parameterized
* parameterized} resource model component.
*
* @param injectionManager injection manager.
* @param parameterized parameterized resource model component.
* @return list of parameter value providers for the parameterized component.
*/
public static List<ParamValueFactoryWithSource<?>> createValueProviders(InjectionManager injectionManager, Parameterized parameterized) {
if ((null == parameterized.getParameters()) || (0 == parameterized.getParameters().size())) {
return Collections.emptyList();
}
List<ValueSupplierProvider> valueSupplierProviders = Providers.getProviders(injectionManager, ValueSupplierProvider.class).stream().sorted((o1, o2) -> o2.getPriority().getWeight() - o1.getPriority().getWeight()).collect(Collectors.toList());
boolean entityParamFound = false;
final List<ParamValueFactoryWithSource<?>> providers = new ArrayList<>(parameterized.getParameters().size());
for (final Parameter parameter : parameterized.getParameters()) {
final Parameter.Source parameterSource = parameter.getSource();
entityParamFound = entityParamFound || Parameter.Source.ENTITY == parameterSource;
final Supplier<?> valueSupplier = getParamValueSupplier(valueSupplierProviders, parameter);
if (valueSupplier != null) {
providers.add(wrapParamValueSupplier(valueSupplier, parameterSource));
} else {
providers.add(null);
}
}
if (!entityParamFound && Collections.frequency(providers, null) == 1) {
// Try to find entity if there is one unresolved parameter and the annotations are unknown
final int entityParamIndex = providers.lastIndexOf(null);
final Parameter parameter = parameterized.getParameters().get(entityParamIndex);
if (Parameter.Source.UNKNOWN == parameter.getSource() && !parameter.isQualified()) {
final Parameter overriddenParameter = Parameter.overrideSource(parameter, Parameter.Source.ENTITY);
final Supplier<?> valueSupplier = getParamValueSupplier(valueSupplierProviders, overriddenParameter);
if (valueSupplier != null) {
providers.set(entityParamIndex, wrapParamValueSupplier(valueSupplier, overriddenParameter.getSource()));
} else {
providers.set(entityParamIndex, null);
}
}
}
return providers;
}
Aggregations