public static <T> Iterator<T> iterator(Class<T> class1, final Object value) { if (value == null) { return Collections.EMPTY_LIST.iterator(); } if (Boon.isArray(value)) { final int length = Arry.len(value); return new Iterator<T>() { int i = 0; @Override public boolean hasNext() { return i < length; } @Override public T next() { if (i >= length) throw new NoSuchElementException("No more properties"); T next = (T) BeanUtils.idx(value, i); i++; return next; } @Override public void remove() {} }; } else if (Typ.isCollection(value.getClass())) { return ((Collection<T>) value).iterator(); } else { return (Iterator<T>) Collections.singleton(value).iterator(); } }
public static byte[] barray(Object value) { // You could handleUnexpectedException shorts, ints, longs and chars more efficiently if (value.getClass() == Typ.byteArray) { return (byte[]) value; } byte[] values = new byte[Boon.len(value)]; int index = 0; Iterator<Object> iterator = iterator(Object.class, value); while (iterator.hasNext()) { values[index] = toByte(iterator.next()); index++; } return values; }
public static char[] carray(Object value) { // You could handleUnexpectedException shorts, ints, longs and chars more efficiently if (value.getClass() == Typ.charArray) { return (char[]) value; } char[] values = new char[Boon.len(value)]; int index = 0; Iterator<Object> iterator = iterator(Typ.object, value); while (iterator.hasNext()) { values[index] = toChar(iterator.next()); index++; } return values; }
/** * Converts the value to boolean, and if it is null, it uses the default value passed. * * @param obj value to convert to boolean * @param defaultValue default value * @return obj converted to boolean */ public static boolean toBoolean(Object obj, boolean defaultValue) { if (obj == null) { return defaultValue; } if (obj instanceof Boolean) { return ((Boolean) obj).booleanValue(); } else if (obj instanceof Number || obj.getClass().isPrimitive()) { int value = toInt(obj); return value != 0 ? true : false; } else if (obj instanceof String || obj instanceof CharSequence) { String str = Conversions.toString(obj); if (str.length() == 0) { return false; } if (str.equals("false")) { return false; } else { return true; } } else if (Boon.isArray(obj)) { return Boon.len(obj) > 0; } else if (obj instanceof Collection) { if (len(obj) > 0) { List list = Lists.list((Collection) obj); while (list.remove(null)) {} return Lists.len(list) > 0; } else { return false; } } else { return toBoolean(Conversions.toString(obj)); } }
@SuppressWarnings({"rawtypes", "unchecked"}) public static Set toSet(Object value) { if (value instanceof Set) { return (Set) value; } else if (value instanceof Collection) { return new HashSet((Collection) value); } else { HashSet set = new HashSet(Boon.len(value)); Iterator<Object> iterator = iterator(Typ.object, value); while (iterator.hasNext()) { set.add(iterator.next()); } return set; } }
public static Collection toCollection(Object value) { if (value instanceof Collection) { return (Collection) value; } else if (value == null) { return new ArrayList(); } else if (value instanceof Map) { return ((Map) value).entrySet(); } else { ArrayList list = new ArrayList(Boon.len(value)); Iterator<Object> iterator = iterator(Typ.object, value); while (iterator.hasNext()) { list.add(iterator.next()); } return list; } }
@SuppressWarnings({"rawtypes", "unchecked"}) public static List toList(Object value) { if (value instanceof List) { return (List) value; } else if (value instanceof Collection) { return new ArrayList((Collection) value); } else if (value == null) { return new ArrayList(); } else if (value instanceof Map) { return new ArrayList(((Map) value).entrySet()); } else { ArrayList list = new ArrayList(Boon.len(value)); Iterator<Object> iterator = iterator(Typ.object, value); while (iterator.hasNext()) { list.add(iterator.next()); } return list; } }
@SuppressWarnings("unchecked") public static <T> T toPrimitiveArrayIfPossible(Class<T> clz, Object value) { if (clz == Typ.intArray) { return (T) iarray(value); } else if (clz == Typ.byteArray) { return (T) barray(value); } else if (clz == Typ.charArray) { return (T) carray(value); } else if (clz == Typ.shortArray) { return (T) sarray(value); } else if (clz == Typ.longArray) { return (T) larray(value); } else if (clz == Typ.floatArray) { return (T) farray(value); } else if (clz == Typ.doubleArray) { return (T) darray(value); } else if (value.getClass() == clz) { return (T) value; } else { int index = 0; Object newInstance = Array.newInstance(clz.getComponentType(), Boon.len(value)); Iterator<Object> iterator = iterator(Typ.object, value); while (iterator.hasNext()) { Object item = iterator.next(); if (clz.getComponentType().isAssignableFrom(item.getClass())) { BeanUtils.idx(newInstance, index, item); } else { item = coerce(clz.getComponentType(), item); BeanUtils.idx(newInstance, index, item); } index++; } return (T) newInstance; } }