public E[] toArray() { E[] result = ArrayUtil.newInstance(componentType, elementCount); System.arraycopy(buffer, 0, result, 0, elementCount); elementCount = 0; buffer = null; return result; }
@SuppressWarnings("unchecked") public static <BEAN, PROP_TYPE> PROP_TYPE[] extractProperties(BEAN[] beans, String propertyName, Class<PROP_TYPE> propertyType) { PROP_TYPE[] result = ArrayUtil.newInstance(propertyType, beans.length); for (int i = 0; i < beans.length; i++) { BEAN bean = beans[i]; result[i] = (PROP_TYPE) getPropertyValue(bean, propertyName); } return result; }
public DataContainer<E[]> next(DataContainer<E[]> container) { beInitialized(container); if (rows.size() == 0 || columnIndex >= rows.get(0).length) return null; @SuppressWarnings("unchecked") E[] column = ArrayUtil.newInstance((Class<E>) source.getType().getComponentType(), rows.size()); for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) { E[] row = rows.get(rowIndex); column[rowIndex] = (columnIndex < row.length ? row[columnIndex] : null); } columnIndex++; return container.setData(column); }
@SuppressWarnings("unchecked") public static <T> T[] append(T value, T[] array) { if (array == null) { return toArray(value); } else { T[] newArray = newInstance(componentType(array), array.length + 1); System.arraycopy(array, 0, newArray, 0, array.length); newArray[array.length] = value; return newArray; } }
public static <T> T[] append(T[] newValues, T[] array) { if (array == null) { return newValues.clone(); } else { T[] newArray = newInstance(componentType(array), array.length + newValues.length); System.arraycopy(array, 0, newArray, 0, array.length); for (int i = 0; i < newValues.length; i++) newArray[array.length + i] = newValues[i]; return newArray; } }
@Override public DataContainer<E[]> next(DataContainer<E[]> container) { beInitialized(container); if (rows.size() == 0 || columnIndex >= rows.get(0).length) return null; @SuppressWarnings("unchecked") E[] column = ArrayUtil.newInstance((Class<E>) source.getType().getComponentType(), rows.size()); for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) { E[] row = rows.get(rowIndex); column[rowIndex] = (columnIndex < row.length ? row[columnIndex] : null); } columnIndex++; return container.setData(column); }
public static <T> T[] cloneAll(T[] input) { T[] output = ArrayUtil.newInstance(ArrayUtil.componentType(input), input.length); for (int i = 0; i < input.length; i++) output[i] = clone(input[i]); return output; }
/** 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; }
@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; }
@Override @SuppressWarnings("unchecked") public E[] evaluate(Context context) { E[][] arrays = (E[][]) ExpressionUtil.evaluateAll(terms, context); int totalLength = totalLength(arrays); E[] result = ArrayUtil.newInstance(componentType(arrays), totalLength); int resultIndex = 0; for (E[] array : arrays) for (int localIndex = 0; localIndex < array.length; localIndex++) result[resultIndex] = array[localIndex]; 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); } }
/** * If there are no converters, it only transforms the array type; * if there is a single converter, all elements are converted with the same converter; * if there are several converters, the number of converters and array elements are * assumed to be equal and each element is converted with the converter of the same index. */ @Override public T[] convert(S[] sourceValues) throws ConversionException { if (sourceValues == null) return null; if (components.length == 0) return convertWith(null, targetComponentType, sourceValues); else if (components.length == 1) return convertWith(components[0], targetComponentType, sourceValues); else { if (sourceValues.length != components.length) throw new IllegalArgumentException("Array has a different size than the converter list"); T[] result = ArrayUtil.newInstance(targetComponentType, components.length); for (int i = 0; i < components.length; i++) result[i] = components[i].convert(sourceValues[i]); return result; } }