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