/** * Creates a Tree of the given elements. * * @param <T> Component type of the List. * @param values Zero or more values. * @return A Tree containing the given values. * @throws NullPointerException if {@code values} is null */ @SuppressWarnings("varargs") @SafeVarargs static <T> Tree<T> of(T... values) { Objects.requireNonNull(values, "values is null"); List<T> list = List.of(values); return list.isEmpty() ? Empty.instance() : new Node<>(list.head(), list.tail().map(Tree::of)); }
/** * Creates a Tree of the given elements. * * <p>If the given iterable is a tree, it is returned as result. if the iteration order of the * elements is stable. * * @param <T> Component type of the List. * @param iterable An Iterable of elements. * @return A list containing the given elements in the same order. * @throws NullPointerException if {@code elements} is null */ @SuppressWarnings("unchecked") static <T> Tree<T> ofAll(Iterable<? extends T> iterable) { Objects.requireNonNull(iterable, "iterable is null"); if (iterable instanceof Tree) { return (Tree<T>) iterable; } else { final List<T> list = List.ofAll(iterable); return list.isEmpty() ? Empty.instance() : new Node<>(list.head(), list.tail().map(Tree::of)); } }
@Override public Tuple2<Vector<T>, Vector<T>> partition(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); final java.util.List<T> left = new ArrayList<>(), right = new ArrayList<>(); for (int i = 0; i < length(); i++) { T t = get(i); (predicate.test(t) ? left : right).add(t); } return Tuple.of(Vector.ofAll(left), Vector.ofAll(right)); }
@SuppressWarnings("unchecked") @Override default <C> Map<C, Seq<T>> groupBy(Function<? super T, ? extends C> classifier) { Objects.requireNonNull(classifier, "classifier is null"); if (isEmpty()) { return HashMap.empty(); } else { return (Map<C, Seq<T>>) traverse().groupBy(classifier); } }
@Override public Vector<T> retainAll(java.lang.Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); final Vector<T> keeped = Vector.ofAll(elements).distinct(); HashArrayMappedTrie<Integer, T> result = HashArrayMappedTrie.empty(); for (T element : this) { if (keeped.contains(element)) { result = result.put(result.size(), element); } } return result.isEmpty() ? empty() : new Vector<>(result); }
protected FilterJoinNode( Class joinClass, String[] joinColumns, String column, FilterExpress express, Serializable value) { Objects.requireNonNull(joinClass); Objects.requireNonNull(joinColumns); this.joinClass = joinClass; this.joinColumns = joinColumns; this.column = column; this.express = express; this.value = value; }
/** * Constructs a rose tree branch. * * @param value A value. * @param children A non-empty list of children. * @throws NullPointerException if children is null * @throws IllegalArgumentException if children is empty */ public Node(T value, List<Node<T>> children) { Objects.requireNonNull(children, "children is null"); this.value = value; this.children = children; this.size = Lazy.of(() -> 1 + children.foldLeft(0, (acc, child) -> acc + child.length())); this.hashCode = 31 * 31 + 31 * Objects.hashCode(value) + Objects.hashCode(children); }
@Override default Tree<T> peek(Consumer<? super T> action) { Objects.requireNonNull(action, "action is null"); if (!isEmpty()) { action.accept(head()); } return this; }
@Override public Vector<T> replaceAll(UnaryOperator<T> operator) { Objects.requireNonNull(operator, "operator is null"); HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { trie = trie.put(trie.size(), operator.apply(get(i))); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
/** * Creates a Vector of the given elements. * * @param <T> Component type of the Vector. * @param elements Zero or more elements. * @return A vector containing the given elements in the same order. * @throws NullPointerException if {@code elements} is null */ @SafeVarargs public static <T> Vector<T> of(T... elements) { Objects.requireNonNull(elements, "elements is null"); HashArrayMappedTrie<Integer, T> result = HashArrayMappedTrie.empty(); for (T element : elements) { result = result.put(result.size(), element); } return elements.length == 0 ? empty() : new Vector<>(result); }
@Override public <U> Vector<U> map(Function<? super T, ? extends U> mapper) { Objects.requireNonNull(mapper, "mapper is null"); HashArrayMappedTrie<Integer, U> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { trie = trie.put(i, mapper.apply(get(i))); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
@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 default <U> U foldRight(U zero, BiFunction<? super T, ? super U, ? extends U> f) { Objects.requireNonNull(f, "f is null"); if (isEmpty()) { return zero; } else { return iterator().foldRight(zero, f); } }
@Override default Seq<T> distinctBy(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); if (isEmpty()) { return Stream.empty(); } else { return traverse().distinctBy(comparator); } }
@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 default <U> Tree<Tuple2<T, U>> zip(Iterable<U> that) { Objects.requireNonNull(that, "that is null"); if (isEmpty()) { return Empty.instance(); } else { return Zip.apply((Node<T>) this, that.iterator()); } }
@Override default Seq<T> filter(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Stream.empty(); } else { return traverse().filter(predicate); } }
@Override default <U> Seq<T> distinctBy(Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor, "keyExtractor is null"); if (isEmpty()) { return Stream.empty(); } else { return traverse().distinctBy(keyExtractor); } }
@SuppressWarnings("unchecked") @Override default Tuple2<Seq<T>, Seq<T>> span(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); if (isEmpty()) { return Tuple.of(Stream.empty(), Stream.empty()); } else { return (Tuple2<Seq<T>, Seq<T>>) traverse().span(predicate); } }
@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 Vector<T> removeLast(Predicate<T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = length() - 1; i >= 0; i--) { if (predicate.test(get(i))) { return removeAt(i); } } return this; }
private static int testRequireNonNull() { int errors = 0; final String RNN_1 = "1-arg requireNonNull"; final String RNN_2 = "2-arg requireNonNull"; final String RNN_3 = "Supplier requireNonNull"; Function<String, String> rnn1 = s -> Objects.requireNonNull(s); Function<String, String> rnn2 = s -> Objects.requireNonNull(s, "trousers"); Function<String, String> rnn3 = s -> Objects.requireNonNull(s, () -> "trousers"); errors += testRNN_NonNull(rnn1, RNN_1); errors += testRNN_NonNull(rnn2, RNN_2); errors += testRNN_NonNull(rnn3, RNN_3); errors += testRNN_Null(rnn1, RNN_1, null); errors += testRNN_Null(rnn2, RNN_2, "trousers"); errors += testRNN_Null(rnn3, RNN_3, "trousers"); return errors; }
@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; }
@SuppressWarnings("unchecked") @Override default <T1, T2, T3> Tuple3<Tree<T1>, Tree<T2>, Tree<T3>> unzip3( Function<? super T, Tuple3<? extends T1, ? extends T2, ? extends T3>> unzipper) { Objects.requireNonNull(unzipper, "unzipper is null"); if (isEmpty()) { return Tuple.of(Empty.instance(), Empty.instance(), Empty.instance()); } else { return (Tuple3<Tree<T1>, Tree<T2>, Tree<T3>>) (Object) Unzip.apply3((Node<T>) this, unzipper); } }
@Override public <U> Vector<Tuple2<T, U>> zip(java.lang.Iterable<U> that) { Objects.requireNonNull(that, "that is null"); HashArrayMappedTrie<Integer, Tuple2<T, U>> result = HashArrayMappedTrie.empty(); Iterator<T> list1 = iterator(); java.util.Iterator<U> list2 = that.iterator(); while (list1.hasNext() && list2.hasNext()) { result = result.put(result.size(), Tuple.of(list1.next(), list2.next())); } return result.isEmpty() ? empty() : new Vector<>(result); }
/** * Creates a Vector of the given elements. * * <p>The resulting vector 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 Vector. * @param elements An java.lang.Iterable of elements. * @return A vector containing the given elements in the same order. * @throws NullPointerException if {@code elements} is null */ @SuppressWarnings("unchecked") public static <T> Vector<T> ofAll(java.lang.Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (elements instanceof Vector) { return (Vector<T>) elements; } else { HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (T element : elements) { trie = trie.put(trie.size(), element); } return new Vector<>(trie); } }
@Override public Vector<T> takeWhile(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { T value = get(i); if (!predicate.test(value)) { break; } trie = trie.put(i, get(i)); } return trie.size() == length() ? this : trie.isEmpty() ? empty() : new Vector<>(trie); }
@Override public <U> Vector<Tuple2<T, U>> zipAll(java.lang.Iterable<U> that, T thisElem, U thatElem) { Objects.requireNonNull(that, "that is null"); HashArrayMappedTrie<Integer, Tuple2<T, U>> result = HashArrayMappedTrie.empty(); Iterator<T> list1 = iterator(); java.util.Iterator<U> list2 = that.iterator(); while (list1.hasNext() || list2.hasNext()) { final T elem1 = list1.hasNext() ? list1.next() : thisElem; final U elem2 = list2.hasNext() ? list2.next() : thatElem; result = result.put(result.size(), Tuple.of(elem1, elem2)); } return result.isEmpty() ? empty() : new Vector<>(result); }
@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())); }