View Javadoc

1   package com.atlassian.util.concurrent;
2   
3   import static com.atlassian.util.concurrent.Assertions.notNull;
4   
5   public final class Functions {
6       /**
7        * Get a function that uses the Supplier as a factory for all inputs.
8        * 
9        * @param <D> the key type, ignored
10       * @param <R> the result type
11       * @param supplier called for all inputs
12       * @return the function
13       */
14      public static <D, R> Function<D, R> fromSupplier(final @NotNull Supplier<R> supplier) {
15          return new FromSupplier<D, R>(supplier);
16      }
17  
18      private static class FromSupplier<D, R> implements Function<D, R> {
19          private final Supplier<R> supplier;
20  
21          FromSupplier(final Supplier<R> supplier) {
22              this.supplier = notNull("supplier", supplier);
23          }
24  
25          public R get(final D input) {
26              return supplier.get();
27          }
28      };
29  
30      /**
31       * Get a function that always returns the input.
32       * 
33       * @param <T> the type of the input and the output for the function.
34       * @return the identity function.
35       */
36      public static <T> Function<T, T> identity() {
37          return new Identity<T>();
38      }
39  
40      private static class Identity<T> implements Function<T, T> {
41          public T get(final T input) {
42              return input;
43          }
44      }
45  
46      // /CLOVER:OFF
47      private Functions() {}
48      // /CLOVER:ON
49  }