Skip to content

Instantly share code, notes, and snippets.

@culmat
Last active August 29, 2015 14:19
Show Gist options
  • Save culmat/d8f3b7e3c19028bb7a3a to your computer and use it in GitHub Desktop.
Save culmat/d8f3b7e3c19028bb7a3a to your computer and use it in GitHub Desktop.
Truth
package common;
import static java.util.Arrays.asList;
import java.awt.Desktop.Action;
import java.awt.Point;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <p>Implements the <a href="http://www.groovy-lang.org/semantics.html#Groovy-Truth">Groovy Thruth</a> for java.</p>
* <br/>Did you ever implement helper methods like <code>nullOrEmpty()</code>?
* <br/>These days are over now.
*/
public final class Truth {
public static interface AsBoolean {
boolean asBoolean();
}
/**
* Helper ENUM to reduce cyclomatic complexity through switch statement.
*/
enum TYPE {
ABSTRACTCOLLECTION, ABSTRACTMAP, BOOLEAN, STRING, NUMBER, MATCHER, OBJECT;
public static TYPE of(java.lang.String s) {
try {
return TYPE.valueOf(s.toUpperCase());
} catch (Exception e) {
return OBJECT;
}
}
};
public static boolean not(Object o) {
return !is(o);
}
public final static boolean is(Object o) {
if (o == null)
return false;
if(o instanceof AsBoolean)
return ((AsBoolean) o).asBoolean();
Class<?> rootClass = rootClass(o);
if (rootClass.isArray())
return Array.getLength(o) > 0;
else if(Iterator.class.isAssignableFrom(rootClass))
return ((Iterator<?>) o).hasNext();
else if(Enumeration.class.isAssignableFrom(rootClass))
return ((Enumeration<?>) o).hasMoreElements();
switch (TYPE.of(rootClass.getSimpleName())) {
case STRING:
return !"".equals(((String) o).trim());
case NUMBER:
return Double.valueOf(o.toString()) != 0;
case ABSTRACTCOLLECTION:
return ((Collection<?>) o).size() > 0;
case ABSTRACTMAP:
return ((Map<?, ?>) o).size() > 0;
case BOOLEAN:
return Boolean.TRUE.equals(o);
case MATCHER : return ((Matcher) o).matches();
case OBJECT:
return true;
}
assert false; // Execution should never reach this point!
return false;
}
static {
assert is("s df");
assert is(1);
assert is(1L);
assert is("FALSE");
assert is("false");
assert not(Boolean.parseBoolean("false"));
assert is(Boolean.parseBoolean("TRUE"));
assert is("null");
assert is("void");
assert is("Off");
assert is("no");
assert not(" ");
assert not("");
assert not(null);
assert not(0);
assert not(0L);
assert not(new LinkedList<String>());
assert is(EnumSet.allOf(Action.class));
assert not(new HashMap<>());
assert is(asList(""));
assert not(new String[] {});
assert is(new Object[] { 1 });
assert is(new String[] { "" });
assert is("abc".getBytes());
assert not(Boolean.FALSE);
assert is(Boolean.TRUE);
assert is(-1);
assert is(Integer.valueOf(1));
assert not(Integer.valueOf(0));
assert is(new Point());
assert is(new Object());
assert not(Pattern.compile("a").matcher("b"));
assert is(Pattern.compile("b").matcher("b"));
assert is(Arrays.asList("a").iterator());
assert not(new LinkedList<>().iterator());
assert not(Collections.EMPTY_LIST.iterator());
assert not(new Vector<>().elements());
assert is(new Vector<>(asList(-1)).elements());
assert is(new AsBoolean(){
public boolean asBoolean(){
return true;
}
});
assert not(new AsBoolean(){
public boolean asBoolean(){
return false;
}
});
}
public static Class<?> rootClass(Object o) {
if (o == null)
return null;
return rootClass(o.getClass());
}
public static Class<?> rootClass(Class<?> c) {
if (c == null)
return null;
if(c.equals(Object.class))
return Object.class;
Class<?> supr = c.getSuperclass();
if (supr.equals(Object.class))
return c;
else
return rootClass(supr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment