private static Object[] convertArray(Object[] values, Class<?>[] targetTypes) { Object[] result = new Object[values.length]; for (int i = 0; i < values.length; i++) result[i] = AnyConverter.convert(values[i], targetTypes[i]); return result; }
@SuppressWarnings("rawtypes") public TypeConvertingExpression(Expression source, Class<E> resultType) { super("", source); this.converter = new AnyConverter<E>(resultType); }
@Override public E convert(Object sourceValue) throws ConversionException { return convert(sourceValue, targetType, datePattern, timePattern, timestampPattern); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static <T> Converter<Object, T>[] createConverters(Class<? extends T> ... elementTypes) { Converter<Object, T>[] converters = new Converter[elementTypes.length]; for (int i = 0; i < elementTypes.length; i++) converters[i] = new AnyConverter(elementTypes[i]); return converters; }
public static <TT> TT convert(Object source, Class<TT> targetType) throws ConversionException { return convert(source, targetType, null, null, null); }
@Override public Boolean evaluate(Context context) { for (int i = 0; i < terms.length; i++) if (!AnyConverter.convert(terms[i].evaluate(context), Boolean.class)) return false; return true; }
@Override public Boolean evaluate(Context context) { for (int i = 0; i < terms.length; i++) if (AnyConverter.convert(terms[i].evaluate(context), Boolean.class)) return true; return false; }
@Override public E evaluate(Context context) { return converter.convert(term.evaluate(context)); }
@Override public Boolean evaluate(Context context) { return !AnyConverter.convert(term.evaluate(context), Boolean.class); }
public static Long queryLong(String query, Connection connection) { return AnyConverter.convert(queryScalar(query, connection), Long.class); }
public static Integer queryInt(String query, Connection connection) { return AnyConverter.convert(queryScalar(query, connection), Integer.class); }
@Override public Object evaluate(Context context) { Object container = term1.evaluate(context); Object indexObject = term2.evaluate(context); if (container instanceof List) { int index = AnyConverter.convert(indexObject, Integer.class); return ((List<?>) container).get(index); } else if (container.getClass().isArray()) { int index = AnyConverter.convert(indexObject, Integer.class); return ((Object[]) container)[index]; } else if (container instanceof String) { int index = AnyConverter.convert(indexObject, Integer.class); return ((String) container).charAt(index); } else if (container instanceof Map) { return ((Map<?,?>) container).get(indexObject); } else throw new IllegalArgumentException("Cannot do index-based access on " + BeanUtil.simpleClassName(container)); }
public static <T> T[] parse(String source, String separator, Class<T> componentType) { ArrayBuilder<T> builder = new ArrayBuilder<T>(componentType); int i = 0; int sepIndex; while ((sepIndex = source.indexOf(separator, i)) >= 0) { String token = source.substring(i, sepIndex); builder.add(AnyConverter.convert(token, componentType)); i = sepIndex + separator.length(); } builder.add(AnyConverter.convert(source.substring(i, source.length()), componentType)); return builder.toArray(); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public boolean lessOrEquals(Object part1, Object part2) { // null handling if (part2 == null || part1 == null) throw new IllegalArgumentException("Cannot compare null"); Class<?> resultType = TypeManager.combinedType(part1.getClass(), part2.getClass()); // convert the terms to the same type and compare them Object s1 = AnyConverter.convert(part1, resultType); Object s2 = AnyConverter.convert(part2, resultType); if (Comparable.class.isAssignableFrom(resultType)) return (((Comparable) s1).compareTo(s2) <= 0); else throw new UnsupportedOperationException("Cannot compare type " + BeanUtil.simpleClassName(part1) + " with " + BeanUtil.simpleClassName(part2)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public boolean less(Object part1, Object part2) { // null handling if (part2 == null || part1 == null) throw new IllegalArgumentException("Cannot compare null"); Class<?> resultType = TypeManager.combinedType(part1.getClass(), part2.getClass()); // convert the terms to the same type and compare them Object s1 = AnyConverter.convert(part1, resultType); Object s2 = AnyConverter.convert(part2, resultType); if (Comparable.class.isAssignableFrom(resultType)) return (((Comparable) s1).compareTo(s2) < 0); else throw new UnsupportedOperationException("Cannot compare type " + BeanUtil.simpleClassName(part1) + " with " + BeanUtil.simpleClassName(part2)); }
/** Tries to convert both arguments to the same type and then compares them * @param o1 the first object to compare * @param o2 the second object to compare * @return true if they are equal, otherwise false */ public static boolean equalsIgnoreType(Object o1, Object o2) { if (NullSafeComparator.equals(o1, o2)) return true; if (o1 == null || o2 == null) return false; if (o1.getClass() == o2.getClass()) return false; // OK, both are not null, but they have a different type if (o1 instanceof String && o2 instanceof Number) { Object tmp = o1; o1 = o2; o2 = tmp; } if (o1 instanceof Number) { if (o2 instanceof String) o2 = AnyConverter.convert(o2, o1.getClass()); return (((Number) o1).doubleValue() == ((Number) o2).doubleValue()); } return false; }
@SuppressWarnings({ "unchecked", "rawtypes" }) public boolean equals(Object part1, Object part2) { // null handling if (part2 == null && part1 == null) return true; if (part2 == null || part1 == null) return false; // comparing non-null values Class<?> resultType = TypeManager.combinedType(part1.getClass(), part2.getClass()); // convert the terms to the same type and compare them Object s1 = AnyConverter.convert(part1, resultType); Object s2 = AnyConverter.convert(part2, resultType); if (Comparable.class.isAssignableFrom(resultType)) return (((Comparable) s1).compareTo(s2) == 0); else throw new UnsupportedOperationException("Cannot compare type " + BeanUtil.simpleClassName(part1) + " with " + BeanUtil.simpleClassName(part2)); }
@Override public E evaluate(Context context) { boolean cond = AnyConverter.convert(condition.evaluate(context), Boolean.class); return (cond ? alt1 : alt2).evaluate(context); }
@Override public void setValue(Object bean, Object value) throws UpdateFailedException { if (bean == null) if (required) throw new IllegalArgumentException("Cannot set a property on null"); else return; if (writeMethod == null) return; if (autoConvert && value != null) { Class<?> sourceType = value.getClass(); Class<?> targetType = writeMethod.getParameterTypes()[0]; try { if (!targetType.isAssignableFrom(sourceType)) value = AnyConverter.convert(value, targetType); } catch (ConversionException e) { throw new ConfigurationError(e); } } BeanUtil.invoke(bean, writeMethod, new Object[] { value }); }