Simple utility functions around plain objects. @author Matthew Tropiano
| 29 | * @author Matthew Tropiano |
| 30 | */ |
| 31 | public final class ObjectUtils |
| 32 | { |
| 33 | private ObjectUtils() {} |
| 34 | |
| 35 | /** |
| 36 | * Apply function for objects. |
| 37 | * @param input the input object to manipulate. |
| 38 | * @param applier the function to pass the input element to. |
| 39 | * @param <T> the return/input type. |
| 40 | * @return the input object. |
| 41 | */ |
| 42 | public static <T> T apply(T input, Consumer<T> applier) |
| 43 | { |
| 44 | applier.accept(input); |
| 45 | return input; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns if two objects are equal, performing null checking. |
| 50 | * @param a the first object. |
| 51 | * @param b the second object. |
| 52 | * @return true if equal, false if not. |
| 53 | * @see Object#equals(Object) |
| 54 | */ |
| 55 | public static boolean areEqual(Object a, Object b) |
| 56 | { |
| 57 | if (a == null) |
| 58 | return b == null; |
| 59 | else if (b == null) |
| 60 | return false; |
| 61 | else |
| 62 | return a.equals(b); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the first object if it is not null, otherwise returns the second. |
| 67 | * @param <T> class that extends Object. |
| 68 | * @param testObject the first ("tested") object. |
| 69 | * @param nullReturn the object to return if testObject is null. |
| 70 | * @return testObject if not null, nullReturn otherwise. |
| 71 | */ |
| 72 | public static <T> T isNull(T testObject, T nullReturn) |
| 73 | { |
| 74 | return testObject != null ? testObject : nullReturn; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the first object if it is not null, otherwise returns the second. |
| 79 | * @param <T> class that extends Object. |
| 80 | * @param testObject the first ("tested") object. |
| 81 | * @param nullReturn the Supplier to call to return if testObject is null. |
| 82 | * @return testObject if not null, nullReturn otherwise. |
| 83 | */ |
| 84 | public static <T> T isNull(T testObject, Supplier<T> nullReturn) |
| 85 | { |
| 86 | return testObject != null ? testObject : nullReturn.get(); |
| 87 | } |
| 88 |
nothing calls this directly
no outgoing calls
no test coverage detected