Skip to content

Commit

Permalink
1.5.9
Browse files Browse the repository at this point in the history
  Add Splitter.splitToStream.

  Rename StringUtil.join(Map....) to StringUtil.joinEntries(Map....).

  Remove Stream.split(String...).

  Improvements and bug fix.
  • Loading branch information
landawn committed Mar 25, 2019
1 parent 4373bca commit 1726220
Show file tree
Hide file tree
Showing 23 changed files with 899 additions and 646 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
### 1.5.9

* Add `Splitter.splitToStream`.

* Rename `StringUtil.join(Map....)` to `StringUtil.joinEntries(Map....)`.

* Remove `Stream.split(String...)`.

* Improvements and bug fix.


Expand Down
64 changes: 32 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,32 +183,32 @@ A general programming library in Java/Android. It's easy to learn and simple to
[RemoteExecutor](https://cdn.rawgit.com/landawn/AbacusUtil/master/docs/RemoteExecutor_view.html).

* More:
[Charsets](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/Charsets.html),
[Ascii](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/Ascii.html),
[CalendarUnit](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/CalendarUnit.html),
[NamingPolicy](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/NamingPolicy.html),
[Properties](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/Properties.html),
[PropertiesUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/PropertiesUtil.html),
[Wrapper](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/Wrapper.html),
[ArrayHashSet](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/ArrayHashSet.html),
[ArrayHashMap](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/ArrayHashMap.html),
[ClassUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/ClassUtil.html),
[EscapeUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/EscapeUtil.html),
[RegExUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/RegExUtil.html),
[FilenameUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/FilenameUtil.html),
[AWSJSONUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/AWSJSONUtil.html),
[AddrUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/AddrUtil.html),
[WSSecurityUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/WSSecurityUtil.html),
[EmailUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/EmailUtil.html),
[IEEE754rUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/IEEE754rUtil.html),
[Index](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/Index.html),
[Median](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/Median.html),
[f](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/f.html),
[Matth](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/Matth.html),
[SafeInitializer](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/SafeInitializer.html),
[Stopwatch](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/Stopwatch.html),
[RateLimiter](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/RateLimiter.html),
[Traverser](https://static.javadoc.io/com.landawn/abacus-util/1.5.8/com/landawn/abacus/util/Traverser.html)(from Apache commons, Google Guava...) ...
[Charsets](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/Charsets.html),
[Ascii](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/Ascii.html),
[CalendarUnit](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/CalendarUnit.html),
[NamingPolicy](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/NamingPolicy.html),
[Properties](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/Properties.html),
[PropertiesUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/PropertiesUtil.html),
[Wrapper](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/Wrapper.html),
[ArrayHashSet](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/ArrayHashSet.html),
[ArrayHashMap](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/ArrayHashMap.html),
[ClassUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/ClassUtil.html),
[EscapeUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/EscapeUtil.html),
[RegExUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/RegExUtil.html),
[FilenameUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/FilenameUtil.html),
[AWSJSONUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/AWSJSONUtil.html),
[AddrUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/AddrUtil.html),
[WSSecurityUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/WSSecurityUtil.html),
[EmailUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/EmailUtil.html),
[IEEE754rUtil](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/IEEE754rUtil.html),
[Index](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/Index.html),
[Median](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/Median.html),
[f](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/f.html),
[Matth](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/Matth.html),
[SafeInitializer](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/SafeInitializer.html),
[Stopwatch](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/Stopwatch.html),
[RateLimiter](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/RateLimiter.html),
[Traverser](https://static.javadoc.io/com.landawn/abacus-util/1.5.9/com/landawn/abacus/util/Traverser.html)(from Apache commons, Google Guava...) ...


## Download/Installation & [Changes](https://github.com/landawn/AbacusUtil/blob/master/CHANGES.md):
Expand All @@ -218,22 +218,22 @@ A general programming library in Java/Android. It's easy to learn and simple to
* Gradle:
```gradle
// JDK 1.8 or above:
compile 'com.landawn:abacus-util:1.5.8'
compile 'com.landawn:abacus-util:1.5.9'
// JDK 1.7:
compile 'com.landawn:abacus-util-jdk7:1.5.8'
compile 'com.landawn:abacus-util-jdk7:1.5.9'
// Android (Java 1.7):
compile 'abacus-android-jdk7:1.5.8'
compile 'abacus-android-jdk7:1.5.9'
// Android-SE (Java 1.7) - small edition without Stream/Matrix/Sheet/...:
compile 'abacus-android-se-jdk7:1.5.8'
compile 'abacus-android-se-jdk7:1.5.9'
// Android (Java 1.8 or above):
compile 'com.landawn:abacus-android:1.5.8'
compile 'com.landawn:abacus-android:1.5.9'
// Android-SE (Java 1.8 or above) - small edition without Stream/Matrix/Sheet/...:
compile 'com.landawn:abacus-android-se:1.5.8'
compile 'com.landawn:abacus-android-se:1.5.9'
```
### Functional Programming:
(It's very important to learn Lambdas and Stream APIs in Java 8 to get the best user experiences with the APIs provided in AbacusUtil)
Expand Down
Binary file modified docs/ExceptionalStream.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/Joiner.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/Splitter.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/Stream.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/StringUtil.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified lib/abacus-util-1.5.9.jar
Binary file not shown.
Binary file modified lib/abacus-util-jdk7-1.5.9.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/com/landawn/abacus/http/OKHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ private <T> T execute(final Class<T> resultClass, final OutputStream outputStrea

if (isOneWayRequest(settings)) {
return null;
} else if (okhttp3.Response.class.equals(resultClass)) {
} else if (resultClass != null && resultClass.equals(okhttp3.Response.class)) {
closeOkHttpResponse = false;

return (T) httpResponse;
Expand Down
13 changes: 8 additions & 5 deletions src/com/landawn/abacus/util/CQLBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
public abstract class CQLBuilder {
private static final Logger logger = LoggerFactory.getLogger(CQLBuilder.class);

private static final Splitter commaSplitter = Splitter.with(WD._COMMA).trim(true);
private static final Splitter spaceSplitter = Splitter.with(WD._COMMA).trim(true);

public static final String DISTINCT = WD.DISTINCT;
public static final String COUNT_ALL = "count(*)";

Expand Down Expand Up @@ -509,10 +512,10 @@ public CQLBuilder into(final Class<?> entityClass) {

public CQLBuilder from(String expr) {
expr = expr.trim();
String tableName = expr.indexOf(WD._COMMA) > 0 ? StringUtil.split(expr, WD._COMMA, true)[0] : expr;
String tableName = expr.indexOf(WD._COMMA) > 0 ? commaSplitter.split(expr).get(0) : expr;

if (tableName.indexOf(WD.SPACE) > 0) {
tableName = StringUtil.split(tableName, WD._SPACE, true)[0];
tableName = spaceSplitter.split(tableName).get(0);
}

return from(tableName, expr);
Expand All @@ -526,7 +529,7 @@ public final CQLBuilder from(final String... tableNames) {
String tableName = tableNames[0].trim();

if (tableName.indexOf(WD.SPACE) > 0) {
tableName = StringUtil.split(tableName, WD._SPACE, true)[0];
tableName = spaceSplitter.split(tableName).get(0);
}

return from(tableName, StringUtil.join(tableNames, WD.COMMA_SPACE));
Expand All @@ -537,7 +540,7 @@ public CQLBuilder from(final Collection<String> tableNames) {
String tableName = tableNames.iterator().next().trim();

if (tableName.indexOf(WD.SPACE) > 0) {
tableName = StringUtil.split(tableName, WD._SPACE, true)[0];
tableName = spaceSplitter.split(tableName).get(0);
}

return from(tableName, StringUtil.join(tableNames, WD.SPACE));
Expand All @@ -547,7 +550,7 @@ public CQLBuilder from(final Map<String, String> tableAliases) {
String tableName = tableAliases.keySet().iterator().next().trim();

if (tableName.indexOf(WD.SPACE) > 0) {
tableName = StringUtil.split(tableName, WD._SPACE, true)[0];
tableName = spaceSplitter.split(tableName).get(0);
}

String expr = "";
Expand Down
177 changes: 177 additions & 0 deletions src/com/landawn/abacus/util/ExceptionalStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,51 @@ public void skip(long n) throws E {
}, false, null, closeHandlers);
}

public <R, A> ExceptionalStream<R, E> split(final int size, final Collector<? super T, A, R> collector) {
checkArgPositive(size, "size");
checkArgNotNull(collector, "collector");

final Supplier<A> supplier = collector.supplier();
final BiConsumer<A, ? super T> accumulator = collector.accumulator();
final Function<A, R> finisher = collector.finisher();

return newStream(new ExceptionalIterator<R, E>() {
@Override
public boolean hasNext() throws E {
return elements.hasNext();
}

@Override
public R next() throws E {
if (hasNext() == false) {
throw new NoSuchElementException();
}

final A container = supplier.get();
int cnt = 0;

while (cnt++ < size && elements.hasNext()) {
accumulator.accept(container, elements.next());
}

return finisher.apply(container);
}

@Override
public long count() throws E {
final long len = elements.count();
return len % size == 0 ? len / size : len / size + 1;
}

@Override
public void skip(long n) throws E {
checkArgNotNegative(n, "n");

elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size);
}
}, false, null, closeHandlers);
}

public ExceptionalStream<Stream<T>, E> sliding(final int windowSize, final int increment) {
return slidingToList(windowSize, increment).map(new Try.Function<List<T>, Stream<T>, E>() {
@Override
Expand Down Expand Up @@ -1698,6 +1743,138 @@ public void skip(long n) throws E {
}, false, null, closeHandlers);
}

public <A, R> ExceptionalStream<R, E> sliding(final int windowSize, final int increment, final Collector<? super T, A, R> collector) {
checkArgument(windowSize > 0 && increment > 0, "windowSize=%s and increment=%s must be bigger than 0", windowSize, increment);
checkArgNotNull(collector, "collector");

final Supplier<A> supplier = collector.supplier();
final BiConsumer<A, ? super T> accumulator = collector.accumulator();
final Function<A, R> finisher = collector.finisher();

return newStream(new ExceptionalIterator<R, E>() {
private Deque<T> queue = null;
private boolean toSkip = false;

@Override
public boolean hasNext() throws E {
if (toSkip) {
int skipNum = increment - windowSize;

while (skipNum-- > 0 && elements.hasNext()) {
elements.next();
}

toSkip = false;
}

return elements.hasNext();
}

@Override
public R next() throws E {
if (hasNext() == false) {
throw new NoSuchElementException();
}

if (increment < windowSize && queue == null) {
queue = new ArrayDeque<>(windowSize - increment);
}

final A container = supplier.get();
int cnt = 0;

if (increment < windowSize && queue.size() > 0) {
cnt = queue.size();

for (T e : queue) {
accumulator.accept(container, e);
}

if (queue.size() <= increment) {
queue.clear();
} else {
for (int i = 0; i < increment; i++) {
queue.removeFirst();
}
}
}

T next = null;

while (cnt++ < windowSize && elements.hasNext()) {
next = elements.next();
accumulator.accept(container, next);

if (cnt > increment) {
queue.add(next);
}
}

toSkip = increment > windowSize;

return finisher.apply(container);
}

@Override
public long count() throws E {
final int prevSize = increment >= windowSize ? 0 : (queue == null ? 0 : queue.size());
final long len = prevSize + elements.count();

if (len == prevSize) {
return 0;
} else if (len <= windowSize) {
return 1;
} else {
final long rlen = len - windowSize;
return 1 + (rlen % increment == 0 ? rlen / increment : rlen / increment + 1);
}
}

@Override
public void skip(long n) throws E {
checkArgNotNegative(n, "n");

if (n == 0) {
return;
}

if (increment >= windowSize) {
elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment);
} else {
if (N.isNullOrEmpty(queue)) {
final long m = ((n - 1) > Long.MAX_VALUE / increment ? Long.MAX_VALUE : (n - 1) * increment);
elements.skip(m);
} else {
final long m = (n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment);
final int prevSize = increment >= windowSize ? 0 : (queue == null ? 0 : queue.size());

if (m < prevSize) {
for (int i = 0; i < m; i++) {
queue.removeFirst();
}
} else {
if (queue != null) {
queue.clear();
}

elements.skip(m - prevSize);
}
}

if (queue == null) {
queue = new ArrayDeque<>(windowSize);
}

int cnt = queue.size();

while (cnt++ < windowSize && elements.hasNext()) {
queue.add(elements.next());
}
}
}
}, false, null, closeHandlers);
}

public ExceptionalStream<T, E> skip(final long n) {
checkArgNotNegative(n, "n");

Expand Down
4 changes: 3 additions & 1 deletion src/com/landawn/abacus/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
public final class IOUtil {
private static final Logger logger = LoggerFactory.getLogger(IOUtil.class);

private static final Splitter pathSplitter = Splitter.with('/').trim(true);

static final int DEFAULT_QUEUE_SIZE_FOR_ROW_PARSER = 1024;

// ...
Expand Down Expand Up @@ -2419,7 +2421,7 @@ public static String simplifyPath(String pathname) {
pathname = pathname.replace('\\', '/');

// split the path apart
String[] components = StringUtil.split(pathname, '/', true);
String[] components = pathSplitter.splitToArray(pathname);
List<String> path = new ArrayList<>();

// resolve ., .., and //
Expand Down
5 changes: 5 additions & 0 deletions src/com/landawn/abacus/util/Joiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class Joiner implements AutoCloseable {
N.checkArgNotNull(delimiter, "The delimiter must not be null");
N.checkArgNotNull(keyValueDelimiter, "The keyValueDelimiter must not be null");
N.checkArgNotNull(suffix, "The suffix must not be null");

// make defensive copies of arguments
this.prefix = prefix.toString();
this.delimiter = delimiter.toString();
Expand Down Expand Up @@ -1135,6 +1136,8 @@ public <T, E extends Exception> T map(Try.Function<? super String, T, E> mapper)
}

/**
* Call {@code mapper} only if at least one element/object/entry is appended.
*
* <pre>
* The underline {@code StringBuilder} will be recycled after this method is called if {@code resueStringBuilder} is set to {@code true},
* and should not continue to this instance.
Expand Down Expand Up @@ -1162,6 +1165,8 @@ public Stream<String> stream() {
}

/**
* Returns a stream with the String value generated by {@code toString()} if at least one element/object/entry is appended, otherwise an empty {@code Stream} is returned.
*
* <pre>
* The underline {@code StringBuilder} will be recycled after this method is called if {@code resueStringBuilder} is set to {@code true},
* and should not continue to this instance.
Expand Down
Loading

0 comments on commit 1726220

Please sign in to comment.