Ejemplo n.º 1
1
 /**
  * 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));
 }
Ejemplo n.º 2
1
 /**
  * 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));
 }
Ejemplo n.º 3
1
 /**
  * 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));
 }
Ejemplo n.º 4
1
 /**
  * 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));
 }
Ejemplo n.º 5
1
 /**
  * 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));
 }
Ejemplo n.º 6
1
 /**
  * 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));
 }
Ejemplo n.º 7
0
 @Override
 public Array<T> peek(Consumer<? super T> action) {
   Objects.requireNonNull(action, "action is null");
   if (!isEmpty()) {
     action.accept(head());
   }
   return this;
 }
Ejemplo n.º 8
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;
 }
Ejemplo n.º 9
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;
 }
Ejemplo n.º 10
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);
 }
Ejemplo n.º 11
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));
 }
Ejemplo n.º 12
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();
 }
Ejemplo n.º 13
0
 @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;
 }
Ejemplo n.º 14
0
 @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);
   }
 }
Ejemplo n.º 15
0
 @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()));
 }
Ejemplo n.º 16
0
 @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());
 }
Ejemplo n.º 17
0
 @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));
   }
 }
Ejemplo n.º 18
0
 @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);
   }
 }
Ejemplo n.º 19
0
 @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));
   }
 }
Ejemplo n.º 20
0
 @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());
   }
 }
Ejemplo n.º 21
0
 /**
  * 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);
 }
Ejemplo n.º 22
0
 /**
  * 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);
 }
Ejemplo n.º 23
0
 @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));
 }
Ejemplo n.º 24
0
 @Override
 public int hashCode() {
   return Objects.hash(delegate);
 }
Ejemplo n.º 25
0
 /**
  * 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));
 }
Ejemplo n.º 26
0
 /**
  * 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());
 }
Ejemplo n.º 27
0
 @Override
 public Array<T> dropUntil(Predicate<? super T> predicate) {
   Objects.requireNonNull(predicate, "predicate is null");
   return dropWhile(predicate.negate());
 }
Ejemplo n.º 28
0
 @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)));
 }
Ejemplo n.º 29
0
 @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);
 }
Ejemplo n.º 30
0
 /**
  * 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);
 }