Example #1
1
 /**
  * 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));
 }
Example #2
1
 /**
  * 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));
   }
 }
Example #3
1
 @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));
 }
Example #4
1
 @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);
   }
 }
 private static int testHashCode() {
   int errors = 0;
   errors += (Objects.hashCode(null) == 0) ? 0 : 1;
   String s = "42";
   errors += (Objects.hashCode(s) == s.hashCode()) ? 0 : 1;
   return errors;
 }
Example #6
0
 /**
  * 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);
 }
 private static int testToString2() {
   int errors = 0;
   String s = "not the default";
   errors += (s.equals(Objects.toString(null, s))) ? 0 : 1;
   errors += (s.equals(Objects.toString(s, "another string"))) ? 0 : 1;
   return errors;
 }
 private static int testToString() {
   int errors = 0;
   errors += ("null".equals(Objects.toString(null))) ? 0 : 1;
   String s = "Some string";
   errors += (s.equals(Objects.toString(s))) ? 0 : 1;
   return errors;
 }
  private static int testNonNull() {
    int errors = 0;

    errors += Objects.nonNull(null) ? 1 : 0;
    errors += Objects.nonNull(Objects.class) ? 0 : 1;

    return errors;
  }
  private static int testHash() {
    int errors = 0;

    Object[] data = new String[] {"perfect", "ham", "THC"};

    errors += ((Objects.hash((Object[]) null) == 0) ? 0 : 1);

    errors += (Objects.hash("perfect", "ham", "THC") == Arrays.hashCode(data)) ? 0 : 1;

    return errors;
  }
Example #11
0
 @Override
 public boolean equals(Object o) {
   if (o == this) {
     return true;
   } else if (o instanceof Node) {
     final Node<?> that = (Node<?>) o;
     return Objects.equals(this.getValue(), that.getValue())
         && Objects.equals(this.getChildren(), that.getChildren());
   } else {
     return false;
   }
 }
Example #12
0
 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;
 }
  private static int testDeepEquals() {
    int errors = 0;
    Object[] values = {
      null,
      null, // Change to values later
      new byte[] {(byte) 1},
      new short[] {(short) 1},
      new int[] {1},
      new long[] {1L},
      new char[] {(char) 1},
      new float[] {1.0f},
      new double[] {1.0d},
      new String[] {"one"}
    };
    values[1] = values;

    for (int i = 0; i < values.length; i++)
      for (int j = 0; j < values.length; j++) {
        boolean expected = (i == j);
        Object a = values[i];
        Object b = values[j];
        boolean result = Objects.deepEquals(a, b);
        if (result != expected) {
          errors++;
          System.err.printf(
              "When equating %s to %s, got %b instead of %b%n.", a, b, result, expected);
        }
      }

    return errors;
  }
Example #14
0
 @Override
 default Tree<T> peek(Consumer<? super T> action) {
   Objects.requireNonNull(action, "action is null");
   if (!isEmpty()) {
     action.accept(head());
   }
   return this;
 }
Example #15
0
 @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);
   }
 }
Example #16
0
 @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);
   }
 }
Example #17
0
 @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);
   }
 }
Example #18
0
 @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);
 }
Example #19
0
 /**
  * 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);
 }
Example #20
0
 @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));
 }
Example #21
0
 @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);
 }
Example #22
0
 @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;
 }
Example #23
0
 @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;
 }
Example #24
0
 @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);
 }
Example #25
0
 @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);
   }
 }
 private static int compareTest(String a, String b, int expected) {
   int errors = 0;
   int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);
   if (Integer.signum(result) != Integer.signum(expected)) {
     errors++;
     System.err.printf("When comparing %s to %s, got %d instead of %d%n.", a, b, result, expected);
   }
   return errors;
 }
Example #27
0
 @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());
   }
 }
Example #28
0
 @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);
   }
 }
Example #29
0
 @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();
 }
Example #30
0
 @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);
   }
 }