/** * Creates a Array based on the elements of a short array. * * @param array a short array * @return A new Array of Short values */ public static Array<Short> ofAll(short[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array based on the elements of an int array. * * @param array an int array * @return A new Array of Integer values */ public static Array<Integer> ofAll(int[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array based on the elements of a long array. * * @param array a long array * @return A new Array of Long values */ public static Array<Long> ofAll(long[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array based on the elements of a char array. * * @param array a char array * @return A new Array of Character values */ public static Array<Character> ofAll(char[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array based on the elements of a double array. * * @param array a double array * @return A new Array of Double values */ public static Array<Double> ofAll(double[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array based on the elements of a boolean array. * * @param array a boolean array * @return A new Array of Boolean values */ public static Array<Boolean> ofAll(boolean[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
@Override public Array<T> peek(Consumer<? super T> action) { Objects.requireNonNull(action, "action is null"); if (!isEmpty()) { action.accept(head()); } return this; }
@Override public int indexOf(T element, int from) { for (int i = from; i < length(); i++) { if (Objects.equals(get(i), element)) { return i; } } return -1; }
@Override public int lastIndexOf(T element, int end) { for (int i = Math.min(end, length() - 1); i >= 0; i--) { if (Objects.equals(get(i), element)) { return i; } } return -1; }
@Override public <U> Array<U> map(Function<? super T, ? extends U> mapper) { Objects.requireNonNull(mapper, "mapper is null"); final Object[] arr = new Object[length()]; for (int i = 0; i < length(); i++) { arr[i] = mapper.apply(get(i)); } return wrap(arr); }
@Override public Tuple2<Array<T>, Array<T>> partition(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); final java.util.List<T> left = new ArrayList<>(), right = new ArrayList<>(); for (T t : this) { (predicate.test(t) ? left : right).add(t); } return Tuple.of(ofAll(left), ofAll(right)); }
@Override public Array<T> dropWhile(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); for (int i = 0; i < length(); i++) { if (!predicate.test(get(i))) { return drop(i); } } return empty(); }
@Override public Array<T> takeWhile(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); for (int i = 0; i < length(); i++) { final T value = get(i); if (!predicate.test(value)) { return take(i); } } return this; }
@Override public Array<T> appendAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); final Object[] source = toArray(elements); if (source.length == 0) { return this; } else { final Object[] copy = new Object[length() + source.length]; System.arraycopy(delegate, startIndex, copy, 0, length()); System.arraycopy(source, 0, copy, length(), source.length); return wrap(copy); } }
@Override public <U> Array<U> scanLeft(U zero, BiFunction<? super U, ? super T, ? extends U> operation) { Objects.requireNonNull(operation, "operation is null"); return Collections.scanLeft( this, zero, operation, new java.util.ArrayList<>(), (c, u) -> { c.add(u); return c; }, list -> Array.wrap(list.toArray())); }
@Override public Tuple2<Array<T>, Array<T>> splitAtInclusive(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); for (int i = 0; i < length(); i++) { final T value = get(i); if (predicate.test(value)) { if (i == length() - 1) { return Tuple.of(this, empty()); } else { return Tuple.of(take(i + 1), drop(i + 1)); } } } return Tuple.of(this, empty()); }
@Override public <U> Array<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); if (isEmpty()) { return empty(); } else { final java.util.List<U> list = new ArrayList<>(); for (T t : this) { for (U u : mapper.apply(t)) { list.add(u); } } return wrap(toArray(list)); } }
@Override public Array<T> removeLast(Predicate<T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); int found = -1; for (int i = length() - 1; i >= 0; i--) { final T value = get(i); if (predicate.test(value)) { found = i; break; } } if (found < 0) { return this; } else { return removeAt(found); } }
@Override public <T1, T2> Tuple2<Array<T1>, Array<T2>> unzip( Function<? super T, Tuple2<? extends T1, ? extends T2>> unzipper) { Objects.requireNonNull(unzipper, "unzipper is null"); if (isEmpty()) { return Tuple.of(empty(), empty()); } else { final Object[] xs = new Object[length()]; final Object[] ys = new Object[length()]; for (int i = 0; i < length(); i++) { final Tuple2<? extends T1, ? extends T2> t = unzipper.apply(get(i)); xs[i] = t._1; ys[i] = t._2; } return Tuple.of(wrap(xs), wrap(ys)); } }
@Override public Array<T> filter(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); final java.util.List<T> list = new ArrayList<>(); for (T t : this) { if (predicate.test(t)) { list.add(t); } } if (list.isEmpty()) { return empty(); } else if (list.size() == size()) { return this; } else { return wrap(list.toArray()); } }
/** * Returns an Array containing {@code n} values supplied by a given Supplier {@code s}. * * @param <T> Component type of the Array * @param n The number of elements in the Array * @param s The Supplier computing element values * @return An Array of size {@code n}, where each element contains the result supplied by {@code * s}. * @throws NullPointerException if {@code s} is null */ public static <T> Array<T> fill(int n, Supplier<? extends T> s) { Objects.requireNonNull(s, "s is null"); return Collections.fill(n, s, empty(), Array::of); }
/** * Transforms this {@code Array}. * * @param f A transformation * @param <U> Type of transformation result * @return An instance of type {@code U} * @throws NullPointerException if {@code f} is null */ public <U> U transform(Function<? super Array<T>, ? extends U> f) { Objects.requireNonNull(f, "f is null"); return f.apply(this); }
@Override public <U> Array<Tuple2<T, U>> zipAll(Iterable<? extends U> that, T thisElem, U thatElem) { Objects.requireNonNull(that, "that is null"); return ofAll(iterator().zipAll(that, thisElem, thatElem)); }
@Override public int hashCode() { return Objects.hash(delegate); }
/** * Creates a Array of the given elements. * * <p>The resulting Array has the same iteration order as the given iterable of elements if the * iteration order of the elements is stable. * * @param <T> Component type of the Array. * @param elements An Iterable of elements. * @return A Array containing the given elements in the same order. * @throws NullPointerException if {@code elements} is null */ @SuppressWarnings("unchecked") public static <T> Array<T> ofAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); return elements instanceof Array ? (Array<T>) elements : wrap(toArray(elements)); }
/** * Creates a Array that contains the elements of the given {@link java.util.stream.Stream}. * * @param javaStream A {@link java.util.stream.Stream} * @param <T> Component type of the Stream. * @return A Array containing the given elements in the same order. */ static <T> Array<T> ofAll(java.util.stream.Stream<? extends T> javaStream) { Objects.requireNonNull(javaStream, "javaStream is null"); return wrap(javaStream.toArray()); }
@Override public Array<T> dropUntil(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); return dropWhile(predicate.negate()); }
@Override public <U> Array<T> distinctBy(Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor, "keyExtractor is null"); final java.util.Set<U> seen = new java.util.HashSet<>(); return filter(t -> seen.add(keyExtractor.apply(t))); }
@Override public Array<T> distinctBy(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); final java.util.Set<T> seen = new java.util.TreeSet<>(comparator); return filter(seen::add); }
/** * Returns an Array containing {@code n} values of a given Function {@code f} over a range of * integer values from 0 to {@code n - 1}. * * @param <T> Component type of the Array * @param n The number of elements in the Array * @param f The Function computing element values * @return An Array consisting of elements {@code f(0),f(1), ..., f(n - 1)} * @throws NullPointerException if {@code f} is null */ public static <T> Array<T> tabulate(int n, Function<? super Integer, ? extends T> f) { Objects.requireNonNull(f, "f is null"); return Collections.tabulate(n, f, empty(), Array::of); }