Java Code Examples for java.util.function.Supplier.get()

The following are Jave code examples for showing how to use get() of the java.util.function.Supplier class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: vertx-zero   File: Deliver.java   View Source Code Vote up 6 votes
/**
 * @param supplier
 * @param runCls
 * @param <T>
 * @return
 */
static <T> T execRun(final Supplier<T> supplier,
                     final Class<? extends ZeroRunException> runCls,
                     final Object... args) {
    T ret = null;
    try {
        ret = supplier.get();
    } catch (final Throwable ex) {
        final Object[] argument = ArrayUtil.add(args, ex);
        final ZeroRunException error = Instance.instance(
                runCls, argument);
        if (null != error) {
            throw error;
        }
    }
    return ret;
}
 
Example 2
Project: Java-EX   File: CacheUtil.java   View Source Code Vote up 5 votes
public static <V> V cacheWeak(Object owner, Object key, Supplier<V> factory) {
  WeakReference<V> ref = cache(owner, key, () -> new WeakReference<>(factory.get()));
  V v = ref.get();
  if (v == null) {
    v = factory.get();
    set(owner, key, new WeakReference<>(v));
  }
  return v;
}
 
Example 3
Project: trellis   File: IdServiceTest.java   View Source Code Vote up 5 votes
@Test
public void testSupplier() {
    final String prefix = "trellis:repository/";
    final Supplier<String> supplier = new UUIDGenerator().getSupplier(prefix);
    final String id1 = supplier.get();
    final String id2 = supplier.get();

    assertTrue(id1.startsWith(prefix));
    assertTrue(id2.startsWith(prefix));
    assertFalse(id1.equals(id2));
}
 
Example 4
Project: ForgeHax   File: Options.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
protected Options(Map<String, Object> data) throws CommandBuildException {
    super(data);
    try {
        Supplier<Collection<E>> supplier =  (Supplier<Collection<E>>)   data.get(SUPPLIER);
        Objects.requireNonNull(supplier, "Missing supplier");

        this.contents = supplier.get();
        this.factory =                      (Function<String, E>)       data.get(FACTORY);
    } catch (Throwable t) {
        throw new CommandBuildException("Failed to build options", t);
    }
}
 
Example 5
Project: bench   File: ResourceManagerAgentTest.java   View Source Code Vote up 5 votes
@Theory
public void agent_registration_complete(final Supplier supplier) throws Exception {
    BenchRule benchRule = (BenchRule) supplier.get();
    before(benchRule);

    assertThat(agentRegistry.all().size(), is(1));
    RegisteredAgent registeredAgent = agentRegistry.all().iterator().next();
    assertThat(registeredAgent.getAgentKey(), is(agent.getKey()));
    assertTrue(registeredAgent.getCreationTime() > 0);
    assertNotNull(registeredAgent.getSystemConfig());
    after(benchRule);
}
 
Example 6
Project: Randores2   File: RandoresItemArmor.java   View Source Code Vote up 5 votes
protected <T> T delegate(ItemStack stack, Function<ItemArmor, T> action, Supplier<T> def) {
    if (stack != null) {
        return this.delegate(new RandoresItemData(stack), action, def);
    } else {
        return def.get();
    }
}
 
Example 7
Project: bench   File: ResourceManagerAgentTest.java   View Source Code Vote up 5 votes
@Theory
public void create_and_close_embedded_actor_on_agent(final Supplier supplier) throws Exception {
    BenchRule benchRule = (BenchRule) supplier.get();
    before(benchRule);

    List<String> preferredHosts = new ArrayList<>();
    ActorSync sync = createActorWith(preferredHosts);
    sync.assertActorCreated();

    resourceManager.closeActor(DUMMY_ACTOR);

    sync.assertActorClosed();
    assertThat(actorRegistry.all().size(), is(0));
    after(benchRule);
}
 
Example 8
Project: MooProject   File: CommandContext.java   View Source Code Vote up 5 votes
/**
 * Gets a value stored before by key
 *
 * @param key      The key
 * @param supplier The supplier if the value does not exist
 * @return The value
 */
public <V> V get(String key, Supplier<V> supplier) {
    Object o = CONTEXT_CACHE.get(getSendersUniqueId(), getCommand().getLabel() + ":" + key);
    if(o == null && supplier != null) {
        o = supplier.get();
        this.set(o);
    }
    return (V) o;
}
 
Example 9
Project: yaess   File: Contract.java   View Source Code Vote up 5 votes
public static <T extends ContractBrokenException, E> Collection<E> notNullOrEmpty(Collection<E> collection, Supplier<T> exceptionSupplier) {

		if (isEmpty(collection)) {
			throw exceptionSupplier.get();
		}
		return collection;
	}
 
Example 10
Project: queries   File: CaffeineCacheWrapper.java   View Source Code Vote up 5 votes
@Override
public ParsedQueries getParsedQueries(SourceId sourceId, Supplier<ParsedQueries> queriesSupplier) {
	if (parsedCache == null) {
		return queriesSupplier.get();
	} else {
		return parsedCache.get(sourceId, (k) -> queriesSupplier.get());
	}
}
 
Example 11
Project: Genesis   File: GenesisCreativeTabs.java   View Source Code Vote up 5 votes
private static CreativeTabs createTabWithStackIcon(final String label, final Supplier<ItemStack> iconSupplier) {
    return new CreativeTabs(GenesisMod.MOD_ID + "." + label) {
        @SideOnly(Side.CLIENT)
        @Override
        public ItemStack getTabIconItem() {
            return iconSupplier.get();
        }
    };
}
 
Example 12
Project: morpheus-core   File: PrinterOfPeriod.java   View Source Code Vote up 5 votes
@Override
public final String apply(Period input) {
    if (input == null) {
        final Supplier<String> nullValue = getNullValue();
        return nullValue != null ? nullValue.get() : "null";
    } else {
        final int days = input.getDays();
        return days + "D";
    }
}
 
Example 13
Project: jdk8u-jdk   File: SpliteratorLateBindingFailFastTest.java   View Source Code Vote up 5 votes
@Test(dataProvider = "Source")
public <T> void lateBindingTestWithForEach(String description, Supplier<Source<T>> ss) {
    Source<T> source = ss.get();
    Collection<T> c = source.asCollection();
    Spliterator<T> s = c.spliterator();

    source.update();

    Set<T> r = new HashSet<>();
    s.forEachRemaining(r::add);

    assertEquals(r, new HashSet<>(c));
}
 
Example 14
Project: morpheus-core   File: ArrayStatsTests.java   View Source Code Vote up 5 votes
@Test(dataProvider = "styles")
public void testCumSumOfDoublesWithNans(ArrayStyle style) {
    final Random random = new Random();
    final Supplier<Array<Double>> factory = () -> {
        switch (style) {
            case DENSE:     return Array.of(Double.class, 10000).applyDoubles(v -> random.nextDouble() * 10d);
            case SPARSE:    return Array.of(Double.class, 10000, 0.8f).applyDoubles(v -> random.nextDouble() * 10d);
            case MAPPED:    return Array.map(Double.class, 10000).applyDoubles(v -> random.nextDouble() * 10d);
            default:    throw new IllegalArgumentException("Unsupported style: " + style);
        }
    };
    final Array<Double> source = factory.get();
    source.setDouble(0, Double.NaN);
    source.setDouble(25, Double.NaN);
    final Array<Double> cumSum = source.cumSum();
    for (int i=0; i<source.length(); ++i) {
        final double actual = cumSum.getDouble(i);
        final double expected = source.stats(0, i+1).sum().doubleValue();
        if (i == 0) {
            Assert.assertTrue(Double.isNaN(actual));
        } else if (i == 25) {
            final double prior = source.stats(0, i).sum().doubleValue();
            Assert.assertEquals(actual, prior, "Values match at index " + i);
        } else {
            Assert.assertEquals(actual, expected, "Values match at index " + i);
        }
    }
}
 
Example 15
Project: ARCLib   File: Utils.java   View Source Code Vote up 4 votes
public static <U, T extends RuntimeException> void ne(U o1,  U o2, Supplier<T> supplier) {
    if (Objects.equals(o1, o2)) {
        throw supplier.get();
    }
}
 
Example 16
Project: xcalendar   File: SwtUtils.java   View Source Code Vote up 4 votes
/**
 * Dispatch
 */
public static void dispatch(final Supplier<Boolean> condition) {
	final Display display = Display.getCurrent();
	while (!condition.get()) if(!display.readAndDispatch()) display.sleep(); 
}
 
Example 17
Project: jdk8u-jdk   File: ExtendsAbstractSet.java   View Source Code Vote up 4 votes
protected ExtendsAbstractSet(Supplier<Set<E>> backer) {
    this.set = backer.get();
}
 
Example 18
Project: Java-9-Cookbook   File: Chapter04Functional.java   View Source Code Vote up 4 votes
public double doSomething1(Supplier<Double> supp, String str, int i){
    return supp.get() * i + str.length();
}
 
Example 19
Project: power-jambda   File: OptionalUtils.java   View Source Code Vote up 3 votes
/**
 * Returns the {@code optional} instance
 * if a value is present in it; supplier.get() otherwise.<br>
 * <br>
 * As of Java 9 you can do simply<br>
 * <pre>{@code
 *     firstOptional.or(() -> secondOptional);
 * }</pre>
 *
 * @param <T> The class of the value
 * @param optional An Optional to check
 * @param supplier A Supplier whose result is returned if no value is present
 * @return An {@code Optional} with the value present
 */
public static <T> Optional<T> or(Optional<T> optional, Supplier<Optional<T>> supplier) {
    Optional<T> res;
    if (optional.isPresent()) {
        res = optional;
    } else {
        res = supplier.get();
    }
    return res;
}
 
Example 20
Project: yunikov-commons   File: TypedMaybe.java   View Source Code Vote up 2 votes
/**
 * Gets the value if not empty, otherwise executes the provided function.
 *
 * @param other other function
 * @return not empty value or performs other function
 */
public T getOrElse(final Supplier<? extends T> other) {
    return !empty() ? value : other.get();
}