1 package com.atlassian.util.concurrent;
2
3 /**
4 * Useful {@link Supplier} implementations.
5 */
6 public final class Suppliers {
7 /**
8 * A {@link Supplier} that always returns the supplied source.
9 *
10 * @param <T> the type
11 * @param source the object that is always returned.
12 * @return a supplier that always returns the supplied argument
13 */
14 public static <T> Supplier<T> memoize(final T source) {
15 return new Supplier<T>() {
16 public T get() {
17 return source;
18 }
19 };
20 }
21
22 /**
23 * A {@link Supplier} that asks the argument function for the result using
24 * the input argument.
25 *
26 * @param <D> the input type
27 * @param <T> the result type
28 * @param input used as the argument when calling the function.
29 * @param function asked to get the result.
30 * @return the result
31 */
32 public static <D, T> Supplier<T> fromFunction(final D input, final Function<D, T> function) {
33 return new Supplier<T>() {
34 public T get() {
35 return function.get(input);
36 }
37 };
38 }
39
40 // /CLOVER:OFF
41 private Suppliers() {
42 throw new AssertionError("cannot instantiate!");
43 }
44 // /CLOVER:ON
45 }