Skip to content

Commit

Permalink
1.5 - Refactoring Stream
Browse files Browse the repository at this point in the history
* Change the stream implementation: close it after execution(any terminal method is executed).

* Remove `Stream.head/tail/headAndTail`.

* Remove `Stream.try` because the all streams will be automatically closed after execution.

* Rename `Stream.of(File/Path...)` to `Stream.lines(File/Path...)`.
  • Loading branch information
landawn committed Feb 15, 2019
1 parent a62febf commit 40ad55b
Show file tree
Hide file tree
Showing 81 changed files with 11,743 additions and 6,044 deletions.
2 changes: 1 addition & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<classpathentry kind="lib" path="lib/neo4j-ogm-core-2.0.0-M03.jar"/>
<classpathentry kind="lib" path="lib/neo4j-ogm-http-driver-2.0.0-M03.jar"/>
<classpathentry kind="lib" path="lib/javaee-api-7.0.jar"/>
<classpathentry kind="lib" path="lib/abacus-util-1.3.33.jar" sourcepath="C:/Users/haiyangl/AppData/Local/Temp/decompiler/source/abacus-util-1.3.29-sources.jar"/>
<classpathentry kind="lib" path="lib/abacus-util-1.5.jar" sourcepath="C:/Users/haiyangl/AppData/Local/Temp/decompiler/source/abacus-util-1.3.29-sources.jar"/>
<classpathentry kind="lib" path="lib/jackson-annotations-2.6.3.jar"/>
<classpathentry kind="lib" path="lib/okhttp-3.13.1.jar" sourcepath="C:/Users/haiyangl/AppData/Local/Temp/decompiler/source/okhttp-3.2.0-sources.jar"/>
<classpathentry kind="lib" path="lib/okhttp-urlconnection-3.2.0.jar"/>
Expand Down
13 changes: 13 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
### 1.5

* Change the stream implementation: close it after execution(any terminal method is executed).

* Remove `Stream.head/tail/headAndTail`.

* Remove `Stream.try` because the all streams will be automatically closed after execution.

* Rename `Stream.of(File/Path...)` to `Stream.lines(File/Path...)`.

* Improvements and bug fix.


### 1.3.33

* Refactoring `HttpClient/HttpRequest/OKHttpClient/HttpSettings/HttpHeaders`.
Expand Down
Binary file not shown.
Binary file not shown.
75 changes: 37 additions & 38 deletions src/com/landawn/abacus/util/AsyncSQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1704,99 +1704,98 @@ public DataSet call() throws Exception {
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> stream(final Class<T> targetClass, final String sql,
final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> stream(final Class<T> targetClass, final String sql, final Object... parameters) {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.stream(targetClass, sql, parameters);
}
});
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> stream(final Class<T> targetClass, final String sql,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> stream(final Class<T> targetClass, final String sql,
final StatementSetter statementSetter, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.stream(targetClass, sql, statementSetter, parameters);
}
});
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> stream(final Class<T> targetClass, final String sql,
final JdbcSettings jdbcSettings, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> stream(final Class<T> targetClass, final String sql, final JdbcSettings jdbcSettings,
final Object... parameters) {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.stream(targetClass, sql, jdbcSettings, parameters);
}
});
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> stream(final Class<T> targetClass, final String sql,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> stream(final Class<T> targetClass, final String sql,
final StatementSetter statementSetter, final JdbcSettings jdbcSettings, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.stream(targetClass, sql, statementSetter, jdbcSettings, parameters);
}
});
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> stream(final String sql,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> stream(final String sql,
final JdbcUtil.BiRecordGetter<T, RuntimeException> recordGetter, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.stream(sql, recordGetter, parameters);
}
});
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> stream(final String sql, final StatementSetter statementSetter,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> stream(final String sql, final StatementSetter statementSetter,
final JdbcUtil.BiRecordGetter<T, RuntimeException> recordGetter, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.stream(sql, statementSetter, recordGetter, parameters);
}
});
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> stream(final String sql,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> stream(final String sql,
final JdbcUtil.BiRecordGetter<T, RuntimeException> recordGetter, final JdbcSettings jdbcSettings, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.stream(sql, recordGetter, jdbcSettings, parameters);
}
});
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> stream(final String sql, final StatementSetter statementSetter,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> stream(final String sql, final StatementSetter statementSetter,
final JdbcUtil.BiRecordGetter<T, RuntimeException> recordGetter, final JdbcSettings jdbcSettings, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.stream(sql, statementSetter, recordGetter, jdbcSettings, parameters);
}
});
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> streamAll(final Class<T> targetClass, final String sql,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> streamAll(final Class<T> targetClass, final String sql,
final JdbcSettings jdbcSettings, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.streamAll(targetClass, sql, jdbcSettings, parameters);
}
});
Expand All @@ -1812,22 +1811,22 @@ public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
* @return
*/
@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> streamAll(final Class<T> targetClass, final String sql,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> streamAll(final Class<T> targetClass, final String sql,
final StatementSetter statementSetter, final JdbcSettings jdbcSettings, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.streamAll(targetClass, sql, statementSetter, jdbcSettings, parameters);
}
});
}

@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> streamAll(final Class<T> targetClass, final List<String> sqls,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> streamAll(final Class<T> targetClass, final List<String> sqls,
final JdbcSettings jdbcSettings, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.streamAll(targetClass, sqls, jdbcSettings, parameters);
}
});
Expand All @@ -1843,11 +1842,11 @@ public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
* @return
*/
@SafeVarargs
public final <T> ContinuableFuture<Try<ExceptionalStream<T, SQLException>>> streamAll(final Class<T> targetClass, final List<String> sqls,
public final <T> ContinuableFuture<ExceptionalStream<T, SQLException>> streamAll(final Class<T> targetClass, final List<String> sqls,
final StatementSetter statementSetter, final JdbcSettings jdbcSettings, final Object... parameters) {
return asyncExecutor.execute(new Callable<Try<ExceptionalStream<T, SQLException>>>() {
return asyncExecutor.execute(new Callable<ExceptionalStream<T, SQLException>>() {
@Override
public Try<ExceptionalStream<T, SQLException>> call() throws Exception {
public ExceptionalStream<T, SQLException> call() throws Exception {
return sqlExecutor.streamAll(targetClass, sqls, statementSetter, jdbcSettings, parameters);
}
});
Expand Down
88 changes: 88 additions & 0 deletions src/com/landawn/abacus/util/BooleanIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.NoSuchElementException;

import com.landawn.abacus.util.function.BooleanSupplier;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.stream.Stream;

/**
Expand Down Expand Up @@ -83,6 +84,93 @@ public BooleanList toList() {
};
}

/**
* Lazy evaluation.
*
* @param iteratorSupplier
* @return
*/
public static BooleanIterator of(final Supplier<? extends BooleanIterator> iteratorSupplier) {
N.checkArgNotNull(iteratorSupplier, "iteratorSupplier");

return new BooleanIterator() {
private BooleanIterator iter = null;
private boolean isInitialized = false;

@Override
public boolean hasNext() {
if (isInitialized == false) {
init();
}

return iter.hasNext();
}

@Override
public boolean nextBoolean() {
if (isInitialized == false) {
init();
}

return iter.nextBoolean();
}

private void init() {
if (isInitialized == false) {
isInitialized = true;
iter = iteratorSupplier.get();
}
}
};
}

/**
* Lazy evaluation.
*
* @param arraySupplier
* @return
*/
public static BooleanIterator oF(final Supplier<boolean[]> arraySupplier) {
N.checkArgNotNull(arraySupplier, "arraySupplier");

return new BooleanIterator() {
private boolean[] aar = null;
private int len = 0;
private int cur = 0;
private boolean isInitialized = false;

@Override
public boolean hasNext() {
if (isInitialized == false) {
init();
}

return cur < len;
}

@Override
public boolean nextBoolean() {
if (isInitialized == false) {
init();
}

if (cur >= len) {
throw new NoSuchElementException();
}

return aar[cur++];
}

private void init() {
if (isInitialized == false) {
isInitialized = true;
aar = arraySupplier.get();
len = N.len(aar);
}
}
};
}

/**
* Returns an infinite {@code BooleanIterator}.
*
Expand Down
Loading

0 comments on commit 40ad55b

Please sign in to comment.