View Javadoc
1   /*
2      Copyright 2011 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue;
17  
18  import java.lang.reflect.Constructor;
19  import java.util.function.BiFunction;
20  import java.util.function.Function;
21  import java.util.function.Predicate;
22  
23  import static io.atlassian.fugue.Either.left;
24  import static io.atlassian.fugue.Either.right;
25  
26  public class UtilityFunctions {
27    public static final Predicate<Integer> isEven = dividableBy(2);
28  
29    public static final BiFunction<Integer, Integer, Integer> sum = (a, b) -> a + b;
30  
31    public static final BiFunction<Integer, Integer, Integer> subtract = (a, b) -> a - b;
32  
33    public static final BiFunction<Integer, Integer, Integer> product = (a, b) -> a * b;
34  
35    public static Predicate<Integer> dividableBy(final int div) {
36      return input -> input % div == 0;
37    }
38  
39    public static Function<Integer, Integer> addOne = integer -> integer + 1;
40  
41    public static Function<Integer, Integer> square = input -> input * input;
42  
43    public static Function<Boolean, String> bool2String = String::valueOf;
44    public static Function<Integer, String> int2String = String::valueOf;
45  
46    public static Function<String, String> reverse = from -> new StringBuilder(from).reverse().toString();
47  
48    public static BiFunction<String, Integer, Option<Character>> charAt = (s, i) -> s != null && i != null && i >= 0 && i < s.length() ? Option.some(s
49      .charAt(i)) : Option.<Character> none();
50  
51    public static Function<Pair<String, Integer>, Option<String>> leftOfString = pair -> pair != null && pair.left() != null && pair.right() != null
52      && pair.right() >= 0 && pair.right() <= pair.left().length() ? Option.some(pair.left().substring(0, pair.right())) : Option.<String> none();
53  
54    public static Function<String, Function<Integer, Boolean>> hasMinLength = text -> min -> (text == null ? "" : text).length() >= (min == null ? 0
55      : min);
56  
57    public static Function<Object, String> toStringFunction() {
58      return Object::toString;
59    }
60  
61    static <A> Function<Class<A>, Either<Exception, A>> defaultCtor() {
62      return klass -> {
63        try {
64          final Constructor<A> declaredConstructor = klass.getDeclaredConstructor();
65          declaredConstructor.setAccessible(true);
66          return right(declaredConstructor.newInstance());
67        } catch (final Exception e) {
68          return left(e);
69        }
70      };
71    }
72  
73  }