/** * Converts this to a Java array having an accurate component type. * * <pre>{@code * // = [] of type String[] * Future.<String> of(() -> { throw new Error(); }) * .toJavaArray(String[]::new) * * // = [ok] of type String[] * Try.of(() -> "ok") * .toJavaArray(String[]::new) * * // = [1, 2, 3] of type Integer[] * List.of(1, 2, 3) * .toJavaArray(Integer[]::new) * }</pre> * * @param arrayFactory an <code>int</code> argument function that * creates an array of the correct component * type with the specified size * @return The array provided by the factory filled with the values from this <code>Value</code>. * @throws NullPointerException if componentType is null */ default T[] toJavaArray(IntFunction<T[]> arrayFactory) { java.util.List<T> javaList = toJavaList(); return javaList.toArray(arrayFactory.apply(javaList.size())); }
componentType = (Class<T>) boxedType; final java.util.List<T> list = toJavaList(); return list.toArray((T[]) java.lang.reflect.Array.newInstance(componentType, list.size()));
/** * Converts this to a Java array with component type {@code Object} * * <pre>{@code * // = [] of type Object[] * Future.<String> of(() -> { throw new Error(); }) * .toJavaArray() * * // = [ok] of type Object[] * Try.of(() -> "ok") * .toJavaArray() * * // = [1, 2, 3] of type Object[] * List.of(1, 2, 3) * .toJavaArray() * }</pre> * * @return A new Java array. */ default Object[] toJavaArray() { if ((this instanceof Traversable<?>) && ((Traversable<?>) this).isTraversableAgain()) { final Object[] results = new Object[((Traversable<T>) this).size()]; final Iterator<T> iter = iterator(); Arrays.setAll(results, i -> iter.next()); return results; } else { return toJavaList().toArray(); } }
@Override Object toJavaObj(T value) throws IOException { return value.toJavaList(); }
/** * Converts this to a Java array having an accurate component type. * * <pre>{@code * // = [] of type String[] * Future.<String> of(() -> { throw new Error(); }) * .toJavaArray(String[]::new) * * // = [ok] of type String[] * Try.of(() -> "ok") * .toJavaArray(String[]::new) * * // = [1, 2, 3] of type Integer[] * List.of(1, 2, 3) * .toJavaArray(Integer[]::new) * }</pre> * * @param arrayFactory an <code>int</code> argument function that * creates an array of the correct component * type with the specified size * @return The array provided by the factory filled with the values from this <code>Value</code>. * @throws NullPointerException if componentType is null */ default T[] toJavaArray(IntFunction<T[]> arrayFactory) { java.util.List<T> javaList = toJavaList(); return javaList.toArray(arrayFactory.apply(javaList.size())); }
componentType = (Class<T>) boxedType; final java.util.List<T> list = toJavaList(); return list.toArray((T[]) java.lang.reflect.Array.newInstance(componentType, list.size()));
/** * Converts this to a Java array with component type {@code Object} * * <pre>{@code * // = [] of type Object[] * Future.<String> of(() -> { throw new Error(); }) * .toJavaArray() * * // = [ok] of type Object[] * Try.of(() -> "ok") * .toJavaArray() * * // = [1, 2, 3] of type Object[] * List.of(1, 2, 3) * .toJavaArray() * }</pre> * * @return A new Java array. */ default Object[] toJavaArray() { if ((this instanceof Traversable<?>) && ((Traversable<?>) this).isTraversableAgain()) { final Object[] results = new Object[((Traversable<T>) this).size()]; final Iterator<T> iter = iterator(); Arrays.setAll(results, i -> iter.next()); return results; } else { return toJavaList().toArray(); } }