@Test public void Conversions_toMap_DoesEnumsFunnyTest() { Stuffing stuffing = new Stuffing(); stuffing.name = "i'm Stuffed!"; stuffing.stuff = Stuff.THIS_KIND_OF_STUFF; Map<String, Object> problemMap = Conversions.toMap(stuffing); puts(problemMap); assertNotNull("What?", problemMap); assertEquals( "Map should be a 2 null are ignored. Default value of null is nothing.", 2, problemMap.size()); // nulls are ignored // Map sets perceived "Wrong" value for enum, it is the right value assertEquals("This works", Stuff.THIS_KIND_OF_STUFF, problemMap.get("stuff")); Stuffing.Stuffed hAndRPuffAndStuff = new Stuffing.Stuffed(); hAndRPuffAndStuff.dateStuffed = new Date(); stuffing.stuffed = hAndRPuffAndStuff; Map<String, Object> noProblemMap = Conversions.toMap(stuffing); puts(noProblemMap); assertNotNull("What?", noProblemMap); assertEquals("Map is now 3.", 3, noProblemMap.size()); }
/** * Gets the field value. * * @return the value of the field */ public Object value() { if (!convert1st) { FieldAccess field = field(); if (field != null) { switch (field.typeEnum()) { case NUMBER: this.value = (VALUE) Conversions.coerce(field.type(), this.value); return new MyNumber(this.value); case ARRAY: case ARRAY_INT: case ARRAY_BYTE: case ARRAY_SHORT: case ARRAY_FLOAT: case ARRAY_DOUBLE: case ARRAY_LONG: case ARRAY_STRING: case ARRAY_OBJECT: case COLLECTION: case SET: case LIST: this.value = (VALUE) Conversions.coerce(field.getComponentClass(), this.value); break; default: this.value = (VALUE) Conversions.coerce(field.type(), this.value); } } convert1st = true; } return value; }
private void initForDate() { value = (VALUE) Conversions.toDate(value); if (operator == Operator.BETWEEN) { values[0] = (VALUE) Conversions.toDate(values[0]); values[1] = (VALUE) Conversions.toDate(values[1]); } }
public static boolean toBooleanOrDie(Object obj) { if (obj == null) { die("Can't convert boolean from a null"); } if (obj instanceof Boolean) { return ((Boolean) obj).booleanValue(); } 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 if (str.equals("true")) { return true; } die("Can't convert string", obj, "to boolean "); return false; } else { die("Can't convert", obj, "to boolean "); return false; } }
private void initForShortValue(short v) { this.value = (VALUE) (Short) v; switch (operator) { case EQUAL: nativeDelegate = ObjectFilter.eqShort(name, v); break; case NOT_EQUAL: nativeDelegate = ObjectFilter.notEqShort(name, v); break; case LESS_THAN: nativeDelegate = ObjectFilter.ltShort(name, v); break; case LESS_THAN_EQUAL: nativeDelegate = ObjectFilter.lteShort(name, v); break; case GREATER_THAN: nativeDelegate = ObjectFilter.gtShort(name, v); break; case GREATER_THAN_EQUAL: nativeDelegate = ObjectFilter.gteShort(name, v); break; case IN: nativeDelegate = ObjectFilter.inShorts(name, Conversions.sarray(values)); break; case BETWEEN: nativeDelegate = ObjectFilter.betweenShort(name, (v), Conversions.toShort(values[1])); break; case NOT_IN: nativeDelegate = ObjectFilter.notInShorts(name, Conversions.sarray(values)); break; default: useDelegate = false; } }
private void initForFloat(float value) { this.value = (VALUE) (Float) value; switch (operator) { case EQUAL: nativeDelegate = ObjectFilter.eqFloat(name, (value)); break; case NOT_EQUAL: nativeDelegate = ObjectFilter.notEqFloat(name, (value)); break; case LESS_THAN: nativeDelegate = ObjectFilter.ltFloat(name, (value)); break; case LESS_THAN_EQUAL: nativeDelegate = ObjectFilter.lteFloat(name, (value)); break; case GREATER_THAN: nativeDelegate = ObjectFilter.gtFloat(name, (value)); break; case GREATER_THAN_EQUAL: nativeDelegate = ObjectFilter.gteFloat(name, (value)); break; case BETWEEN: nativeDelegate = ObjectFilter.betweenFloat(name, (value), Conversions.toFloat(values[1])); break; case IN: nativeDelegate = ObjectFilter.inFloats(name, Conversions.farray(values)); break; case NOT_IN: nativeDelegate = ObjectFilter.notInFloats(name, Conversions.farray(values)); break; default: useDelegate = false; } }
private void initForLong(long value) { this.value = (VALUE) (Long) value; switch (operator) { case EQUAL: nativeDelegate = ObjectFilter.eqLong(name, (value)); break; case NOT_EQUAL: nativeDelegate = ObjectFilter.notEqLong(name, (value)); break; case LESS_THAN: nativeDelegate = ObjectFilter.ltLong(name, (value)); break; case LESS_THAN_EQUAL: nativeDelegate = ObjectFilter.lteLong(name, (value)); break; case GREATER_THAN: nativeDelegate = ObjectFilter.gtLong(name, (value)); break; case GREATER_THAN_EQUAL: nativeDelegate = ObjectFilter.gteLong(name, (value)); break; case IN: nativeDelegate = ObjectFilter.inLongs(name, Conversions.larray(values)); break; case BETWEEN: nativeDelegate = ObjectFilter.betweenLong(name, (value), Conversions.toLong(values[1])); break; case NOT_IN: nativeDelegate = ObjectFilter.notInLongs(name, Conversions.larray(values)); break; default: useDelegate = false; } }
private void initForByte(byte value) { this.value = (VALUE) (Byte) value; switch (operator) { case EQUAL: nativeDelegate = ObjectFilter.eqByte(name, (value)); break; case NOT_EQUAL: nativeDelegate = ObjectFilter.notEqByte(name, (value)); break; case LESS_THAN: nativeDelegate = ObjectFilter.ltByte(name, (value)); break; case LESS_THAN_EQUAL: nativeDelegate = ObjectFilter.lteByte(name, (value)); break; case GREATER_THAN: nativeDelegate = ObjectFilter.gtByte(name, (value)); break; case GREATER_THAN_EQUAL: nativeDelegate = ObjectFilter.gteByte(name, (value)); break; case IN: nativeDelegate = ObjectFilter.inBytes(name, Conversions.barray(values)); break; case NOT_IN: nativeDelegate = ObjectFilter.notInBytes(name, Conversions.barray(values)); break; case BETWEEN: nativeDelegate = ObjectFilter.betweenByte(name, (value), Conversions.toByte(values[1])); break; default: useDelegate = false; } }
/** * Gets the field value. * * @return the value of the field */ public Object value2() { if (!convert2nd) { FieldAccess field = this.field(); this.value2 = (VALUE) Conversions.coerce(field.type(), this.value2); convert2nd = true; } return value2; }
private void initForChar(char value) { this.value = (VALUE) (Character) value; switch (operator) { case EQUAL: nativeDelegate = ObjectFilter.eqChar(name, (value)); break; case NOT_EQUAL: nativeDelegate = ObjectFilter.notEqChar(name, (value)); break; case LESS_THAN: nativeDelegate = ObjectFilter.ltChar(name, (value)); break; case LESS_THAN_EQUAL: nativeDelegate = ObjectFilter.lteChar(name, (value)); break; case GREATER_THAN: nativeDelegate = ObjectFilter.gtChar(name, (value)); break; case GREATER_THAN_EQUAL: nativeDelegate = ObjectFilter.gteChar(name, (value)); break; case BETWEEN: nativeDelegate = ObjectFilter.betweenChar(name, (value), Conversions.toChar(values[1])); break; case IN: nativeDelegate = ObjectFilter.inChars(name, Conversions.carray(values)); break; case NOT_IN: nativeDelegate = ObjectFilter.notInChars(name, Conversions.carray(values)); break; default: useDelegate = false; } }
private void initForDouble(double value) { this.value = (VALUE) (Double) value; switch (operator) { case EQUAL: nativeDelegate = ObjectFilter.eqDouble(name, (value)); break; case NOT_EQUAL: nativeDelegate = ObjectFilter.notEqDouble(name, (value)); break; case LESS_THAN: nativeDelegate = ObjectFilter.ltDouble(name, (value)); break; case LESS_THAN_EQUAL: nativeDelegate = ObjectFilter.lteDouble(name, (value)); break; case GREATER_THAN: nativeDelegate = ObjectFilter.gtDouble(name, (value)); break; case GREATER_THAN_EQUAL: nativeDelegate = ObjectFilter.gteDouble(name, (value)); break; case BETWEEN: nativeDelegate = ObjectFilter.betweenDouble(name, (value), Conversions.toDouble(values[1])); break; case IN: nativeDelegate = ObjectFilter.inDoubles(name, Conversions.darray(values)); break; case NOT_IN: nativeDelegate = ObjectFilter.notInDoubles(name, Conversions.darray(values)); break; default: useDelegate = false; } }
private void initForInt(int v) { this.value = (VALUE) (Integer) v; switch (operator) { case EQUAL: nativeDelegate = ObjectFilter.eqInt(name, v); break; case NOT_EQUAL: nativeDelegate = ObjectFilter.notEqInt(name, v); break; case LESS_THAN: nativeDelegate = ObjectFilter.ltInt(name, v); break; case LESS_THAN_EQUAL: nativeDelegate = ObjectFilter.lteInt(name, v); break; case GREATER_THAN: nativeDelegate = ObjectFilter.gtInt(name, v); break; case GREATER_THAN_EQUAL: nativeDelegate = ObjectFilter.gteInt(name, v); break; case BETWEEN: nativeDelegate = ObjectFilter.betweenInt(name, v, Conversions.toInt(values[1])); break; case IN: nativeDelegate = ObjectFilter.inInts(name, Conversions.iarray(values)); break; case NOT_IN: nativeDelegate = ObjectFilter.notInInts(name, Conversions.iarray(values)); break; default: useDelegate = false; } }
/** * Gets the field value. * * @return the value of the field */ public Object value() { if (!convert1st) { FieldAccess field = field(); if (field != null) { switch (field.typeEnum()) { case ARRAY: case COLLECTION: case SET: case LIST: this.value = (VALUE) Conversions.coerce(field.getComponentClass(), this.value); break; default: this.value = (VALUE) Conversions.coerce(field.type(), this.value); } } convert1st = true; } return value; }
/** * 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)); } }
public static <T> T[] toArray(Class<T> componentType, Collection<T> collection) { T[] array = (T[]) Array.newInstance(componentType, collection.size()); if (componentType.isAssignableFrom(getComponentType(collection))) { return collection.toArray(array); } else { int index = 0; for (Object o : collection) { array[index] = Conversions.coerce(componentType, o); index++; } return array; } }
public Set<Object> valueSet() { if (!convert1st) { HashSet<Object> set = new HashSet<>(values.length); FieldAccess field = this.field(); Class<?> classType = field.type(); for (Object v : values) { v = Conversions.coerce(classType, this.value); set.add(v); } value = (VALUE) set; convert1st = true; } return (Set<Object>) value; }
@Override public int intValue() { return Conversions.toInt(value); }
@Override public float floatValue() { return Conversions.toFloat(value); }
@Override public double doubleValue() { return Conversions.toDouble(value); }
public static <V> V[] array(Class<V> type, final Collection<V> array) { return Conversions.toArray(type, array); }
private void initIfNeeded() { if (initialized) return; initialized = true; String name = this.name.toString(); FieldAccess field = field(); if (field == null) { return; } Class type = field.type(); if (!type.isPrimitive() && type != Typ.date) { return; } if (type == Typ.date) { if (!(value instanceof Date)) { initForDate(); } return; } useDelegate = true; if (type == Typ.intgr) { int v = Conversions.toInt(value); initForInt(v); } else if (type == Typ.bt) { byte v = Conversions.toByte(value); initForByte(v); } else if (type == Typ.shrt) { short v = Conversions.toShort(value); initForShortValue(v); } else if (type == Typ.lng) { long v = Conversions.toLong(value); initForLong(v); } else if (type == Typ.flt) { float v = Conversions.toFloat(value); initForFloat(v); } else if (type == Typ.dbl) { double v = Conversions.toDouble(value); initForDouble(v); } else if (type == Typ.bln) { switch (operator) { case EQUAL: nativeDelegate = ObjectFilter.eqBoolean(name, Conversions.toBoolean(value)); break; case NOT_EQUAL: nativeDelegate = ObjectFilter.notEqBoolean(name, Conversions.toBoolean(value)); break; default: useDelegate = false; } } else if (type == Typ.chr) { char v = Conversions.toChar(value); initForChar(v); } }
@Override public long longValue() { return Conversions.toLong(value); }