View Javadoc
1   package com.github.davidmoten.rx2;
2   
3   import com.github.davidmoten.rx2.exceptions.ThrowingException;
4   
5   import io.reactivex.functions.Function;
6   
7   public final class Functions {
8   
9       private Functions() {
10          // prevent instantiation
11      }
12  
13      public static <T> Function<Object, T> constant(final T value) {
14          return new Function<Object, T>() {
15  
16              @Override
17              public T apply(Object t) throws Exception {
18                  return value;
19              }
20          };
21      }
22  
23      @SuppressWarnings("unchecked")
24      public static <T> Function<T, T> identity() {
25          return (Function<T, T>) IdentityHolder.INSTANCE;
26      }
27  
28      private static final class IdentityHolder {
29          static final Function<Object, Object> INSTANCE = new Function<Object, Object>() {
30  
31              @Override
32              public Object apply(Object t) throws Exception {
33                  return t;
34              }
35          };
36      }
37  
38      public static <T, R> Function<T, R> throwing() {
39          // TODO make holder
40          return new Function<T, R>() {
41  
42              @Override
43              public R apply(T t) {
44                  throw new ThrowingException();
45              }
46          };
47      }
48      
49      public static <T> Function<T, String> toStringFunction() {
50          // TODO make holder
51          return new Function<T,String> () {
52  
53              @Override
54              public String apply(T t) throws Exception {
55                  return String.valueOf(t);
56              }
57          };
58      }
59  
60  }