Skip to content

Commit

Permalink
Added a few methods to the public API of collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Pap Lőrinc committed Jun 25, 2016
1 parent bf160e7 commit 3bff648
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 93 deletions.
27 changes: 10 additions & 17 deletions javaslang/src/main/java/javaslang/Lazy.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@

import javaslang.collection.Iterator;
import javaslang.collection.List;
import javaslang.collection.Seq;
import javaslang.collection.*;
import javaslang.control.Option;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.*;

/**
* Represents a lazy evaluated value. Compared to a Supplier, Lazy is memoizing, i.e. it evaluates only once and
* therefore is referential transparent.
*
* <p>
* <pre>
* <code>
* final Lazy&lt;Double&gt; l = Lazy.of(Math::random);
Expand All @@ -35,9 +28,9 @@
* l.get(); // = 0.123 (memoized)
* </code>
* </pre>
*
* <p>
* Since 2.0.0 you may also create a <em>real</em> lazy value (works only with interfaces):
*
* <p>
* <pre><code>final CharSequence chars = Lazy.val(() -&gt; "Yay!", CharSequence.class);</code></pre>
*
* @author Daniel Dietrich
Expand Down Expand Up @@ -68,7 +61,7 @@ private Lazy(Supplier<? extends T> supplier) {
* @return the given {@code lazy} instance as narrowed type {@code Lazy<T>}.
*/
@SuppressWarnings("unchecked")
static <T> Lazy<T> narrow(Lazy<? extends T> lazy) {
public static <T> Lazy<T> narrow(Lazy<? extends T> lazy) {
return (Lazy<T>) lazy;
}

Expand Down Expand Up @@ -127,7 +120,7 @@ public static <T> T val(Supplier<? extends T> supplier, Class<T> type) {
}

public Option<T> filter(Predicate<? super T> predicate) {
T v = get();
final T v = get();
return predicate.test(v) ? Option.some(v) : Option.none();
}

Expand Down
12 changes: 6 additions & 6 deletions javaslang/src/main/java/javaslang/collection/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static <T> Array<T> ofAll(Iterable<? extends T> elements) {
* @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) {
public static <T> Array<T> ofAll(java.util.stream.Stream<? extends T> javaStream) {
Objects.requireNonNull(javaStream, "javaStream is null");
return wrap(javaStream.toArray());
}
Expand Down Expand Up @@ -482,7 +482,7 @@ public static Array<Long> rangeClosedBy(long from, long toInclusive, long step)
* @return an Array with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T, U> Array<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> f) {
public static <T, U> Array<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> f) {
return Iterator.unfoldRight(seed, f).toArray();
}

Expand All @@ -509,7 +509,7 @@ static <T, U> Array<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? e
* @return an Array with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T, U> Array<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
public static <T, U> Array<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
return Iterator.unfoldLeft(seed, f).toArray();
}

Expand All @@ -536,7 +536,7 @@ static <T, U> Array<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? ex
* @return an Array with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T> Array<T> unfold(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends T>>> f) {
public static <T> Array<T> unfold(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends T>>> f) {
return Iterator.unfold(seed, f).toArray();
}

Expand Down Expand Up @@ -1052,13 +1052,13 @@ public <U> Array<U> scanLeft(U zero, BiFunction<? super U, ? super T, ? extends
new java.util.ArrayList<>(), (c, u) -> {
c.add(u);
return c;
}, list -> Array.<U> wrap(list.toArray()));
}, list -> Array.wrap(list.toArray()));
}

@Override
public <U> Array<U> scanRight(U zero, BiFunction<? super T, ? super U, ? extends U> operation) {
Objects.requireNonNull(operation, "operation is null");
return Collections.scanRight(this, zero, operation, List.empty(), List::prepend, list -> Array.<U> wrap(list.toJavaArray()));
return Collections.scanRight(this, zero, operation, List.empty(), List::prepend, list -> Array.wrap(list.toJavaArray()));
}

@Override
Expand Down
20 changes: 10 additions & 10 deletions javaslang/src/main/java/javaslang/collection/CharSeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public static CharSeq rangeClosedBy(char from, char toInclusive, int step) {
* @return a CharSeq with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T> CharSeq unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends Character, ? extends T>>> f) {
public static <T> CharSeq unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends Character, ? extends T>>> f) {
return CharSeq.ofAll(Iterator.unfoldRight(seed, f));
}

Expand All @@ -265,7 +265,7 @@ static <T> CharSeq unfoldRight(T seed, Function<? super T, Option<Tuple2<? exten
* @return a CharSeq with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T> CharSeq unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends Character>>> f) {
public static <T> CharSeq unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends Character>>> f) {
return CharSeq.ofAll(Iterator.unfoldLeft(seed, f));
}

Expand All @@ -292,7 +292,7 @@ static <T> CharSeq unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extend
* @return a CharSeq with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static CharSeq unfold(Character seed, Function<? super Character, Option<Tuple2<? extends Character, ? extends Character>>> f) {
public static CharSeq unfold(Character seed, Function<? super Character, Option<Tuple2<? extends Character, ? extends Character>>> f) {
return CharSeq.ofAll(Iterator.unfold(seed, f));
}

Expand Down Expand Up @@ -1839,7 +1839,7 @@ public int lastIndexOf(int ch, int fromIndex) {
* @param fromIndex the index to start the search from.
* @return {@code Some(index)} or {@code None} if not found.
*/
Option<Integer> lastIndexOfOption(int ch, int fromIndex) {
public Option<Integer> lastIndexOfOption(int ch, int fromIndex) {
return Collections.indexOption(lastIndexOf(ch, fromIndex));
}

Expand Down Expand Up @@ -1867,7 +1867,7 @@ public int indexOf(CharSeq str) {
* @param str the substring to search for.
* @return {@code Some(index)} or {@code None} if not found.
*/
Option<Integer> indexOfOption(CharSeq str) {
public Option<Integer> indexOfOption(CharSeq str) {
return Collections.indexOption(indexOf(str));
}

Expand Down Expand Up @@ -1899,7 +1899,7 @@ public int indexOf(CharSeq str, int fromIndex) {
* @param fromIndex the index from which to start the search.
* @return {@code Some(index)} or {@code None} if not found.
*/
Option<Integer> indexOfOption(CharSeq str, int fromIndex) {
public Option<Integer> indexOfOption(CharSeq str, int fromIndex) {
return Collections.indexOption(indexOf(str, fromIndex));
}

Expand Down Expand Up @@ -1928,7 +1928,7 @@ public int lastIndexOf(CharSeq str) {
* @param str the substring to search for.
* @return {@code Some(index)} or {@code None} if not found.
*/
Option<Integer> lastIndexOfOption(CharSeq str) {
public Option<Integer> lastIndexOfOption(CharSeq str) {
return Collections.indexOption(lastIndexOf(str));
}

Expand Down Expand Up @@ -1960,7 +1960,7 @@ public int lastIndexOf(CharSeq str, int fromIndex) {
* @param fromIndex the index to start the search from.
* @return {@code Some(index)} or {@code None} if not found.
*/
Option<Integer> lastIndexOfOption(CharSeq str, int fromIndex) {
public Option<Integer> lastIndexOfOption(CharSeq str, int fromIndex) {
return Collections.indexOption(lastIndexOf(str, fromIndex));
}

Expand Down Expand Up @@ -2500,12 +2500,12 @@ public char[] toCharArray() {
}

@FunctionalInterface
interface CharUnaryOperator {
public interface CharUnaryOperator {
char apply(char c);
}

@FunctionalInterface
interface CharFunction<R> {
public interface CharFunction<R> {
R apply(char c);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static <T> LinkedHashSet<T> ofAll(Iterable<? extends T> elements) {
* @param <T> Component type of the Stream.
* @return A LinkedHashSet containing the given elements in the same order.
*/
static <T> LinkedHashSet<T> ofAll(java.util.stream.Stream<? extends T> javaStream) {
public static <T> LinkedHashSet<T> ofAll(java.util.stream.Stream<? extends T> javaStream) {
Objects.requireNonNull(javaStream, "javaStream is null");
return ofAll(Iterator.ofAll(javaStream.iterator()));
}
Expand Down
8 changes: 4 additions & 4 deletions javaslang/src/main/java/javaslang/collection/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ static List<Long> rangeClosedBy(long from, long toInclusive, long step) {
* @return a list with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T,U> List<U> unfoldRight(T seed, Function<? super T,Option<Tuple2<? extends U, ? extends T>>> f) {
static <T, U> List<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> f) {
return Iterator.unfoldRight(seed, f).toList();
}

Expand All @@ -607,7 +607,7 @@ static <T,U> List<U> unfoldRight(T seed, Function<? super T,Option<Tuple2<? exte
* @return a list with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T,U> List<U> unfoldLeft(T seed, Function<? super T,Option<Tuple2<? extends T, ? extends U>>> f) {
static <T, U> List<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
return Iterator.unfoldLeft(seed, f).toList();
}

Expand All @@ -634,7 +634,7 @@ static <T,U> List<U> unfoldLeft(T seed, Function<? super T,Option<Tuple2<? exten
* @return a list with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T> List<T> unfold(T seed, Function<? super T,Option<Tuple2<? extends T, ? extends T>>> f) {
static <T> List<T> unfold(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends T>>> f) {
return Iterator.unfold(seed, f).toList();
}

Expand All @@ -646,7 +646,7 @@ default List<T> append(T element) {
@Override
default List<T> appendAll(Iterable<? extends T> elements) {
Objects.requireNonNull(elements, "elements is null");
return List.<T>ofAll(elements).prependAll(this);
return List.<T> ofAll(elements).prependAll(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static <T> PriorityQueue<T> empty(Comparator<? super T> comparator) {
* @param <T> Component type of the {@code PriorityQueue}.
* @return A {@code PriorityQueue<T>} Collector.
*/
static <T> Collector<T, ArrayList<T>, PriorityQueue<T>> collector() {
public static <T> Collector<T, ArrayList<T>, PriorityQueue<T>> collector() {
final Supplier<ArrayList<T>> supplier = ArrayList::new;
final BiConsumer<ArrayList<T>, T> accumulator = ArrayList::add;
final BinaryOperator<ArrayList<T>> combiner = (left, right) -> {
Expand Down Expand Up @@ -213,7 +213,7 @@ public static <T> PriorityQueue<T> ofAll(Comparator<? super T> comparator, java.
* @throws NullPointerException if {@code function} is null
*/
@GwtIncompatible
static <T> PriorityQueue<T> tabulate(int size, Function<? super Integer, ? extends T> function) {
public static <T> PriorityQueue<T> tabulate(int size, Function<? super Integer, ? extends T> function) {
Objects.requireNonNull(function, "function is null");
final Comparator<? super T> comparator = naturalComparator();
return Collections.tabulate(size, function, empty(comparator), values -> ofAll(comparator, List.of(values)));
Expand All @@ -228,9 +228,9 @@ static <T> PriorityQueue<T> tabulate(int size, Function<? super Integer, ? exten
* @return A {@link PriorityQueue} of size {@code size}, where each element contains the result supplied by {@code supplier}.
* @throws NullPointerException if {@code supplier} is null
*/
@SuppressWarnings("unchecked")
@GwtIncompatible
static <T> PriorityQueue<T> fill(int size, Supplier<? extends T> supplier) {
@SuppressWarnings("unchecked")
public static <T> PriorityQueue<T> fill(int size, Supplier<? extends T> supplier) {
Objects.requireNonNull(supplier, "supplier is null");
final Comparator<? super T> comparator = naturalComparator();
return Collections.fill(size, supplier, empty(comparator), values -> ofAll(comparator, List.of(values)));
Expand Down
10 changes: 5 additions & 5 deletions javaslang/src/main/java/javaslang/collection/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package javaslang.collection;

import javaslang.*;
import javaslang.control.*;
import javaslang.control.Option;

import java.util.*;
import java.util.function.*;
Expand Down Expand Up @@ -162,7 +162,7 @@ public static <T> Queue<T> ofAll(Iterable<? extends T> elements) {
* @param <T> Component type of the Stream.
* @return A Queue containing the given elements in the same order.
*/
static <T> Queue<T> ofAll(java.util.stream.Stream<? extends T> javaStream) {
public static <T> Queue<T> ofAll(java.util.stream.Stream<? extends T> javaStream) {
Objects.requireNonNull(javaStream, "javaStream is null");
return new Queue<>(List.ofAll(javaStream), List.empty());
}
Expand Down Expand Up @@ -525,7 +525,7 @@ public static Queue<Long> rangeClosedBy(long from, long toInclusive, long step)
* @return a Queue with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T,U> Queue<U> unfoldRight(T seed, Function<? super T,Option<Tuple2<? extends U, ? extends T>>> f) {
public static <T, U> Queue<U> unfoldRight(T seed, Function<? super T, Option<Tuple2<? extends U, ? extends T>>> f) {
return Iterator.unfoldRight(seed, f).toQueue();
}

Expand All @@ -552,7 +552,7 @@ static <T,U> Queue<U> unfoldRight(T seed, Function<? super T,Option<Tuple2<? ext
* @return a Queue with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T,U> Queue<U> unfoldLeft(T seed, Function<? super T,Option<Tuple2<? extends T, ? extends U>>> f) {
public static <T, U> Queue<U> unfoldLeft(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends U>>> f) {
return Iterator.unfoldLeft(seed, f).toQueue();
}

Expand All @@ -579,7 +579,7 @@ static <T,U> Queue<U> unfoldLeft(T seed, Function<? super T,Option<Tuple2<? exte
* @return a Queue with the values built up by the iteration
* @throws IllegalArgumentException if {@code f} is null
*/
static <T> Queue<T> unfold(T seed, Function<? super T,Option<Tuple2<? extends T, ? extends T>>> f) {
public static <T> Queue<T> unfold(T seed, Function<? super T, Option<Tuple2<? extends T, ? extends T>>> f) {
return Iterator.unfold(seed, f).toQueue();
}

Expand Down
Loading

0 comments on commit 3bff648

Please sign in to comment.