Search in sources :

Example 11 with Supplier

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;
    }
}
Also used : Field(java.lang.reflect.Field) Supplier(java.util.function.Supplier)

Example 12 with Supplier

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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) DisposableSupplier(org.glassfish.jersey.internal.inject.DisposableSupplier) RequestScope(org.glassfish.jersey.process.internal.RequestScope) Test(org.junit.Test)

Example 13 with Supplier

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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) DisposableSupplier(org.glassfish.jersey.internal.inject.DisposableSupplier) RequestScope(org.glassfish.jersey.process.internal.RequestScope) Test(org.junit.Test)

Example 14 with Supplier

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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) DisposableSupplier(org.glassfish.jersey.internal.inject.DisposableSupplier) RequestScope(org.glassfish.jersey.process.internal.RequestScope) Test(org.junit.Test)

Example 15 with Supplier

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;
}
Also used : Iterator(java.util.Iterator) Collection(java.util.Collection) MappableException(org.glassfish.jersey.server.internal.process.MappableException) Providers(org.glassfish.jersey.internal.inject.Providers) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) ArrayList(java.util.ArrayList) List(java.util.List) MessageBodyProviderNotFoundException(org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException) Parameterized(org.glassfish.jersey.server.model.Parameterized) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) Parameter(org.glassfish.jersey.server.model.Parameter) Collections(java.util.Collections) NotSupportedException(javax.ws.rs.NotSupportedException) ArrayList(java.util.ArrayList) Parameter(org.glassfish.jersey.server.model.Parameter)

Aggregations

Supplier (java.util.function.Supplier)81 List (java.util.List)29 Test (org.junit.Test)28 Map (java.util.Map)16 Collectors (java.util.stream.Collectors)15 Function (java.util.function.Function)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 Arrays (java.util.Arrays)11 Consumer (java.util.function.Consumer)11 Assert.assertEquals (org.junit.Assert.assertEquals)10 Optional (java.util.Optional)9 Mockito.mock (org.mockito.Mockito.mock)6 Collections (java.util.Collections)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Assert.assertFalse (org.junit.Assert.assertFalse)5 Matchers.any (org.mockito.Matchers.any)5 Mockito.when (org.mockito.Mockito.when)5 Maps (com.google.common.collect.Maps)4