Skip to content

Commit

Permalink
1.5 - Add assertNotClosed check to Streams
Browse files Browse the repository at this point in the history
  • Loading branch information
landawn committed Feb 16, 2019
1 parent 40ad55b commit cafe912
Show file tree
Hide file tree
Showing 34 changed files with 1,042 additions and 2 deletions.
Binary file modified lib/abacus-util-1.5.jar
Binary file not shown.
Binary file modified lib/abacus-util-jdk7-1.5.jar
Binary file not shown.
55 changes: 55 additions & 0 deletions src/com/landawn/abacus/util/ExceptionalStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,7 @@ private void init() throws E {

public void forEach(Try.Consumer<? super T, ? extends E> action) throws E {
N.checkArgNotNull(action, "action");
assertNotClosed();

try {
while (elements.hasNext()) {
Expand All @@ -1572,6 +1573,8 @@ public void forEach(Try.Consumer<? super T, ? extends E> action) throws E {
}

public Optional<T> min(Comparator<? super T> comparator) throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return Optional.empty();
Expand Down Expand Up @@ -1600,6 +1603,7 @@ public Optional<T> min(Comparator<? super T> comparator) throws E {

public Optional<T> minBy(final Function<? super T, ? extends Comparable> keyExtractor) throws E {
N.checkArgNotNull(keyExtractor, "keyExtractor");
assertNotClosed();

try {
final Comparator<? super T> comparator = Fn.comparingBy(keyExtractor);
Expand All @@ -1611,6 +1615,8 @@ public Optional<T> minBy(final Function<? super T, ? extends Comparable> keyExtr
}

public Optional<T> max(Comparator<? super T> comparator) throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return Optional.empty();
Expand Down Expand Up @@ -1646,6 +1652,7 @@ public Optional<T> max(Comparator<? super T> comparator) throws E {

public Optional<T> maxBy(final Function<? super T, ? extends Comparable> keyExtractor) throws E {
N.checkArgNotNull(keyExtractor, "keyExtractor");
assertNotClosed();

try {
final Comparator<? super T> comparator = Fn.comparingBy(keyExtractor);
Expand All @@ -1658,6 +1665,7 @@ public Optional<T> maxBy(final Function<? super T, ? extends Comparable> keyExtr

public boolean anyMatch(final Try.Predicate<? super T, ? extends E> predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
assertNotClosed();

try {
while (elements.hasNext()) {
Expand All @@ -1674,6 +1682,7 @@ public boolean anyMatch(final Try.Predicate<? super T, ? extends E> predicate) t

public boolean allMatch(final Try.Predicate<? super T, ? extends E> predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
assertNotClosed();

try {
while (elements.hasNext()) {
Expand All @@ -1690,6 +1699,7 @@ public boolean allMatch(final Try.Predicate<? super T, ? extends E> predicate) t

public boolean noneMatch(final Try.Predicate<? super T, ? extends E> predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
assertNotClosed();

try {
while (elements.hasNext()) {
Expand All @@ -1706,6 +1716,7 @@ public boolean noneMatch(final Try.Predicate<? super T, ? extends E> predicate)

public Optional<T> findFirst(final Try.Predicate<? super T, ? extends E> predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
assertNotClosed();

try {
while (elements.hasNext()) {
Expand All @@ -1724,6 +1735,7 @@ public Optional<T> findFirst(final Try.Predicate<? super T, ? extends E> predica

public Optional<T> findLast(final Try.Predicate<? super T, ? extends E> predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
assertNotClosed();

try {
if (elements.hasNext() == false) {
Expand All @@ -1750,6 +1762,8 @@ public Optional<T> findLast(final Try.Predicate<? super T, ? extends E> predicat
}

public Optional<T> first() throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return Optional.empty();
Expand All @@ -1762,6 +1776,8 @@ public Optional<T> first() throws E {
}

public Optional<T> last() throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return Optional.empty();
Expand All @@ -1780,6 +1796,8 @@ public Optional<T> last() throws E {
}

public Object[] toArray() throws E {
assertNotClosed();

try {
return toList().toArray();
} finally {
Expand All @@ -1789,6 +1807,7 @@ public Object[] toArray() throws E {

public <A> A[] toArray(IntFunction<A[]> generator) throws E {
N.checkArgNotNull(generator, "generator");
assertNotClosed();

try {
final List<T> list = toList();
Expand All @@ -1800,6 +1819,8 @@ public <A> A[] toArray(IntFunction<A[]> generator) throws E {
}

public List<T> toList() throws E {
assertNotClosed();

try {
final List<T> result = new ArrayList<>();

Expand All @@ -1814,6 +1835,8 @@ public List<T> toList() throws E {
}

public Set<T> toSet() throws E {
assertNotClosed();

try {
final Set<T> result = new HashSet<>();

Expand All @@ -1829,6 +1852,7 @@ public Set<T> toSet() throws E {

public <C extends Collection<T>> C toCollection(final Supplier<C> supplier) throws E {
N.checkArgNotNull(supplier, "supplier");
assertNotClosed();

try {
final C result = supplier.get();
Expand Down Expand Up @@ -1911,6 +1935,7 @@ public <K, V, M extends Map<K, V>> M toMap(final Try.Function<? super T, ? exten
N.checkArgNotNull(valueMapper, "valueMapper");
N.checkArgNotNull(mergeFunction, "mergeFunction");
N.checkArgNotNull(mapFactory, "mapFactory");
assertNotClosed();

try {
final M result = mapFactory.get();
Expand Down Expand Up @@ -1981,6 +2006,7 @@ public <K, V, A, D, M extends Map<K, D>> M toMap(final Try.Function<? super T, ?
N.checkArgNotNull(valueMapper, "valueMapper");
N.checkArgNotNull(downstream, "downstream");
N.checkArgNotNull(mapFactory, "mapFactory");
assertNotClosed();

try {
final Supplier<A> downstreamSupplier = downstream.supplier();
Expand Down Expand Up @@ -2057,6 +2083,7 @@ public <K, V, M extends Map<K, List<V>>> M groupTo(Try.Function<? super T, ? ext
N.checkArgNotNull(keyExtractor, "keyExtractor");
N.checkArgNotNull(valueMapper, "valueMapper");
N.checkArgNotNull(mapFactory, "mapFactory");
assertNotClosed();

try {
final M result = mapFactory.get();
Expand All @@ -2081,6 +2108,8 @@ public <K, V, M extends Map<K, List<V>>> M groupTo(Try.Function<? super T, ? ext
}

public long count() throws E {
assertNotClosed();

try {
return elements.count();
} finally {
Expand All @@ -2095,6 +2124,8 @@ public long count() throws E {
* @throws E
*/
public Optional<T> onlyOne() throws DuplicatedResultException, E {
assertNotClosed();

try {
Optional<T> result = Optional.empty();

Expand All @@ -2113,6 +2144,8 @@ public Optional<T> onlyOne() throws DuplicatedResultException, E {
}

public OptionalInt sumInt(Try.ToIntFunction<T, E> func) throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return OptionalInt.empty();
Expand All @@ -2131,6 +2164,8 @@ public OptionalInt sumInt(Try.ToIntFunction<T, E> func) throws E {
}

public OptionalLong sumLong(Try.ToLongFunction<T, E> func) throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return OptionalLong.empty();
Expand All @@ -2149,6 +2184,8 @@ public OptionalLong sumLong(Try.ToLongFunction<T, E> func) throws E {
}

public OptionalDouble sumDouble(Try.ToDoubleFunction<T, E> func) throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return OptionalDouble.empty();
Expand All @@ -2167,6 +2204,8 @@ public OptionalDouble sumDouble(Try.ToDoubleFunction<T, E> func) throws E {
}

public OptionalDouble averageInt(Try.ToIntFunction<T, E> func) throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return OptionalDouble.empty();
Expand All @@ -2187,6 +2226,8 @@ public OptionalDouble averageInt(Try.ToIntFunction<T, E> func) throws E {
}

public OptionalDouble averageLong(Try.ToLongFunction<T, E> func) throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return OptionalDouble.empty();
Expand All @@ -2207,6 +2248,8 @@ public OptionalDouble averageLong(Try.ToLongFunction<T, E> func) throws E {
}

public OptionalDouble averageDouble(Try.ToDoubleFunction<T, E> func) throws E {
assertNotClosed();

try {
if (elements.hasNext() == false) {
return OptionalDouble.empty();
Expand All @@ -2226,6 +2269,7 @@ public OptionalDouble averageDouble(Try.ToDoubleFunction<T, E> func) throws E {

public T reduce(T identity, Try.BinaryOperator<T, ? extends E> accumulator) throws E {
N.checkArgNotNull(accumulator, "accumulator");
assertNotClosed();

try {
T result = identity;
Expand All @@ -2242,6 +2286,7 @@ public T reduce(T identity, Try.BinaryOperator<T, ? extends E> accumulator) thro

public Optional<T> reduce(Try.BinaryOperator<T, ? extends E> accumulator) throws E {
N.checkArgNotNull(accumulator, "accumulator");
assertNotClosed();

try {
if (elements.hasNext() == false) {
Expand All @@ -2263,6 +2308,7 @@ public Optional<T> reduce(Try.BinaryOperator<T, ? extends E> accumulator) throws
public <R> R collect(Supplier<R> supplier, final Try.BiConsumer<R, ? super T, ? extends E> accumulator) throws E {
N.checkArgNotNull(supplier, "supplier");
N.checkArgNotNull(accumulator, "accumulator");
assertNotClosed();

try {
final R result = supplier.get();
Expand All @@ -2282,6 +2328,7 @@ public <R, RR> RR collect(Supplier<R> supplier, final Try.BiConsumer<R, ? super
N.checkArgNotNull(supplier, "supplier");
N.checkArgNotNull(accumulator, "accumulator");
N.checkArgNotNull(finisher, "finisher");
assertNotClosed();

try {
final R result = supplier.get();
Expand All @@ -2298,6 +2345,7 @@ public <R, RR> RR collect(Supplier<R> supplier, final Try.BiConsumer<R, ? super

public <R, A> R collect(final Collector<? super T, A, R> collector) throws E {
N.checkArgNotNull(collector, "collector");
assertNotClosed();

try {
final A container = collector.supplier().get();
Expand All @@ -2316,6 +2364,7 @@ public <R, A> R collect(final Collector<? super T, A, R> collector) throws E {
public <R, RR, A> RR collectAndThen(final Collector<? super T, A, R> collector, final Try.Function<? super R, ? extends RR, E> func) throws E {
N.checkArgNotNull(collector, "collector");
N.checkArgNotNull(func, "func");
assertNotClosed();

try {
final A container = collector.supplier().get();
Expand Down Expand Up @@ -2504,6 +2553,12 @@ public synchronized void close() throws E {
}
}

void assertNotClosed() {
if (isClosed) {
throw new IllegalStateException("This stream has been closed");
}
}

static <T, E extends Exception> ExceptionalStream<T, E> newStream(final ExceptionalIterator<T, E> iter) {
return new ExceptionalStream<>(iter, null);
}
Expand Down
6 changes: 4 additions & 2 deletions src/com/landawn/abacus/util/URLEncodedUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,9 @@ public static String encode(final String url, final Object parameters) {
return encode(url, parameters, Charsets.UTF_8);
}

@SuppressWarnings("rawtypes")
public static String encode(final String url, final Object parameters, final Charset charset) {
if (parameters == null) {
if (parameters == null || (parameters instanceof Map && ((Map) parameters).isEmpty())) {
return url;
}

Expand All @@ -345,8 +346,9 @@ public static void encode(final StringBuilder output, final Object parameters) {
encode(output, parameters, Charsets.UTF_8);
}

@SuppressWarnings("rawtypes")
public static void encode(final StringBuilder output, final Object parameters, final Charset charset) {
if (parameters == null) {
if (parameters == null || (parameters instanceof Map && ((Map) parameters).isEmpty())) {
return;
}

Expand Down
14 changes: 14 additions & 0 deletions src/com/landawn/abacus/util/stream/AbstractByteStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,8 @@ public ByteMatrix toMatrix() {

@Override
public OptionalByte first() {
assertNotClosed();

try {
final ByteIterator iter = this.iteratorEx();

Expand All @@ -896,6 +898,8 @@ public OptionalByte first() {

@Override
public OptionalByte last() {
assertNotClosed();

try {
final ByteIterator iter = this.iteratorEx();

Expand All @@ -917,6 +921,8 @@ public OptionalByte last() {

@Override
public OptionalByte onlyOne() throws DuplicatedResultException {
assertNotClosed();

try {
final ByteIterator iter = this.iteratorEx();

Expand All @@ -940,6 +946,8 @@ public <E extends Exception> OptionalByte findAny(final Try.BytePredicate<E> pre
@Override
public <E extends Exception, E2 extends Exception> OptionalByte findFirstOrLast(Try.BytePredicate<E> predicateForFirst,
Try.BytePredicate<E> predicateForLast) throws E, E2 {
assertNotClosed();

try {
final ByteIteratorEx iter = iteratorEx();
MutableByte last = null;
Expand Down Expand Up @@ -967,6 +975,8 @@ public <E extends Exception, E2 extends Exception> OptionalByte findFirstOrLast(

@Override
public Optional<Map<Percentage, Byte>> percentiles() {
assertNotClosed();

try {
final byte[] a = sorted().toArray();

Expand All @@ -982,6 +992,8 @@ public Optional<Map<Percentage, Byte>> percentiles() {

@Override
public Pair<ByteSummaryStatistics, Optional<Map<Percentage, Byte>>> summarizeAndPercentiles() {
assertNotClosed();

try {
final byte[] a = sorted().toArray();

Expand All @@ -997,6 +1009,8 @@ public Pair<ByteSummaryStatistics, Optional<Map<Percentage, Byte>>> summarizeAnd

@Override
public String join(final CharSequence delimiter, final CharSequence prefix, final CharSequence suffix) {
assertNotClosed();

try {
final Joiner joiner = Joiner.with(delimiter, prefix, suffix).reuseCachedBuffer(true);
final ByteIteratorEx iter = this.iteratorEx();
Expand Down
Loading

0 comments on commit cafe912

Please sign in to comment.