Created
April 9, 2014 07:13
-
-
Save veysiertekin/91eb34b201ab3b4058fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Object decode(Object... values) { | |
if (values == null) | |
return null; | |
Object object = values[0]; | |
for (int j = 1; j < values.length; j += 2) { | |
if (nullSafeEquals(values[j], object)) { | |
if (j + 1 < values.length) { | |
return values[j + 1]; | |
} | |
} | |
if (j + 1 == values.length) { | |
return values[j]; | |
} | |
} | |
return null; | |
} | |
public static <T> T nvl(T object, T defaultValue) { | |
if (object instanceof String) { | |
return (((String) object) == null || ((String) object).trim().length() == 0) ? defaultValue : object; | |
} | |
return object == null ? defaultValue : object; | |
} | |
public static Map<Object, Object> nvlMap(Map<Object, Object> object, Map<Object, Object> defaultValues) { | |
if (object == null) { | |
object = new HashMap<Object, Object>(); | |
} | |
if (defaultValues == null) { | |
defaultValues = new HashMap<Object, Object>(); | |
} | |
Map<Object, Object> map = EBSUtil.clone(object); | |
for (Object key : defaultValues.keySet()) { | |
map.put(key, nvl(object.get(key), defaultValues.get(key))); | |
} | |
return map; | |
} | |
/** | |
* <a href="http://examples.javacodegeeks.com/core-java/class/null-safe-equals-method/">Null safe equals method</a> | |
* | |
* @param o1 | |
* @param o2 | |
* @return | |
* boolean | |
*/ | |
public static boolean nullSafeEquals(Object o1, Object o2) { | |
if (o1 == o2) { | |
return true; | |
} | |
if (o1 == null || o2 == null) { | |
return false; | |
} | |
if (o1.equals(o2)) { | |
return true; | |
} | |
if (o1 instanceof Object[] && o2 instanceof Object[]) { | |
return Arrays.equals((Object[]) o1, (Object[]) o2); | |
} | |
if (o1 instanceof boolean[] && o2 instanceof boolean[]) { | |
return Arrays.equals((boolean[]) o1, (boolean[]) o2); | |
} | |
if (o1 instanceof byte[] && o2 instanceof byte[]) { | |
return Arrays.equals((byte[]) o1, (byte[]) o2); | |
} | |
if (o1 instanceof char[] && o2 instanceof char[]) { | |
return Arrays.equals((char[]) o1, (char[]) o2); | |
} | |
if (o1 instanceof double[] && o2 instanceof double[]) { | |
return Arrays.equals((double[]) o1, (double[]) o2); | |
} | |
if (o1 instanceof float[] && o2 instanceof float[]) { | |
return Arrays.equals((float[]) o1, (float[]) o2); | |
} | |
if (o1 instanceof int[] && o2 instanceof int[]) { | |
return Arrays.equals((int[]) o1, (int[]) o2); | |
} | |
if (o1 instanceof long[] && o2 instanceof long[]) { | |
return Arrays.equals((long[]) o1, (long[]) o2); | |
} | |
if (o1 instanceof short[] && o2 instanceof short[]) { | |
return Arrays.equals((short[]) o1, (short[]) o2); | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment