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; }
@Override public E convert(Object sourceValue) throws ConversionException { return convert(sourceValue, targetType, datePattern, timePattern, timestampPattern); }
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 E evaluate(Context context) { return converter.convert(term.evaluate(context)); }
@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; }
public static Long queryLong(String query, Connection connection) { return AnyConverter.convert(queryScalar(query, connection), Long.class); }
@Override public Boolean evaluate(Context context) { return !AnyConverter.convert(term.evaluate(context), Boolean.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)); }
@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); }
public static <T> T[] queryScalarRowsAsArray(String query, Class<T> componentType, Connection connection) { Statement statement = null; ResultSet resultSet = null; try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); ArrayBuilder<T> builder = new ArrayBuilder<T>(componentType); while (resultSet.next()) builder.add(AnyConverter.convert(resultSet.getObject(1), componentType)); return builder.toArray(); } catch (SQLException e) { throw new RuntimeException("Database query failed: " + query, e); } finally { closeResultSetAndStatement(resultSet, statement); } }
/** Converts all array elements with the same {@link Converter}. * @param converter the converter to apply * @param componentType the component type of the result array * @param sourceValues the source values to convert * @return an array with the mapped values * @param <S> the object type to convert from * @param <T> the object type to convert to * @throws ConversionException if conversion fails */ public static <S, T> T[] convertWith(Converter<S, T> converter, Class<T> componentType, S[] sourceValues) throws ConversionException { T[] result = ArrayUtil.newInstance(componentType, sourceValues.length); for (int i = 0; i < sourceValues.length; i++) { Object tmp = (converter != null ? converter.convert(sourceValues[i]) : sourceValues[i]); result[i] = AnyConverter.convert(tmp, componentType); } return result; }
public static <T> T[] queryScalarRow(String query, Class<T> componentType, Connection connection) { Statement statement = null; ResultSet resultSet = null; try { statement = connection.createStatement(); resultSet = statement.executeQuery(query); assertNext(resultSet, query); int columnCount = columnCount(resultSet); T[] result = ArrayUtil.newInstance(componentType, columnCount); for (int i = 0; i < columnCount; i++) Array.set(result, i, AnyConverter.convert(resultSet.getObject(i + 1), componentType)); assertNoNext(resultSet, query); return result; } catch (SQLException e) { throw new RuntimeException("Database query failed: " + query, e); } finally { closeResultSetAndStatement(resultSet, statement); } }
@SuppressWarnings("unchecked") public static <T> T[] convert(Object[] sourceValue, String propertyName, Class<T> propertyType) throws ConversionException { T[] array = ArrayUtil.newInstance(propertyType, sourceValue.length); PropertyAccessor<Object, T> propertyAccessor = PropertyAccessorFactory.getAccessor(propertyName); for (int i = 0; i < sourceValue.length; i++) { Object value = propertyAccessor.getValue(sourceValue[i]); array[i] = AnyConverter.convert(value, propertyType); } return array; }