[84] | 1 | package com.framsticks.util; |
---|
| 2 | |
---|
[100] | 3 | // import org.apache.logging.log4j.Logger; |
---|
[88] | 4 | |
---|
[84] | 5 | /** |
---|
| 6 | * Author: Piotr Śniegowski |
---|
| 7 | */ |
---|
| 8 | public class Misc { |
---|
[88] | 9 | // private static final Logger log = |
---|
[100] | 10 | // LogManager.getLogger(Misc.class); |
---|
[88] | 11 | |
---|
| 12 | public static class WithType { |
---|
| 13 | protected Object value; |
---|
| 14 | |
---|
| 15 | /** |
---|
| 16 | * @param value |
---|
| 17 | */ |
---|
| 18 | public WithType(Object value) { |
---|
| 19 | this.value = value; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | @Override |
---|
| 23 | public String toString() { |
---|
| 24 | if (value == null) { |
---|
| 25 | return "null"; |
---|
| 26 | } |
---|
| 27 | return value + "(" + value.getClass() + ")"; |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | public static WithType withType(Object value) { |
---|
| 32 | return new WithType(value); |
---|
| 33 | } |
---|
| 34 | |
---|
[84] | 35 | public static boolean equals(Object a, Object b) { |
---|
[100] | 36 | // log.info("equality of {} ? {}", withType(a), withType(b)); |
---|
[84] | 37 | if (a != null) { |
---|
| 38 | return (b != null && a.equals(b)); |
---|
| 39 | } |
---|
| 40 | return b == null; |
---|
| 41 | } |
---|
[88] | 42 | |
---|
| 43 | public static <T> T returnNotNull(T first, T second) { |
---|
| 44 | if (first != null) { |
---|
| 45 | return first; |
---|
| 46 | } |
---|
| 47 | return second; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | public static <T> T throwIfNull(T value) { |
---|
| 51 | if (value == null) { |
---|
| 52 | throw new FramsticksException().msg("value should not be null"); |
---|
| 53 | } |
---|
| 54 | return value; |
---|
| 55 | } |
---|
[102] | 56 | |
---|
| 57 | public static void checkEquals(Object expected, Object found, String message, Object context) { |
---|
| 58 | if (equals(expected, found)) { |
---|
| 59 | return; |
---|
| 60 | } |
---|
[103] | 61 | FramsticksException e = new FramsticksException().msg(message).arg("expected", expected).arg("found", found); |
---|
| 62 | if (context != null) { |
---|
| 63 | e.arg("in", context); |
---|
| 64 | } |
---|
| 65 | throw e; |
---|
[102] | 66 | } |
---|
[107] | 67 | |
---|
| 68 | public static Class<?> getClass(Object object) { |
---|
| 69 | return (object != null ? object.getClass() : null); |
---|
| 70 | } |
---|
[84] | 71 | } |
---|