diff --git a/CHANGES.md b/CHANGES.md index 28da4968..74713128 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,9 +1,13 @@ ### 1.5.7 +* Add `Stream.split/splitAt/splitBy/sliding(..., Collector collector)`. + * Add `Stream/IntStream/.../DoubleStream.scan(seed, accumulator, seedIncluded)`. * Add `ByteStream/IntStream/.../DoubleStream.flatMappToObj(ByteFunction mapper)`. +* Add `JdbcUtil.batchUpdate/batchInsert(PreparedQuery...)`. + * Improvements and bug fix. diff --git a/lib/abacus-util-1.5.7.jar b/lib/abacus-util-1.5.7.jar index a4af453f..ce2ef0e0 100644 Binary files a/lib/abacus-util-1.5.7.jar and b/lib/abacus-util-1.5.7.jar differ diff --git a/lib/abacus-util-jdk7-1.5.7.jar b/lib/abacus-util-jdk7-1.5.7.jar index 36b84ced..6639fec5 100644 Binary files a/lib/abacus-util-jdk7-1.5.7.jar and b/lib/abacus-util-jdk7-1.5.7.jar differ diff --git a/src/com/landawn/abacus/android/util/Observer.java b/src/com/landawn/abacus/android/util/Observer.java index f32f8c32..3b97beee 100644 --- a/src/com/landawn/abacus/android/util/Observer.java +++ b/src/com/landawn/abacus/android/util/Observer.java @@ -166,13 +166,15 @@ public Observer> timestamp() { @Override public O skip(final long n) { + N.checkArgNotNegative(n, "n"); + return (O) super.skip(n); } @Override - public O limit(final long n) { - return (O) super.limit(n); + public O limit(final long maxSize) { + return (O) super.limit(maxSize); } /* diff --git a/src/com/landawn/abacus/core/RowDataSet.java b/src/com/landawn/abacus/core/RowDataSet.java index 8d8b7dbd..cdd473be 100644 --- a/src/com/landawn/abacus/core/RowDataSet.java +++ b/src/com/landawn/abacus/core/RowDataSet.java @@ -8468,6 +8468,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + checkForComodification(); cursor = n > toRowIndex - cursor ? toRowIndex : (int) n + cursor; @@ -8558,6 +8560,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + checkForComodification(); cursor = n > toRowIndex - cursor ? toRowIndex : (int) n + cursor; diff --git a/src/com/landawn/abacus/spring/DBUtil.java b/src/com/landawn/abacus/spring/DBUtil.java index 9a20cba0..ed7a0b27 100644 --- a/src/com/landawn/abacus/spring/DBUtil.java +++ b/src/com/landawn/abacus/spring/DBUtil.java @@ -131,11 +131,11 @@ public PreparedCallableQuery prepareCallableQuery(final Try.Function createCloseHandler(final Connection conn) { - return new Try.Runnable() { + private Runnable createCloseHandler(final Connection conn) { + return new Runnable() { @Override - public void run() throws SQLException { - DataSourceUtils.doReleaseConnection(conn, ds); + public void run() { + DataSourceUtils.releaseConnection(conn, ds); } }; } diff --git a/src/com/landawn/abacus/util/Array.java b/src/com/landawn/abacus/util/Array.java index 701e7ea8..9abf3c43 100644 --- a/src/com/landawn/abacus/util/Array.java +++ b/src/com/landawn/abacus/util/Array.java @@ -1342,7 +1342,7 @@ static void sort(final List c, final int fromIndex, final int t try { array = (T[]) N.listElementDataField.get(c); - } catch (Exception e) { + } catch (Throwable e) { // ignore; N.isListElementDataFieldGettable = false; } @@ -2561,7 +2561,7 @@ static void parallelSort(final List c, final int fromIndex, fin try { array = (T[]) N.listElementDataField.get(c); - } catch (Exception e) { + } catch (Throwable e) { // ignore; N.isListElementDataFieldGettable = false; } @@ -3281,7 +3281,7 @@ static int binarySearch(final List list, final int fromIndex, f try { array = (T[]) N.listElementDataField.get(list); - } catch (Exception e) { + } catch (Throwable e) { // ignore; N.isListElementDataFieldGettable = false; } diff --git a/src/com/landawn/abacus/util/BooleanMatrix.java b/src/com/landawn/abacus/util/BooleanMatrix.java index 618282cb..c620c76e 100644 --- a/src/com/landawn/abacus/util/BooleanMatrix.java +++ b/src/com/landawn/abacus/util/BooleanMatrix.java @@ -1052,6 +1052,8 @@ public Boolean next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1094,6 +1096,8 @@ public Boolean next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1159,6 +1163,8 @@ public Boolean next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toRowIndex - i) * cols * 1L - j) { i = toRowIndex; j = 0; @@ -1252,6 +1258,8 @@ public Boolean next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toColumnIndex - j) * BooleanMatrix.this.rows * 1L - i) { i = 0; j = toColumnIndex; @@ -1331,6 +1339,8 @@ public Stream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1402,6 +1412,8 @@ public Boolean next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1414,6 +1426,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/ByteMatrix.java b/src/com/landawn/abacus/util/ByteMatrix.java index 7bd9fc30..88753830 100644 --- a/src/com/landawn/abacus/util/ByteMatrix.java +++ b/src/com/landawn/abacus/util/ByteMatrix.java @@ -1389,6 +1389,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1431,6 +1433,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1496,6 +1500,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toRowIndex - i) * cols * 1L - j) { i = toRowIndex; j = 0; @@ -1585,6 +1591,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toColumnIndex - j) * ByteMatrix.this.rows * 1L - i) { i = 0; j = toColumnIndex; @@ -1661,6 +1669,8 @@ public ByteStream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1732,6 +1742,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1744,6 +1756,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/CharMatrix.java b/src/com/landawn/abacus/util/CharMatrix.java index 435c274b..aafdc907 100644 --- a/src/com/landawn/abacus/util/CharMatrix.java +++ b/src/com/landawn/abacus/util/CharMatrix.java @@ -1355,6 +1355,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1397,6 +1399,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1462,6 +1466,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toRowIndex - i) * cols * 1L - j) { i = toRowIndex; j = 0; @@ -1552,6 +1558,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toColumnIndex - j) * CharMatrix.this.rows * 1L - i) { i = 0; j = toColumnIndex; @@ -1628,6 +1636,8 @@ public CharStream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1699,6 +1709,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1711,6 +1723,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/DoubleMatrix.java b/src/com/landawn/abacus/util/DoubleMatrix.java index 8cb7a6ee..fc2cc66a 100644 --- a/src/com/landawn/abacus/util/DoubleMatrix.java +++ b/src/com/landawn/abacus/util/DoubleMatrix.java @@ -1335,6 +1335,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1377,6 +1379,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1433,6 +1437,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toRowIndex - i) * cols * 1L - j) { i = toRowIndex; j = 0; @@ -1523,6 +1529,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toColumnIndex - j) * DoubleMatrix.this.rows * 1L - i) { i = 0; j = toColumnIndex; @@ -1599,6 +1607,8 @@ public DoubleStream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1670,6 +1680,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1682,6 +1694,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/ExceptionalStream.java b/src/com/landawn/abacus/util/ExceptionalStream.java index 25b9738a..078237ac 100644 --- a/src/com/landawn/abacus/util/ExceptionalStream.java +++ b/src/com/landawn/abacus/util/ExceptionalStream.java @@ -135,9 +135,9 @@ public long count() throws E { @Override public void skip(long n) throws E { - if (n <= 0) { - return; - } else if (n > len - position) { + N.checkArgNotNegative(n, "n"); + + if (n > len - position) { position = len; } @@ -218,6 +218,8 @@ public T next() throws E { @Override public void skip(long n) throws E { + N.checkArgNotNegative(n, "n"); + if (iter == null) { s = s.skip(n); } else { @@ -593,10 +595,8 @@ public T next() throws SQLException { } @Override - public void skip(final long n) throws SQLException { - if (n <= 0) { - return; - } + public void skip(long n) throws SQLException { + N.checkArgNotNegative(n, "n"); final long m = hasNext ? n - 1 : n; @@ -650,10 +650,8 @@ public T next() throws SQLException { } @Override - public void skip(final long n) throws SQLException { - if (n <= 0) { - return; - } + public void skip(long n) throws SQLException { + N.checkArgNotNegative(n, "n"); final long m = hasNext ? n - 1 : n; @@ -713,10 +711,8 @@ public T next() throws SQLException { } @Override - public void skip(final long n) throws SQLException { - if (n <= 0) { - return; - } + public void skip(long n) throws SQLException { + N.checkArgNotNegative(n, "n"); final long m = hasNext ? n - 1 : n; @@ -1421,7 +1417,9 @@ public long count() throws E { @Override public void skip(long n) throws E { - elements.skip(n >= Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); } }, false, null, closeHandlers); } @@ -1438,21 +1436,20 @@ public Stream apply(List t) { public ExceptionalStream, E> slidingToList(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); - final ExceptionalIterator elements = this.elements; - return newStream(new ExceptionalIterator, E>() { private List prev = null; + private boolean toSkip = false; @Override public boolean hasNext() throws E { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.next(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -1468,25 +1465,83 @@ public List next() throws E { int cnt = 0; if (prev != null && increment < windowSize) { - result.addAll(prev.subList(windowSize - cnt, prev.size())); + cnt = windowSize - increment; + + if (cnt <= 8) { + for (int i = windowSize - cnt; i < windowSize; i++) { + result.add(prev.get(i)); + } + } else { + result.addAll(prev.subList(windowSize - cnt, windowSize)); + } } while (cnt++ < windowSize && elements.hasNext()) { result.add(elements.next()); } + toSkip = increment > windowSize; + return prev = result; } + + @Override + public long count() throws E { + final int prevSize = increment >= windowSize ? 0 : (prev == null ? 0 : prev.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 { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } + + if (increment >= windowSize) { + elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment); + } else { + final List tmp = new ArrayList<>(windowSize); + + if (N.isNullOrEmpty(prev)) { + 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 : (prev == null ? 0 : prev.size()); + + if (m < prevSize) { + tmp.addAll(prev.subList((int) m, prevSize)); + } else { + elements.skip(m - prevSize); + } + } + + int cnt = tmp.size(); + + while (cnt++ < windowSize && elements.hasNext()) { + tmp.add(elements.next()); + } + + prev = tmp; + } + } }, false, null, closeHandlers); } public ExceptionalStream skip(final long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - return newStream(new ExceptionalIterator() { private boolean skipped = false; @@ -1613,6 +1668,8 @@ public long count() throws E { @Override public void skip(long n) throws E { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -2508,6 +2565,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + try { elements.skip(n); } catch (Exception e) { @@ -2546,6 +2605,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + try { elements.skip(n); } catch (Exception e) { @@ -2645,18 +2706,20 @@ public Stream __() { // } // } // - // @Override - // public void skip(final long n) throws E2 { - // if (initialized == false) { - // init(); - // } - // - // try { - // iter.skip(n); - // } catch (Exception e) { - // throw convertE.apply(e); + // @Override + // public void skip(long n) throws E2 { + // N.checkArgNotNegative(n, "n"); + // + // if (initialized == false) { + // init(); + // } + // + // try { + // iter.skip(n); + // } catch (Exception e) { + // throw convertE.apply(e); + // } // } - // } // // @Override // public long count() throws E2 { @@ -2819,6 +2882,8 @@ public T next() throws E { @Override public void skip(long n) throws E { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -2901,6 +2966,8 @@ public long count() throws E { @Override public void skip(long n) throws E { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -2929,9 +2996,7 @@ private void init() throws E { public abstract T next() throws E; public void skip(long n) throws E { - if (n <= 0) { - return; - } + N.checkArgNotNegative(n, "n"); while (n-- > 0 && hasNext()) { next(); diff --git a/src/com/landawn/abacus/util/FloatMatrix.java b/src/com/landawn/abacus/util/FloatMatrix.java index 78bc6a1c..267b14c6 100644 --- a/src/com/landawn/abacus/util/FloatMatrix.java +++ b/src/com/landawn/abacus/util/FloatMatrix.java @@ -1296,6 +1296,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1338,6 +1340,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1403,6 +1407,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toRowIndex - i) * cols * 1L - j) { i = toRowIndex; j = 0; @@ -1493,6 +1499,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toColumnIndex - j) * FloatMatrix.this.rows * 1L - i) { i = 0; j = toColumnIndex; @@ -1569,6 +1577,8 @@ public FloatStream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1640,6 +1650,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1652,6 +1664,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/IntMatrix.java b/src/com/landawn/abacus/util/IntMatrix.java index 54ebf88e..d2183caf 100644 --- a/src/com/landawn/abacus/util/IntMatrix.java +++ b/src/com/landawn/abacus/util/IntMatrix.java @@ -1377,6 +1377,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1419,6 +1421,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1484,6 +1488,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toRowIndex - i) * cols * 1L - j) { i = toRowIndex; j = 0; @@ -1574,6 +1580,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toColumnIndex - j) * IntMatrix.this.rows * 1L - i) { i = 0; j = toColumnIndex; @@ -1650,6 +1658,8 @@ public IntStream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1721,6 +1731,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1733,6 +1745,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/JdbcUtil.java b/src/com/landawn/abacus/util/JdbcUtil.java index d6ce478e..6c19b34e 100644 --- a/src/com/landawn/abacus/util/JdbcUtil.java +++ b/src/com/landawn/abacus/util/JdbcUtil.java @@ -1418,6 +1418,137 @@ public static PreparedCallableQuery prepareCallableQuery(final Connection conn, // return new PreparedCallableQuery(stmtCreator.get()); // } + /** + * Generally, this method should be executed in transaction. + * + * @param query + * @param batchSize + * @param batchParameters + * @param paramSetter + * @return + * @throws SQLException + * @throws E + */ + public static , T, E extends Exception> int batchUpdate(final Q query, + final int batchSize, final Collection batchParameters, Try.EE.BiConsumer paramSetter) + throws SQLException, E { + return batchUpdate(query, batchSize, batchParameters.iterator(), paramSetter); + } + + /** + * Generally, this method should be executed in transaction. + * + * @param query + * @param batchSize + * @param batchParameters + * @param paramSetter + * @return + * @throws SQLException + * @throws E + */ + public static , T, E extends Exception> int batchUpdate(final Q query, + final int batchSize, final Iterator batchParameters, Try.EE.BiConsumer paramSetter) + throws SQLException, E { + N.checkArgNotNull(query); + N.checkArgPositive(batchSize, "batchSize"); + N.checkArgNotNull(batchParameters); + N.checkArgNotNull(paramSetter); + + long result = 0; + final boolean closeAfterExecution = query.closeAfterExecution(); + query.closeAfterExecution(false); + long cnt = 0; + + try { + while (batchParameters.hasNext()) { + query.settParameters(batchParameters.next(), paramSetter); + query.addBatch(); + + if (++cnt % batchSize == 0) { + result += N.sum(query.batchUpdate()); + } + } + + if (cnt % batchSize != 0) { + result += N.sum(query.batchUpdate()); + } + + } finally { + query.closeAfterExecution(closeAfterExecution); + + if (closeAfterExecution) { + query.close(); + } + } + + return result >= Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) result; + } + + /** + * Generally, this method should be executed in transaction. + * + * @param query + * @param batchSize + * @param batchParameters + * @param paramSetter + * @return + * @throws SQLException + * @throws E + */ + public static , T, ID, E extends Exception> List batchInsert(final Q query, + final int batchSize, final Collection batchParameters, Try.EE.BiConsumer paramSetter) + throws SQLException, E { + return batchInsert(query, batchSize, batchParameters.iterator(), paramSetter); + } + + /** + * Generally, this method should be executed in transaction. + * + * @param query + * @param batchSize + * @param batchParameters + * @param paramSetter + * @return + * @throws SQLException + * @throws E + */ + public static , T, ID, E extends Exception> List batchInsert(final Q query, + final int batchSize, final Iterator batchParameters, Try.EE.BiConsumer paramSetter) + throws SQLException, E { + N.checkArgNotNull(query); + N.checkArgPositive(batchSize, "batchSize"); + N.checkArgNotNull(batchParameters); + N.checkArgNotNull(paramSetter); + + final List result = new ArrayList<>(); + final boolean closeAfterExecution = query.closeAfterExecution(); + query.closeAfterExecution(false); + long cnt = 0; + + try { + while (batchParameters.hasNext()) { + query.settParameters(batchParameters.next(), paramSetter); + query.addBatch(); + + if (++cnt % batchSize == 0) { + result.addAll(query. batchInsert()); + } + } + + if (cnt % batchSize != 0) { + result.addAll(query. batchInsert()); + } + } finally { + query.closeAfterExecution(closeAfterExecution); + + if (closeAfterExecution) { + query.close(); + } + } + + return result; + } + @SafeVarargs public static PreparedStatement prepareStatement(final Connection conn, final String sql, final Object... parameters) throws SQLException { final NamedSQL namedSQL = NamedSQL.parse(sql); @@ -3220,7 +3351,7 @@ static abstract class AbstractPreparedQuery closeHandler; + Runnable closeHandler; AbstractPreparedQuery(S stmt) { this(stmt, null); @@ -3251,23 +3382,27 @@ public Q closeAfterExecution(boolean closeAfterExecution) { return (Q) this; } + public boolean closeAfterExecution() { + return closeAfterExecution; + } + /** * * @param closeHandler A task to execute after this {@code Query} is closed * @return */ - public Q onClose(final Try.Runnable closeHandler) { + public Q onClose(final Runnable closeHandler) { checkArgNotNull(closeHandler, "closeHandler"); assertNotClosed(); if (this.closeHandler == null) { this.closeHandler = closeHandler; } else { - final Try.Runnable tmp = this.closeHandler; + final Runnable tmp = this.closeHandler; - this.closeHandler = new Try.Runnable() { + this.closeHandler = new Runnable() { @Override - public void run() throws SQLException { + public void run() { try { tmp.run(); } finally { @@ -4058,13 +4193,14 @@ public Q setParameters(Try.EE.Consumer Q settParameters(Try.EE.Consumer paramSetter) throws SQLException, E { + public Q setParameters(final T parameter, final Try.EE.BiConsumer paramSetter) + throws SQLException, E { checkArgNotNull(paramSetter, "paramSetter"); boolean isOK = false; try { - paramSetter.accept((Q) this); + paramSetter.accept(stmt, parameter); isOK = true; } finally { @@ -4078,20 +4214,18 @@ public Q settParameters(Try.EE.Consumer Q setParameters(final int startParameterIndex, Try.EE.BiConsumer paramSetter) - throws SQLException, E { + public Q settParameters(Try.EE.Consumer paramSetter) throws SQLException, E { checkArgNotNull(paramSetter, "paramSetter"); boolean isOK = false; try { - paramSetter.accept(startParameterIndex, stmt); + paramSetter.accept((Q) this); isOK = true; } finally { @@ -4105,20 +4239,19 @@ public Q setParameters(final int startParameterIndex, Try. /** * - * @param startParameterIndex * @param paramSetter * @return * @throws SQLException * @throws E */ - public Q settParameters(final int startParameterIndex, Try.EE.BiConsumer paramSetter) + public Q settParameters(final T parameter, Try.EE.BiConsumer paramSetter) throws SQLException, E { checkArgNotNull(paramSetter, "paramSetter"); boolean isOK = false; try { - paramSetter.accept(startParameterIndex, (Q) this); + paramSetter.accept((Q) this, parameter); isOK = true; } finally { @@ -4130,6 +4263,60 @@ public Q settParameters(final int startParameterIndex, Try return (Q) this; } + // /** + // * + // * @param startParameterIndex + // * @param paramSetter + // * @return + // * @throws SQLException + // * @throws E + // */ + // public Q setParameters(final int startParameterIndex, Try.EE.BiConsumer paramSetter) + // throws SQLException, E { + // checkArgNotNull(paramSetter, "paramSetter"); + // + // boolean isOK = false; + // + // try { + // paramSetter.accept(startParameterIndex, stmt); + // + // isOK = true; + // } finally { + // if (isOK == false) { + // close(); + // } + // } + // + // return (Q) this; + // } + // + // /** + // * + // * @param startParameterIndex + // * @param paramSetter + // * @return + // * @throws SQLException + // * @throws E + // */ + // public Q settParameters(final int startParameterIndex, Try.EE.BiConsumer paramSetter) + // throws SQLException, E { + // checkArgNotNull(paramSetter, "paramSetter"); + // + // boolean isOK = false; + // + // try { + // paramSetter.accept(startParameterIndex, (Q) this); + // + // isOK = true; + // } finally { + // if (isOK == false) { + // close(); + // } + // } + // + // return (Q) this; + // } + public Q addBatch() throws SQLException { stmt.addBatch(); isBatch = true; @@ -4771,10 +4958,8 @@ public T next() throws SQLException { } @Override - public void skip(final long n) throws SQLException { - if (n <= 0) { - return; - } + public void skip(long n) throws SQLException { + N.checkArgNotNegative(n, "n"); final long m = hasNext ? n - 1 : n; hasNext = false; @@ -4872,10 +5057,8 @@ public T next() throws SQLException { } @Override - public void skip(final long n) throws SQLException { - if (n <= 0) { - return; - } + public void skip(long n) throws SQLException { + N.checkArgNotNegative(n, "n"); final long m = hasNext ? n - 1 : n; hasNext = false; @@ -5385,11 +5568,13 @@ public R call() throws E { }); } else { return asyncExecutor.execute(new Try.Callable() { + @Override public R call() throws E { return func.apply(q); } }); + } } @@ -5423,11 +5608,13 @@ public void run() throws E { }); } else { return asyncExecutor.execute(new Try.Callable() { + @Override public Void call() throws E { action.accept(q); return null; } + }); } } @@ -5451,7 +5638,7 @@ protected void checkArgNotNull(Object arg, String argName) { if (arg == null) { try { close(); - } catch (SQLException e) { + } catch (Exception e) { logger.error("Failed to close PreparedQuery", e); } @@ -5463,7 +5650,7 @@ protected void checkArg(boolean b, String errorMsg) { if (b == false) { try { close(); - } catch (SQLException e) { + } catch (Exception e) { logger.error("Failed to close PreparedQuery", e); } @@ -5478,7 +5665,7 @@ Q onClose(Connection conn) { } @Override - public void close() throws SQLException { + public void close() { if (isClosed) { return; } @@ -5491,6 +5678,8 @@ public void close() throws SQLException { } else { stmt.clearParameters(); } + } catch (SQLException e) { + logger.error("Failed to clear the parameters set in Statements", e); } finally { if (closeHandler == null) { JdbcUtil.closeQuietly(stmt, conn); diff --git a/src/com/landawn/abacus/util/LongMatrix.java b/src/com/landawn/abacus/util/LongMatrix.java index 35f01fed..99a29a0b 100644 --- a/src/com/landawn/abacus/util/LongMatrix.java +++ b/src/com/landawn/abacus/util/LongMatrix.java @@ -1332,6 +1332,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1374,6 +1376,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1439,6 +1443,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toRowIndex - i) * cols * 1L - j) { i = toRowIndex; j = 0; @@ -1529,6 +1535,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toColumnIndex - j) * LongMatrix.this.rows * 1L - i) { i = 0; j = toColumnIndex; @@ -1605,6 +1613,8 @@ public LongStream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1676,6 +1686,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1688,6 +1700,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/Matrix.java b/src/com/landawn/abacus/util/Matrix.java index 2ede3366..a3692015 100644 --- a/src/com/landawn/abacus/util/Matrix.java +++ b/src/com/landawn/abacus/util/Matrix.java @@ -1368,6 +1368,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1410,6 +1412,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1475,6 +1479,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toRowIndex - i) * cols * 1L - j) { i = toRowIndex; j = 0; @@ -1568,6 +1574,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toColumnIndex - j) * Matrix.this.rows * 1L - i) { i = 0; j = toColumnIndex; @@ -1647,6 +1655,8 @@ public Stream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1718,6 +1728,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1730,6 +1742,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/N.java b/src/com/landawn/abacus/util/N.java index a6406519..d8084f76 100644 --- a/src/com/landawn/abacus/util/N.java +++ b/src/com/landawn/abacus/util/N.java @@ -514,14 +514,14 @@ public final class N { try { tmp = String.class.getDeclaredField("offset"); - } catch (Exception e) { + } catch (Throwable e) { // ignore. } if (tmp == null) { try { tmp = String.class.getDeclaredField("count"); - } catch (Exception e) { + } catch (Throwable e) { // ignore. } } @@ -529,7 +529,7 @@ public final class N { if (tmp == null) { try { tmp = String.class.getDeclaredField("value"); - } catch (Exception e) { + } catch (Throwable e) { // ignore. } } @@ -538,7 +538,7 @@ public final class N { try { tmp = ArrayList.class.getDeclaredField("elementData"); - } catch (Exception e) { + } catch (Throwable e) { // ignore. } @@ -552,7 +552,7 @@ public final class N { try { tmp = ArrayList.class.getDeclaredField("size"); - } catch (Exception e) { + } catch (Throwable e) { // ignore. } @@ -11113,7 +11113,7 @@ private static List createList(final T... a) { N.listSizeField.set(list, a.length); return list; - } catch (Exception e) { + } catch (Throwable e) { // ignore; N.isListElementDataFieldSettable = false; } @@ -22789,7 +22789,7 @@ public static boolean deleteRange(final List c, final int fromIndex, fina c.add(null); c.remove(c.size() - 1); return true; - } catch (Exception e) { + } catch (Throwable e) { // ignore; isListElementDataFieldSettable = false; } diff --git a/src/com/landawn/abacus/util/Observer.java b/src/com/landawn/abacus/util/Observer.java index e37d4f85..4fc71523 100644 --- a/src/com/landawn/abacus/util/Observer.java +++ b/src/com/landawn/abacus/util/Observer.java @@ -496,7 +496,7 @@ public void onNext(final Object param) { } public Observer skip(final long n) { - N.checkArgument(n >= 0, "n can't be negative"); + N.checkArgNotNegative(n, "n"); if (n > 0) { dispatcher.append(new Dispatcher() { @@ -514,15 +514,15 @@ public void onNext(final Object param) { return this; } - public Observer limit(final long n) { - N.checkArgument(n >= 0, "n can't be negative"); + public Observer limit(final long maxSize) { + N.checkArgNotNegative(maxSize, "maxSize"); dispatcher.append(new Dispatcher() { private final AtomicLong counter = new AtomicLong(); @Override public void onNext(final Object param) { - if (downDispatcher != null && counter.incrementAndGet() <= n) { + if (downDispatcher != null && counter.incrementAndGet() <= maxSize) { downDispatcher.onNext(param); } else { hasMore = false; diff --git a/src/com/landawn/abacus/util/SQLExecutor.java b/src/com/landawn/abacus/util/SQLExecutor.java index 1c7d719f..6dc112f9 100644 --- a/src/com/landawn/abacus/util/SQLExecutor.java +++ b/src/com/landawn/abacus/util/SQLExecutor.java @@ -2791,10 +2791,8 @@ public T next() { } @Override - public void skip(final long n) { - if (n <= 0) { - return; - } + public void skip(long n) { + N.checkArgNotNegative(n, "n"); if (skipped == false) { skip(); @@ -3040,7 +3038,7 @@ public PreparedQuery prepareQuery(final Connection conn, final String sql, State try { final PreparedStatement stmt = prepareStatement(ds, localConn, namedSQL, statementSetter, jdbcSettings, false, false, parameters); - result = new PreparedQuery(stmt, _asyncExecutor).onClose(new Try.Runnable() { + result = new PreparedQuery(stmt, _asyncExecutor).onClose(new Runnable() { @Override public void run() { closeQuietly(localConn, conn, ds); diff --git a/src/com/landawn/abacus/util/Seq.java b/src/com/landawn/abacus/util/Seq.java index ed68bccd..b5239773 100644 --- a/src/com/landawn/abacus/util/Seq.java +++ b/src/com/landawn/abacus/util/Seq.java @@ -1995,12 +1995,12 @@ public , E extends Exception, E2 extends Exception> L /** * - * @param n + * @param where * @return */ @SuppressWarnings("rawtypes") - public Pair, List> splitAt(final int n) { - N.checkArgument(n >= 0, "'n' can't be negative: ", n); + public Pair, List> splitAt(final int where) { + N.checkArgNotNegative(where, "where"); List left = null; List right = null; @@ -2008,18 +2008,18 @@ public Pair, List> splitAt(final int n) { if (N.isNullOrEmpty(coll)) { left = new ArrayList<>(); right = new ArrayList<>(); - } else if (n == 0) { + } else if (where == 0) { left = new ArrayList<>(); right = new ArrayList<>(coll); - } else if (n >= coll.size()) { + } else if (where >= coll.size()) { left = new ArrayList<>(); right = new ArrayList<>(coll); } else if (coll instanceof List) { - left = new ArrayList<>(((List) coll).subList(0, n)); - right = new ArrayList<>(((List) coll).subList(n, size())); + left = new ArrayList<>(((List) coll).subList(0, where)); + right = new ArrayList<>(((List) coll).subList(where, size())); } else { - left = new ArrayList<>(slice(0, n)); - right = new ArrayList<>(slice(n, size())); + left = new ArrayList<>(slice(0, where)); + right = new ArrayList<>(slice(where, size())); } return Pair.of(left, right); diff --git a/src/com/landawn/abacus/util/Sheet.java b/src/com/landawn/abacus/util/Sheet.java index 7756b502..3e49f098 100644 --- a/src/com/landawn/abacus/util/Sheet.java +++ b/src/com/landawn/abacus/util/Sheet.java @@ -1353,6 +1353,8 @@ public Sheet.Cell next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + n : toIndex; } @@ -1422,6 +1424,8 @@ public Sheet.Cell next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + n : toIndex; } @@ -1493,6 +1497,8 @@ public Cell next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + columnIndex = n < columnLength - columnIndex ? columnIndex + (int) n : columnLength; } @@ -1505,6 +1511,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + rowIndex = n < toRowIndex - rowIndex ? rowIndex + (int) n : toRowIndex; } @@ -1577,6 +1585,8 @@ public Cell apply(int rowIndex) { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + columnIndex = n < toColumnIndex - columnIndex ? columnIndex + (int) n : toColumnIndex; } @@ -1740,6 +1750,8 @@ public E next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + n : toIndex; } @@ -1806,6 +1818,8 @@ public E next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + n : toIndex; } @@ -1878,6 +1892,8 @@ public E next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1890,6 +1906,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1946,6 +1964,8 @@ public Stream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/ShortMatrix.java b/src/com/landawn/abacus/util/ShortMatrix.java index d2ec1212..8dfaea84 100644 --- a/src/com/landawn/abacus/util/ShortMatrix.java +++ b/src/com/landawn/abacus/util/ShortMatrix.java @@ -1380,6 +1380,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1422,6 +1424,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1487,6 +1491,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toRowIndex - i) * cols * 1L - j) { i = toRowIndex; j = 0; @@ -1577,6 +1583,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n >= (toColumnIndex - j) * ShortMatrix.this.rows * 1L - i) { i = 0; j = toColumnIndex; @@ -1653,6 +1661,8 @@ public ShortStream next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1724,6 +1734,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor2 = n < toIndex2 - cursor2 ? cursor2 + (int) n : toIndex2; } @@ -1736,6 +1748,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/Splitter.java b/src/com/landawn/abacus/util/Splitter.java index 98ff683e..1c02df5e 100644 --- a/src/com/landawn/abacus/util/Splitter.java +++ b/src/com/landawn/abacus/util/Splitter.java @@ -68,9 +68,7 @@ public static Splitter pattern(CharSequence delimiterRegex) { } public Splitter limit(int max) { - if (max < 1) { - throw new IllegalArgumentException("'max' must be greater than 0"); - } + N.checkArgPositive(max, "max"); this.max = max; @@ -280,9 +278,7 @@ public static MapSplitter pattern(CharSequence entryDelimiterRegex, CharSequence } public MapSplitter limit(int max) { - if (max < 1) { - throw new IllegalArgumentException("'max' must be greater than 0"); - } + N.checkArgPositive(max, "max"); this.max = max; diff --git a/src/com/landawn/abacus/util/stream/AbstractByteStream.java b/src/com/landawn/abacus/util/stream/AbstractByteStream.java index fb300856..436c44c4 100644 --- a/src/com/landawn/abacus/util/stream/AbstractByteStream.java +++ b/src/com/landawn/abacus/util/stream/AbstractByteStream.java @@ -108,9 +108,7 @@ public Stream apply(byte t) { @Override public ByteStream skip(final long n, final ByteConsumer action) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } @@ -229,6 +227,117 @@ public ByteStream apply(ByteList t) { }); } + @Override + public Stream splitBy(final BytePredicate where) { + N.checkArgNotNull(where); + + final ByteIteratorEx iter = iteratorEx(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + private byte next = 0; + private boolean hasNext = false; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public ByteStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + ByteStream result = null; + + if (cursor == 0) { + final ByteList list = new ByteList(); + + while (iter.hasNext()) { + next = iter.nextByte(); + + if (where.test(next)) { + list.add(next); + } else { + hasNext = true; + break; + } + } + + result = new ArrayByteStream(list.array(), 0, list.size(), sorted, null); + } else { + ByteIteratorEx iterEx = iter; + + if (hasNext) { + iterEx = new ByteIteratorEx() { + private boolean isFirst = true; + + @Override + public boolean hasNext() { + return isFirst || iter.hasNext(); + } + + @Override + public byte nextByte() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + if (isFirst) { + isFirst = false; + return next; + } else { + return iter.nextByte(); + } + } + }; + } + + result = new IteratorByteStream(iterEx, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + iter.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.nextByte(); + + if (where.test(next) == false) { + hasNext = true; + break; + } + } + } else { + iter.skip(Long.MAX_VALUE); + } + } else { + iter.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + + }, false, null); + } + @Override public Stream sliding(final int windowSize, final int increment) { return slidingToList(windowSize, increment).map(new Function() { @@ -383,104 +492,6 @@ public boolean test(Byte value) { }).mapToByte(ToByteFunction.UNBOX)).iteratorEx(), false); } - @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); - - return newStream(new ObjIteratorEx() { - private ByteStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public ByteStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final ByteIterator iter = AbstractByteStream.this.iteratorEx(); - final ByteList list = new ByteList(); - - while (list.size() < n && iter.hasNext()) { - list.add(iter.nextByte()); - } - - a = new ByteStream[] { new ArrayByteStream(list.array(), 0, list.size(), sorted, null), new IteratorByteStream(iter, sorted, null) }; - } - } - - }, false, null); - } - - @Override - public Stream splitBy(final BytePredicate where) { - N.checkArgNotNull(where); - - return newStream(new ObjIteratorEx() { - private ByteStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public ByteStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final ByteIterator iter = AbstractByteStream.this.iteratorEx(); - final ByteList list = new ByteList(); - byte next = 0; - ByteStream s = null; - - while (iter.hasNext()) { - next = iter.nextByte(); - - if (where.test(next)) { - list.add(next); - } else { - s = ByteStream.of(next); - - break; - } - } - - a = new ByteStream[] { new ArrayByteStream(list.array(), 0, list.size(), sorted, null), new IteratorByteStream(iter, sorted, null) }; - - if (s != null) { - if (sorted) { - a[1] = new IteratorByteStream(a[1].prepend(s).iteratorEx(), sorted, null); - } else { - a[1] = a[1].prepend(s); - } - } - } - } - - }, false, null); - } - @Override public ByteStream reversed() { return newStream(new ByteIteratorEx() { @@ -521,6 +532,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -602,6 +615,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -704,6 +719,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -783,6 +800,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } diff --git a/src/com/landawn/abacus/util/stream/AbstractCharStream.java b/src/com/landawn/abacus/util/stream/AbstractCharStream.java index e954f78a..46ca042a 100644 --- a/src/com/landawn/abacus/util/stream/AbstractCharStream.java +++ b/src/com/landawn/abacus/util/stream/AbstractCharStream.java @@ -108,9 +108,7 @@ public Stream apply(char t) { @Override public CharStream skip(final long n, final CharConsumer action) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } @@ -229,6 +227,117 @@ public CharStream apply(CharList t) { }); } + @Override + public Stream splitBy(final CharPredicate where) { + N.checkArgNotNull(where); + + final CharIteratorEx iter = iteratorEx(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + private char next = 0; + private boolean hasNext = false; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public CharStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + CharStream result = null; + + if (cursor == 0) { + final CharList list = new CharList(); + + while (iter.hasNext()) { + next = iter.nextChar(); + + if (where.test(next)) { + list.add(next); + } else { + hasNext = true; + break; + } + } + + result = new ArrayCharStream(list.array(), 0, list.size(), sorted, null); + } else { + CharIteratorEx iterEx = iter; + + if (hasNext) { + iterEx = new CharIteratorEx() { + private boolean isFirst = true; + + @Override + public boolean hasNext() { + return isFirst || iter.hasNext(); + } + + @Override + public char nextChar() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + if (isFirst) { + isFirst = false; + return next; + } else { + return iter.nextChar(); + } + } + }; + } + + result = new IteratorCharStream(iterEx, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + iter.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.nextChar(); + + if (where.test(next) == false) { + hasNext = true; + break; + } + } + } else { + iter.skip(Long.MAX_VALUE); + } + } else { + iter.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + + }, false, null); + } + @Override public Stream sliding(final int windowSize, final int increment) { return slidingToList(windowSize, increment).map(new Function() { @@ -383,104 +492,6 @@ public boolean test(Character value) { }).mapToChar(ToCharFunction.UNBOX)).iteratorEx(), false); } - @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); - - return newStream(new ObjIteratorEx() { - private CharStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public CharStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final CharIterator iter = AbstractCharStream.this.iteratorEx(); - final CharList list = new CharList(); - - while (list.size() < n && iter.hasNext()) { - list.add(iter.nextChar()); - } - - a = new CharStream[] { new ArrayCharStream(list.array(), 0, list.size(), sorted, null), new IteratorCharStream(iter, sorted, null) }; - } - } - - }, false, null); - } - - @Override - public Stream splitBy(final CharPredicate where) { - N.checkArgNotNull(where); - - return newStream(new ObjIteratorEx() { - private CharStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public CharStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final CharIterator iter = AbstractCharStream.this.iteratorEx(); - final CharList list = new CharList(); - char next = 0; - CharStream s = null; - - while (iter.hasNext()) { - next = iter.nextChar(); - - if (where.test(next)) { - list.add(next); - } else { - s = CharStream.of(next); - - break; - } - } - - a = new CharStream[] { new ArrayCharStream(list.array(), 0, list.size(), sorted, null), new IteratorCharStream(iter, sorted, null) }; - - if (s != null) { - if (sorted) { - a[1] = new IteratorCharStream(a[1].prepend(s).iteratorEx(), sorted, null); - } else { - a[1] = a[1].prepend(s); - } - } - } - } - - }, false, null); - } - @Override public CharStream reversed() { return newStream(new CharIteratorEx() { @@ -521,6 +532,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -602,6 +615,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -704,6 +719,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -783,6 +800,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } diff --git a/src/com/landawn/abacus/util/stream/AbstractDoubleStream.java b/src/com/landawn/abacus/util/stream/AbstractDoubleStream.java index 2cf691f7..301e2e4a 100644 --- a/src/com/landawn/abacus/util/stream/AbstractDoubleStream.java +++ b/src/com/landawn/abacus/util/stream/AbstractDoubleStream.java @@ -109,9 +109,7 @@ public Stream apply(double t) { @Override public DoubleStream skip(final long n, final DoubleConsumer action) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } @@ -230,6 +228,117 @@ public DoubleStream apply(DoubleList t) { }); } + @Override + public Stream splitBy(final DoublePredicate where) { + N.checkArgNotNull(where); + + final DoubleIteratorEx iter = iteratorEx(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + private double next = 0; + private boolean hasNext = false; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public DoubleStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + DoubleStream result = null; + + if (cursor == 0) { + final DoubleList list = new DoubleList(); + + while (iter.hasNext()) { + next = iter.nextDouble(); + + if (where.test(next)) { + list.add(next); + } else { + hasNext = true; + break; + } + } + + result = new ArrayDoubleStream(list.array(), 0, list.size(), sorted, null); + } else { + DoubleIteratorEx iterEx = iter; + + if (hasNext) { + iterEx = new DoubleIteratorEx() { + private boolean isFirst = true; + + @Override + public boolean hasNext() { + return isFirst || iter.hasNext(); + } + + @Override + public double nextDouble() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + if (isFirst) { + isFirst = false; + return next; + } else { + return iter.nextDouble(); + } + } + }; + } + + result = new IteratorDoubleStream(iterEx, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + iter.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.nextDouble(); + + if (where.test(next) == false) { + hasNext = true; + break; + } + } + } else { + iter.skip(Long.MAX_VALUE); + } + } else { + iter.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + + }, false, null); + } + @Override public Stream sliding(final int windowSize, final int increment) { return slidingToList(windowSize, increment).map(new Function() { @@ -395,104 +504,6 @@ public boolean test(Double value) { }).mapToDouble(ToDoubleFunction.UNBOX)).iteratorEx(), false); } - @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); - - return newStream(new ObjIteratorEx() { - private DoubleStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public DoubleStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final DoubleIterator iter = AbstractDoubleStream.this.iteratorEx(); - final DoubleList list = new DoubleList(); - - while (list.size() < n && iter.hasNext()) { - list.add(iter.nextDouble()); - } - - a = new DoubleStream[] { new ArrayDoubleStream(list.array(), 0, list.size(), sorted, null), new IteratorDoubleStream(iter, sorted, null) }; - } - } - - }, false, null); - } - - @Override - public Stream splitBy(final DoublePredicate where) { - N.checkArgNotNull(where); - - return newStream(new ObjIteratorEx() { - private DoubleStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public DoubleStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final DoubleIterator iter = AbstractDoubleStream.this.iteratorEx(); - final DoubleList list = new DoubleList(); - double next = 0; - DoubleStream s = null; - - while (iter.hasNext()) { - next = iter.nextDouble(); - - if (where.test(next)) { - list.add(next); - } else { - s = DoubleStream.of(next); - - break; - } - } - - a = new DoubleStream[] { new ArrayDoubleStream(list.array(), 0, list.size(), sorted, null), new IteratorDoubleStream(iter, sorted, null) }; - - if (s != null) { - if (sorted) { - a[1] = new IteratorDoubleStream(a[1].prepend(s).iteratorEx(), sorted, null); - } else { - a[1] = a[1].prepend(s); - } - } - } - } - - }, false, null); - } - @Override public DoubleStream reversed() { return newStream(new DoubleIteratorEx() { @@ -533,6 +544,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -614,6 +627,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -716,6 +731,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -795,6 +812,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } diff --git a/src/com/landawn/abacus/util/stream/AbstractFloatStream.java b/src/com/landawn/abacus/util/stream/AbstractFloatStream.java index fc5f4123..198387e0 100644 --- a/src/com/landawn/abacus/util/stream/AbstractFloatStream.java +++ b/src/com/landawn/abacus/util/stream/AbstractFloatStream.java @@ -110,9 +110,7 @@ public Stream apply(float t) { @Override public FloatStream skip(final long n, final FloatConsumer action) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } @@ -231,6 +229,117 @@ public FloatStream apply(FloatList t) { }); } + @Override + public Stream splitBy(final FloatPredicate where) { + N.checkArgNotNull(where); + + final FloatIteratorEx iter = iteratorEx(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + private float next = 0; + private boolean hasNext = false; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public FloatStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + FloatStream result = null; + + if (cursor == 0) { + final FloatList list = new FloatList(); + + while (iter.hasNext()) { + next = iter.nextFloat(); + + if (where.test(next)) { + list.add(next); + } else { + hasNext = true; + break; + } + } + + result = new ArrayFloatStream(list.array(), 0, list.size(), sorted, null); + } else { + FloatIteratorEx iterEx = iter; + + if (hasNext) { + iterEx = new FloatIteratorEx() { + private boolean isFirst = true; + + @Override + public boolean hasNext() { + return isFirst || iter.hasNext(); + } + + @Override + public float nextFloat() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + if (isFirst) { + isFirst = false; + return next; + } else { + return iter.nextFloat(); + } + } + }; + } + + result = new IteratorFloatStream(iterEx, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + iter.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.nextFloat(); + + if (where.test(next) == false) { + hasNext = true; + break; + } + } + } else { + iter.skip(Long.MAX_VALUE); + } + } else { + iter.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + + }, false, null); + } + @Override public Stream sliding(final int windowSize, final int increment) { return slidingToList(windowSize, increment).map(new Function() { @@ -390,104 +499,6 @@ public boolean test(Float value) { }).mapToFloat(ToFloatFunction.UNBOX)).iteratorEx(), false); } - @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); - - return newStream(new ObjIteratorEx() { - private FloatStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public FloatStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final FloatIterator iter = AbstractFloatStream.this.iteratorEx(); - final FloatList list = new FloatList(); - - while (list.size() < n && iter.hasNext()) { - list.add(iter.nextFloat()); - } - - a = new FloatStream[] { new ArrayFloatStream(list.array(), 0, list.size(), sorted, null), new IteratorFloatStream(iter, sorted, null) }; - } - } - - }, false, null); - } - - @Override - public Stream splitBy(final FloatPredicate where) { - N.checkArgNotNull(where); - - return newStream(new ObjIteratorEx() { - private FloatStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public FloatStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final FloatIterator iter = AbstractFloatStream.this.iteratorEx(); - final FloatList list = new FloatList(); - float next = 0; - FloatStream s = null; - - while (iter.hasNext()) { - next = iter.nextFloat(); - - if (where.test(next)) { - list.add(next); - } else { - s = FloatStream.of(next); - - break; - } - } - - a = new FloatStream[] { new ArrayFloatStream(list.array(), 0, list.size(), sorted, null), new IteratorFloatStream(iter, sorted, null) }; - - if (s != null) { - if (sorted) { - a[1] = new IteratorFloatStream(a[1].prepend(s).iteratorEx(), sorted, null); - } else { - a[1] = a[1].prepend(s); - } - } - } - } - - }, false, null); - } - @Override public FloatStream reversed() { return newStream(new FloatIteratorEx() { @@ -528,6 +539,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -609,6 +622,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -711,6 +726,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -790,6 +807,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } diff --git a/src/com/landawn/abacus/util/stream/AbstractIntStream.java b/src/com/landawn/abacus/util/stream/AbstractIntStream.java index 0ada1624..e63245f6 100644 --- a/src/com/landawn/abacus/util/stream/AbstractIntStream.java +++ b/src/com/landawn/abacus/util/stream/AbstractIntStream.java @@ -108,9 +108,7 @@ public Stream apply(int t) { @Override public IntStream skip(final long n, final IntConsumer action) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } @@ -229,6 +227,117 @@ public IntStream apply(IntList t) { }); } + @Override + public Stream splitBy(final IntPredicate where) { + N.checkArgNotNull(where); + + final IntIteratorEx iter = iteratorEx(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + private int next = 0; + private boolean hasNext = false; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public IntStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + IntStream result = null; + + if (cursor == 0) { + final IntList list = new IntList(); + + while (iter.hasNext()) { + next = iter.nextInt(); + + if (where.test(next)) { + list.add(next); + } else { + hasNext = true; + break; + } + } + + result = new ArrayIntStream(list.array(), 0, list.size(), sorted, null); + } else { + IntIteratorEx iterEx = iter; + + if (hasNext) { + iterEx = new IntIteratorEx() { + private boolean isFirst = true; + + @Override + public boolean hasNext() { + return isFirst || iter.hasNext(); + } + + @Override + public int nextInt() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + if (isFirst) { + isFirst = false; + return next; + } else { + return iter.nextInt(); + } + } + }; + } + + result = new IteratorIntStream(iterEx, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + iter.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.nextInt(); + + if (where.test(next) == false) { + hasNext = true; + break; + } + } + } else { + iter.skip(Long.MAX_VALUE); + } + } else { + iter.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + + }, false, null); + } + @Override public Stream sliding(final int windowSize, final int increment) { return slidingToList(windowSize, increment).map(new Function() { @@ -388,104 +497,6 @@ public boolean test(Integer value) { }).mapToInt(ToIntFunction.UNBOX)).iteratorEx(), false); } - @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); - - return newStream(new ObjIteratorEx() { - private IntStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public IntStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final IntIterator iter = AbstractIntStream.this.iteratorEx(); - final IntList list = new IntList(); - - while (list.size() < n && iter.hasNext()) { - list.add(iter.nextInt()); - } - - a = new IntStream[] { new ArrayIntStream(list.array(), 0, list.size(), sorted, null), new IteratorIntStream(iter, sorted, null) }; - } - } - - }, false, null); - } - - @Override - public Stream splitBy(final IntPredicate where) { - N.checkArgNotNull(where); - - return newStream(new ObjIteratorEx() { - private IntStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public IntStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final IntIterator iter = AbstractIntStream.this.iteratorEx(); - final IntList list = new IntList(); - int next = 0; - IntStream s = null; - - while (iter.hasNext()) { - next = iter.nextInt(); - - if (where.test(next)) { - list.add(next); - } else { - s = IntStream.of(next); - - break; - } - } - - a = new IntStream[] { new ArrayIntStream(list.array(), 0, list.size(), sorted, null), new IteratorIntStream(iter, sorted, null) }; - - if (s != null) { - if (sorted) { - a[1] = new IteratorIntStream(a[1].prepend(s).iteratorEx(), sorted, null); - } else { - a[1] = a[1].prepend(s); - } - } - } - } - - }, false, null); - } - @Override public IntStream reversed() { return newStream(new IntIteratorEx() { @@ -526,6 +537,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -607,6 +620,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -709,6 +724,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -788,6 +805,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } diff --git a/src/com/landawn/abacus/util/stream/AbstractLongStream.java b/src/com/landawn/abacus/util/stream/AbstractLongStream.java index abebe8b1..205d6730 100644 --- a/src/com/landawn/abacus/util/stream/AbstractLongStream.java +++ b/src/com/landawn/abacus/util/stream/AbstractLongStream.java @@ -107,9 +107,7 @@ public Stream apply(long t) { @Override public LongStream skip(final long n, final LongConsumer action) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } @@ -228,6 +226,117 @@ public LongStream apply(LongList t) { }); } + @Override + public Stream splitBy(final LongPredicate where) { + N.checkArgNotNull(where); + + final LongIteratorEx iter = iteratorEx(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + private long next = 0; + private boolean hasNext = false; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public LongStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + LongStream result = null; + + if (cursor == 0) { + final LongList list = new LongList(); + + while (iter.hasNext()) { + next = iter.nextLong(); + + if (where.test(next)) { + list.add(next); + } else { + hasNext = true; + break; + } + } + + result = new ArrayLongStream(list.array(), 0, list.size(), sorted, null); + } else { + LongIteratorEx iterEx = iter; + + if (hasNext) { + iterEx = new LongIteratorEx() { + private boolean isFirst = true; + + @Override + public boolean hasNext() { + return isFirst || iter.hasNext(); + } + + @Override + public long nextLong() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + if (isFirst) { + isFirst = false; + return next; + } else { + return iter.nextLong(); + } + } + }; + } + + result = new IteratorLongStream(iterEx, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + iter.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.nextLong(); + + if (where.test(next) == false) { + hasNext = true; + break; + } + } + } else { + iter.skip(Long.MAX_VALUE); + } + } else { + iter.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + + }, false, null); + } + @Override public Stream sliding(final int windowSize, final int increment) { return slidingToList(windowSize, increment).map(new Function() { @@ -387,104 +496,6 @@ public boolean test(Long value) { }).mapToLong(ToLongFunction.UNBOX)).iteratorEx(), false); } - @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); - - return newStream(new ObjIteratorEx() { - private LongStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public LongStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final LongIterator iter = AbstractLongStream.this.iteratorEx(); - final LongList list = new LongList(); - - while (list.size() < n && iter.hasNext()) { - list.add(iter.nextLong()); - } - - a = new LongStream[] { new ArrayLongStream(list.array(), 0, list.size(), sorted, null), new IteratorLongStream(iter, sorted, null) }; - } - } - - }, false, null); - } - - @Override - public Stream splitBy(final LongPredicate where) { - N.checkArgNotNull(where); - - return newStream(new ObjIteratorEx() { - private LongStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public LongStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final LongIterator iter = AbstractLongStream.this.iteratorEx(); - final LongList list = new LongList(); - long next = 0; - LongStream s = null; - - while (iter.hasNext()) { - next = iter.nextLong(); - - if (where.test(next)) { - list.add(next); - } else { - s = LongStream.of(next); - - break; - } - } - - a = new LongStream[] { new ArrayLongStream(list.array(), 0, list.size(), sorted, null), new IteratorLongStream(iter, sorted, null) }; - - if (s != null) { - if (sorted) { - a[1] = new IteratorLongStream(a[1].prepend(s).iteratorEx(), sorted, null); - } else { - a[1] = a[1].prepend(s); - } - } - } - } - - }, false, null); - } - @Override public LongStream reversed() { return newStream(new LongIteratorEx() { @@ -525,6 +536,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -606,6 +619,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -708,6 +723,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -787,6 +804,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } diff --git a/src/com/landawn/abacus/util/stream/AbstractShortStream.java b/src/com/landawn/abacus/util/stream/AbstractShortStream.java index 47585014..b490af02 100644 --- a/src/com/landawn/abacus/util/stream/AbstractShortStream.java +++ b/src/com/landawn/abacus/util/stream/AbstractShortStream.java @@ -108,9 +108,7 @@ public Stream apply(short t) { @Override public ShortStream skip(final long n, final ShortConsumer action) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } @@ -229,6 +227,117 @@ public ShortStream apply(ShortList t) { }); } + @Override + public Stream splitBy(final ShortPredicate where) { + N.checkArgNotNull(where); + + final ShortIteratorEx iter = iteratorEx(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + private short next = 0; + private boolean hasNext = false; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public ShortStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + ShortStream result = null; + + if (cursor == 0) { + final ShortList list = new ShortList(); + + while (iter.hasNext()) { + next = iter.nextShort(); + + if (where.test(next)) { + list.add(next); + } else { + hasNext = true; + break; + } + } + + result = new ArrayShortStream(list.array(), 0, list.size(), sorted, null); + } else { + ShortIteratorEx iterEx = iter; + + if (hasNext) { + iterEx = new ShortIteratorEx() { + private boolean isFirst = true; + + @Override + public boolean hasNext() { + return isFirst || iter.hasNext(); + } + + @Override + public short nextShort() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + if (isFirst) { + isFirst = false; + return next; + } else { + return iter.nextShort(); + } + } + }; + } + + result = new IteratorShortStream(iterEx, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + iter.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.nextShort(); + + if (where.test(next) == false) { + hasNext = true; + break; + } + } + } else { + iter.skip(Long.MAX_VALUE); + } + } else { + iter.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + + }, false, null); + } + @Override public Stream sliding(final int windowSize, final int increment) { return slidingToList(windowSize, increment).map(new Function() { @@ -388,104 +497,6 @@ public boolean test(Short value) { }).mapToShort(ToShortFunction.UNBOX)).iteratorEx(), false); } - @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); - - return newStream(new ObjIteratorEx() { - private ShortStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public ShortStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final ShortIterator iter = AbstractShortStream.this.iteratorEx(); - final ShortList list = new ShortList(); - - while (list.size() < n && iter.hasNext()) { - list.add(iter.nextShort()); - } - - a = new ShortStream[] { new ArrayShortStream(list.array(), 0, list.size(), sorted, null), new IteratorShortStream(iter, sorted, null) }; - } - } - - }, false, null); - } - - @Override - public Stream splitBy(final ShortPredicate where) { - N.checkArgNotNull(where); - - return newStream(new ObjIteratorEx() { - private ShortStream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public ShortStream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final ShortIterator iter = AbstractShortStream.this.iteratorEx(); - final ShortList list = new ShortList(); - short next = 0; - ShortStream s = null; - - while (iter.hasNext()) { - next = iter.nextShort(); - - if (where.test(next)) { - list.add(next); - } else { - s = ShortStream.of(next); - - break; - } - } - - a = new ShortStream[] { new ArrayShortStream(list.array(), 0, list.size(), sorted, null), new IteratorShortStream(iter, sorted, null) }; - - if (s != null) { - if (sorted) { - a[1] = new IteratorShortStream(a[1].prepend(s).iteratorEx(), sorted, null); - } else { - a[1] = a[1].prepend(s); - } - } - } - } - - }, false, null); - } - @Override public ShortStream reversed() { return newStream(new ShortIteratorEx() { @@ -526,6 +537,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -607,6 +620,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -709,6 +724,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -788,6 +805,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } diff --git a/src/com/landawn/abacus/util/stream/AbstractStream.java b/src/com/landawn/abacus/util/stream/AbstractStream.java index 97c3b67f..8501645b 100644 --- a/src/com/landawn/abacus/util/stream/AbstractStream.java +++ b/src/com/landawn/abacus/util/stream/AbstractStream.java @@ -95,9 +95,7 @@ abstract class AbstractStream extends Stream { @Override public Stream skip(final long n, final Consumer action) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } @@ -360,6 +358,76 @@ public Stream apply(T t) { }); } + @Override + public CharStream flattMapToChar(final Function mapper) { + return flatMapToChar(new Function() { + @Override + public CharStream apply(T t) { + return CharStream.of(mapper.apply(t)); + } + }); + } + + @Override + public ByteStream flattMapToByte(final Function mapper) { + return flatMapToByte(new Function() { + @Override + public ByteStream apply(T t) { + return ByteStream.of(mapper.apply(t)); + } + }); + } + + @Override + public ShortStream flattMapToShort(final Function mapper) { + return flatMapToShort(new Function() { + @Override + public ShortStream apply(T t) { + return ShortStream.of(mapper.apply(t)); + } + }); + } + + @Override + public IntStream flattMapToInt(final Function mapper) { + return flatMapToInt(new Function() { + @Override + public IntStream apply(T t) { + return IntStream.of(mapper.apply(t)); + } + }); + } + + @Override + public LongStream flattMapToLong(final Function mapper) { + return flatMapToLong(new Function() { + @Override + public LongStream apply(T t) { + return LongStream.of(mapper.apply(t)); + } + }); + } + + @Override + public FloatStream flattMapToFloat(final Function mapper) { + return flatMapToFloat(new Function() { + @Override + public FloatStream apply(T t) { + return FloatStream.of(mapper.apply(t)); + } + }); + } + + @Override + public DoubleStream flattMapToDouble(final Function mapper) { + return flatMapToDouble(new Function() { + @Override + public DoubleStream apply(T t) { + return DoubleStream.of(mapper.apply(t)); + } + }); + } + @Override public EntryStream flatMapToEntry(final Function>> mapper) { return EntryStream.of(flatMap(mapper)); @@ -449,7 +517,7 @@ public Stream> split(final int size) { return splitToList(size).map(new Function, Stream>() { @Override public Stream apply(List t) { - return new ArrayStream<>(toArray(t), 0, t.size(), sorted, cmp, null); + return new ArrayStream<>(Stream.toArray(t), 0, t.size(), sorted, cmp, null); } }); } @@ -469,7 +537,7 @@ public Stream> split(final Predicate predicate) { return splitToList(predicate).map(new Function, Stream>() { @Override public Stream apply(List t) { - return new ArrayStream<>(toArray(t), 0, t.size(), sorted, cmp, null); + return new ArrayStream<>(Stream.toArray(t), 0, t.size(), sorted, cmp, null); } }); } @@ -484,6 +552,207 @@ public Stream> splitToSet(final Predicate predicate) { return split(predicate, Suppliers. ofSet()); } + @Override + public Stream> splitBy(final Predicate where) { + N.checkArgNotNull(where); + + final IteratorEx iter = iteratorEx(); + + return newStream(new ObjIteratorEx>() { + private int cursor = 0; + private T next = null; + private boolean hasNext = false; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public Stream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + Stream result = null; + + if (cursor == 0) { + final List list = new ArrayList<>(); + + while (iter.hasNext()) { + next = iter.next(); + + if (where.test(next)) { + list.add(next); + } else { + hasNext = true; + break; + } + } + + result = new ArrayStream(Stream.toArray(list), 0, list.size(), sorted, cmp, null); + } else { + IteratorEx iterEx = iter; + + if (hasNext) { + iterEx = new ObjIteratorEx() { + private boolean isFirst = true; + + @Override + public boolean hasNext() { + return isFirst || iter.hasNext(); + } + + @Override + public T next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + if (isFirst) { + isFirst = false; + return next; + } else { + return iter.next(); + } + } + }; + } + + result = new IteratorStream(iterEx, sorted, cmp, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + iter.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.next(); + + if (where.test(next) == false) { + hasNext = true; + break; + } + } + } else { + iter.skip(Long.MAX_VALUE); + } + } else { + iter.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + + }, false, null); + } + + @Override + public Stream splitBy(final Predicate where, Collector collector) { + N.checkArgNotNull(where, "where"); + N.checkArgNotNull(collector, "collector"); + + final Supplier supplier = collector.supplier(); + final BiConsumer accumulator = collector.accumulator(); + final Function finisher = collector.finisher(); + + final IteratorEx iter = iteratorEx(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + private T next = null; + private boolean hasNext = false; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public R next() { + if (hasNext()) { + throw new NoSuchElementException(); + } + + final A container = supplier.get(); + + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.next(); + + if (where.test(next)) { + accumulator.accept(container, next); + } else { + hasNext = true; + break; + } + } + } else { + if (hasNext) { + accumulator.accept(container, next); + } + + while (iter.hasNext()) { + accumulator.accept(container, iter.next()); + } + } + + cursor++; + + return finisher.apply(container); + } + + @Override + public long count() { + iter.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + while (iter.hasNext()) { + next = iter.next(); + + if (where.test(next) == false) { + hasNext = true; + break; + } + } + } else { + iter.skip(Long.MAX_VALUE); + } + } else { + iter.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream> sliding(final int windowSize, final int increment) { return slidingToList(windowSize, increment).map(new Function, Stream>() { @@ -504,6 +773,11 @@ public > Stream sliding(final int windowSize, IntFunc return sliding(windowSize, 1, collectionSupplier); } + @Override + public Stream sliding(int windowSize, Collector collector) { + return sliding(windowSize, 1, collector); + } + @Override public Stream collapse(final BiPredicate collapsible, final BiFunction mergeFunction) { final ObjIteratorEx iter = iteratorEx(); @@ -1598,106 +1872,6 @@ public boolean test(T value) { })).iterator(), false, null); } - @Override - public Stream> splitAt(final int n) { - N.checkArgNotNegative(n, "n"); - - return newStream(new ObjIteratorEx>() { - private Stream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public Stream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final Iterator iter = AbstractStream.this.iteratorEx(); - final List list = new ArrayList<>(); - - while (list.size() < n && iter.hasNext()) { - list.add(iter.next()); - } - - a = new Stream[] { new ArrayStream((T[]) list.toArray(), 0, list.size(), sorted, cmp, null), - new IteratorStream(iter, sorted, cmp, null) }; - } - } - - }, false, null); - } - - @Override - public Stream> splitBy(final Predicate where) { - N.checkArgNotNull(where); - - return newStream(new ObjIteratorEx>() { - private Stream[] a = null; - private int cursor = 0; - - @Override - public boolean hasNext() { - init(); - - return cursor < 2; - } - - @Override - public Stream next() { - if (hasNext() == false) { - throw new NoSuchElementException(); - } - - return a[cursor++]; - } - - private void init() { - if (a == null) { - final Iterator iter = AbstractStream.this.iteratorEx(); - final List list = new ArrayList<>(); - T next = null; - Stream s = null; - - while (iter.hasNext()) { - next = iter.next(); - - if (where.test(next)) { - list.add(next); - } else { - s = Stream.of(next); - - break; - } - } - - a = new Stream[] { new ArrayStream((T[]) list.toArray(), 0, list.size(), sorted, cmp, null), - new IteratorStream(iter, sorted, cmp, null) }; - - if (s != null) { - if (sorted) { - a[1] = new IteratorStream(a[1].prepend(s).iteratorEx(), sorted, cmp, null); - } else { - a[1] = a[1].prepend(s); - } - } - } - } - - }, false, null); - } - @Override public Stream reversed() { return newStream(new ObjIteratorEx() { @@ -1738,6 +1912,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -1819,6 +1995,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -1933,6 +2111,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -2167,6 +2347,8 @@ public List next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n <= size - cursor ? cursor + (int) n : size; } @@ -2230,6 +2412,8 @@ public List next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n <= size - cursor ? cursor + (int) n : size; } @@ -2268,11 +2452,11 @@ public DataSet toDataSet() { } @Override - public DataSet toDataSet(boolean isFirstHeader) { + public DataSet toDataSet(boolean isFirstTitle) { assertNotClosed(); try { - if (isFirstHeader) { + if (isFirstTitle) { final ObjIterator iter = this.iterator(); if (iter.hasNext() == false) { @@ -2290,13 +2474,15 @@ public DataSet toDataSet(boolean isFirstHeader) { for (Object e : a) { columnNames.add(N.stringOf(e)); } - } else { + } else if (type.isCollection()) { final Collection c = (Collection) header; columnNames = new ArrayList<>(c.size()); for (Object e : c) { columnNames.add(N.stringOf(e)); } + } else { + throw new IllegalArgumentException("Unsupported header type: " + type.name()); } return N.newDataSet(columnNames, Iterators.toList(iter)); diff --git a/src/com/landawn/abacus/util/stream/ArrayByteStream.java b/src/com/landawn/abacus/util/stream/ArrayByteStream.java index 5437b135..c1a0a230 100644 --- a/src/com/landawn/abacus/util/stream/ArrayByteStream.java +++ b/src/com/landawn/abacus/util/stream/ArrayByteStream.java @@ -228,9 +228,9 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; - } + N.checkArgNotNegative(n, "n"); + + cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; } @Override @@ -265,15 +265,17 @@ public byte nextByte() { return mapper.applyAsByte(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public byte[] toArray() { @@ -307,15 +309,17 @@ public int nextInt() { return mapper.applyAsInt(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public int[] toArray() { @@ -349,15 +353,17 @@ public U next() { return mapper.apply(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -605,6 +611,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -640,6 +648,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -718,11 +728,11 @@ public ByteList next() { } @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); final ByteStream[] a = new ByteStream[2]; - final int middleIndex = n < toIndex - fromIndex ? fromIndex + n : toIndex; + final int middleIndex = where < toIndex - fromIndex ? fromIndex + where : toIndex; a[0] = middleIndex == fromIndex ? ByteStream.empty() : new ArrayByteStream(elements, fromIndex, middleIndex, sorted, null); a[1] = middleIndex == toIndex ? ByteStream.empty() : new ArrayByteStream(elements, middleIndex, toIndex, sorted, null); @@ -769,12 +779,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -819,12 +829,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -867,13 +877,9 @@ public byte[] toArray() { } @Override - public ByteStream limit(long maxSize) { + public ByteStream limit(final long maxSize) { N.checkArgNotNegative(maxSize, "maxSize"); - if (maxSize >= toIndex - fromIndex) { - return this; - } - return newStream(elements, fromIndex, (int) (fromIndex + maxSize), sorted); } @@ -881,10 +887,6 @@ public ByteStream limit(long maxSize) { public ByteStream skip(long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - if (n >= toIndex - fromIndex) { return newStream(elements, toIndex, toIndex, sorted); } else { @@ -1342,6 +1344,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < cursor - fromIndex ? cursor - (int) n : fromIndex; } @@ -1401,6 +1405,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n < len - cnt ? cnt + (int) n : len; } @@ -1545,6 +1551,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/stream/ArrayCharStream.java b/src/com/landawn/abacus/util/stream/ArrayCharStream.java index d9ae6a59..8b664b91 100644 --- a/src/com/landawn/abacus/util/stream/ArrayCharStream.java +++ b/src/com/landawn/abacus/util/stream/ArrayCharStream.java @@ -228,9 +228,9 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; - } + N.checkArgNotNegative(n, "n"); + + cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; } @Override @@ -265,15 +265,17 @@ public char nextChar() { return mapper.applyAsChar(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public char[] toArray() { @@ -307,15 +309,17 @@ public int nextInt() { return mapper.applyAsInt(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public int[] toArray() { @@ -349,15 +353,17 @@ public U next() { return mapper.apply(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -605,6 +611,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -640,6 +648,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -718,11 +728,11 @@ public CharList next() { } @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); final CharStream[] a = new CharStream[2]; - final int middleIndex = n < toIndex - fromIndex ? fromIndex + n : toIndex; + final int middleIndex = where < toIndex - fromIndex ? fromIndex + where : toIndex; a[0] = middleIndex == fromIndex ? CharStream.empty() : new ArrayCharStream(elements, fromIndex, middleIndex, sorted, null); a[1] = middleIndex == toIndex ? CharStream.empty() : new ArrayCharStream(elements, middleIndex, toIndex, sorted, null); @@ -769,12 +779,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -819,12 +829,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -867,13 +877,9 @@ public char[] toArray() { } @Override - public CharStream limit(long maxSize) { + public CharStream limit(final long maxSize) { N.checkArgNotNegative(maxSize, "maxSize"); - if (maxSize >= toIndex - fromIndex) { - return this; - } - return newStream(elements, fromIndex, (int) (fromIndex + maxSize), sorted); } @@ -881,10 +887,6 @@ public CharStream limit(long maxSize) { public CharStream skip(long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - if (n >= toIndex - fromIndex) { return newStream(elements, toIndex, toIndex, sorted); } else { @@ -1342,6 +1344,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < cursor - fromIndex ? cursor - (int) n : fromIndex; } @@ -1401,6 +1405,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n < len - cnt ? cnt + (int) n : len; } @@ -1545,6 +1551,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/stream/ArrayDoubleStream.java b/src/com/landawn/abacus/util/stream/ArrayDoubleStream.java index f34faa82..ce8de686 100644 --- a/src/com/landawn/abacus/util/stream/ArrayDoubleStream.java +++ b/src/com/landawn/abacus/util/stream/ArrayDoubleStream.java @@ -232,9 +232,9 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; - } + N.checkArgNotNegative(n, "n"); + + cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; } @Override @@ -269,15 +269,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public double[] toArray() { @@ -311,15 +313,17 @@ public int nextInt() { return mapper.applyAsInt(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public int[] toArray() { @@ -353,15 +357,17 @@ public long nextLong() { return mapper.applyAsLong(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public long[] toArray() { @@ -395,15 +401,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public float[] toArray() { @@ -437,15 +445,17 @@ public U next() { return mapper.apply(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -829,6 +839,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -864,6 +876,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -942,11 +956,11 @@ public DoubleList next() { } @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); final DoubleStream[] a = new DoubleStream[2]; - final int middleIndex = n < toIndex - fromIndex ? fromIndex + n : toIndex; + final int middleIndex = where < toIndex - fromIndex ? fromIndex + where : toIndex; a[0] = middleIndex == fromIndex ? DoubleStream.empty() : new ArrayDoubleStream(elements, fromIndex, middleIndex, sorted, null); a[1] = middleIndex == toIndex ? DoubleStream.empty() : new ArrayDoubleStream(elements, middleIndex, toIndex, sorted, null); @@ -993,12 +1007,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -1043,12 +1057,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -1103,6 +1117,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -1175,13 +1191,9 @@ public double[] toArray() { } @Override - public DoubleStream limit(long maxSize) { + public DoubleStream limit(final long maxSize) { N.checkArgNotNegative(maxSize, "maxSize"); - if (maxSize >= toIndex - fromIndex) { - return this; - } - return newStream(elements, fromIndex, (int) (fromIndex + maxSize), sorted); } @@ -1189,10 +1201,6 @@ public DoubleStream limit(long maxSize) { public DoubleStream skip(long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - if (n >= toIndex - fromIndex) { return newStream(elements, toIndex, toIndex, sorted); } else { @@ -1624,6 +1632,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < cursor - fromIndex ? cursor - (int) n : fromIndex; } @@ -1683,6 +1693,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n < len - cnt ? cnt + (int) n : len; } diff --git a/src/com/landawn/abacus/util/stream/ArrayFloatStream.java b/src/com/landawn/abacus/util/stream/ArrayFloatStream.java index 6a186754..d745818d 100644 --- a/src/com/landawn/abacus/util/stream/ArrayFloatStream.java +++ b/src/com/landawn/abacus/util/stream/ArrayFloatStream.java @@ -232,9 +232,9 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; - } + N.checkArgNotNegative(n, "n"); + + cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; } @Override @@ -269,15 +269,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public float[] toArray() { @@ -311,15 +313,17 @@ public int nextInt() { return mapper.applyAsInt(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public int[] toArray() { @@ -353,15 +357,17 @@ public long nextLong() { return mapper.applyAsLong(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public long[] toArray() { @@ -395,15 +401,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public double[] toArray() { @@ -437,15 +445,17 @@ public U next() { return mapper.apply(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -829,6 +839,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -864,6 +876,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -942,11 +956,11 @@ public FloatList next() { } @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); final FloatStream[] a = new FloatStream[2]; - final int middleIndex = n < toIndex - fromIndex ? fromIndex + n : toIndex; + final int middleIndex = where < toIndex - fromIndex ? fromIndex + where : toIndex; a[0] = middleIndex == fromIndex ? FloatStream.empty() : new ArrayFloatStream(elements, fromIndex, middleIndex, sorted, null); a[1] = middleIndex == toIndex ? FloatStream.empty() : new ArrayFloatStream(elements, middleIndex, toIndex, sorted, null); @@ -993,12 +1007,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -1043,12 +1057,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -1103,6 +1117,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -1175,13 +1191,9 @@ public float[] toArray() { } @Override - public FloatStream limit(long maxSize) { + public FloatStream limit(final long maxSize) { N.checkArgNotNegative(maxSize, "maxSize"); - if (maxSize >= toIndex - fromIndex) { - return this; - } - return newStream(elements, fromIndex, (int) (fromIndex + maxSize), sorted); } @@ -1189,10 +1201,6 @@ public FloatStream limit(long maxSize) { public FloatStream skip(long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - if (n >= toIndex - fromIndex) { return newStream(elements, toIndex, toIndex, sorted); } else { @@ -1624,6 +1632,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < cursor - fromIndex ? cursor - (int) n : fromIndex; } @@ -1683,6 +1693,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n < len - cnt ? cnt + (int) n : len; } @@ -1827,6 +1839,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/stream/ArrayIntStream.java b/src/com/landawn/abacus/util/stream/ArrayIntStream.java index ead14763..7d2a99d9 100644 --- a/src/com/landawn/abacus/util/stream/ArrayIntStream.java +++ b/src/com/landawn/abacus/util/stream/ArrayIntStream.java @@ -239,9 +239,9 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; - } + N.checkArgNotNegative(n, "n"); + + cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; } @Override @@ -276,15 +276,17 @@ public int nextInt() { return mapper.applyAsInt(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public int[] toArray() { @@ -318,15 +320,17 @@ public char nextChar() { return mapper.applyAsChar(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public char[] toArray() { @@ -360,15 +364,17 @@ public byte nextByte() { return mapper.applyAsByte(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public byte[] toArray() { @@ -402,15 +408,17 @@ public short nextShort() { return mapper.applyAsShort(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public short[] toArray() { @@ -444,15 +452,17 @@ public long nextLong() { return mapper.applyAsLong(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public long[] toArray() { @@ -486,15 +496,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public float[] toArray() { @@ -528,15 +540,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public double[] toArray() { @@ -570,15 +584,17 @@ public U next() { return mapper.apply(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -1166,6 +1182,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -1201,6 +1219,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -1279,11 +1299,11 @@ public IntList next() { } @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); final IntStream[] a = new IntStream[2]; - final int middleIndex = n < toIndex - fromIndex ? fromIndex + n : toIndex; + final int middleIndex = where < toIndex - fromIndex ? fromIndex + where : toIndex; a[0] = middleIndex == fromIndex ? IntStream.empty() : new ArrayIntStream(elements, fromIndex, middleIndex, sorted, null); a[1] = middleIndex == toIndex ? IntStream.empty() : new ArrayIntStream(elements, middleIndex, toIndex, sorted, null); @@ -1329,12 +1349,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -1379,12 +1399,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } @@ -1440,6 +1460,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -1512,13 +1534,9 @@ public int[] toArray() { } @Override - public IntStream limit(long maxSize) { + public IntStream limit(final long maxSize) { N.checkArgNotNegative(maxSize, "maxSize"); - if (maxSize >= toIndex - fromIndex) { - return this; - } - return newStream(elements, fromIndex, (int) (fromIndex + maxSize), sorted); } @@ -1526,10 +1544,6 @@ public IntStream limit(long maxSize) { public IntStream skip(long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - if (n >= toIndex - fromIndex) { return newStream(elements, toIndex, toIndex, sorted); } else { @@ -1987,6 +2001,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < cursor - fromIndex ? cursor - (int) n : fromIndex; } @@ -2046,6 +2062,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n < len - cnt ? cnt + (int) n : len; } @@ -2190,6 +2208,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -2232,6 +2252,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -2274,6 +2296,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/stream/ArrayLongStream.java b/src/com/landawn/abacus/util/stream/ArrayLongStream.java index b78a4617..9246d539 100644 --- a/src/com/landawn/abacus/util/stream/ArrayLongStream.java +++ b/src/com/landawn/abacus/util/stream/ArrayLongStream.java @@ -233,9 +233,9 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; - } + N.checkArgNotNegative(n, "n"); + + cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; } @Override @@ -270,15 +270,17 @@ public long nextLong() { return mapper.applyAsLong(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public long[] toArray() { @@ -312,15 +314,17 @@ public int nextInt() { return mapper.applyAsInt(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public int[] toArray() { @@ -354,15 +358,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public float[] toArray() { @@ -396,15 +402,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public double[] toArray() { @@ -438,15 +446,17 @@ public U next() { return mapper.apply(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -830,6 +840,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -865,6 +877,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -943,11 +957,11 @@ public LongList next() { } @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); final LongStream[] a = new LongStream[2]; - final int middleIndex = n < toIndex - fromIndex ? fromIndex + n : toIndex; + final int middleIndex = where < toIndex - fromIndex ? fromIndex + where : toIndex; a[0] = middleIndex == fromIndex ? LongStream.empty() : new ArrayLongStream(elements, fromIndex, middleIndex, sorted, null); a[1] = middleIndex == toIndex ? LongStream.empty() : new ArrayLongStream(elements, middleIndex, toIndex, sorted, null); @@ -994,12 +1008,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -1044,12 +1058,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -1104,6 +1118,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -1176,13 +1192,9 @@ public long[] toArray() { } @Override - public LongStream limit(long maxSize) { + public LongStream limit(final long maxSize) { N.checkArgNotNegative(maxSize, "maxSize"); - if (maxSize >= toIndex - fromIndex) { - return this; - } - return newStream(elements, fromIndex, (int) (fromIndex + maxSize), sorted); } @@ -1190,10 +1202,6 @@ public LongStream limit(long maxSize) { public LongStream skip(long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - if (n >= toIndex - fromIndex) { return newStream(elements, toIndex, toIndex, sorted); } else { @@ -1651,6 +1659,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < cursor - fromIndex ? cursor - (int) n : fromIndex; } @@ -1710,6 +1720,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n < len - cnt ? cnt + (int) n : len; } @@ -1854,6 +1866,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1896,6 +1910,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/stream/ArrayShortStream.java b/src/com/landawn/abacus/util/stream/ArrayShortStream.java index 22b3afa9..f455a83b 100644 --- a/src/com/landawn/abacus/util/stream/ArrayShortStream.java +++ b/src/com/landawn/abacus/util/stream/ArrayShortStream.java @@ -229,9 +229,9 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; - } + N.checkArgNotNegative(n, "n"); + + cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; } @Override @@ -266,15 +266,17 @@ public short nextShort() { return mapper.applyAsShort(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public short[] toArray() { @@ -308,15 +310,17 @@ public int nextInt() { return mapper.applyAsInt(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public int[] toArray() { @@ -350,15 +354,17 @@ public U next() { return mapper.apply(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -606,6 +612,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -641,6 +649,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -719,11 +729,11 @@ public ShortList next() { } @Override - public Stream splitAt(final int n) { - N.checkArgNotNegative(n, "n"); + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); final ShortStream[] a = new ShortStream[2]; - final int middleIndex = n < toIndex - fromIndex ? fromIndex + n : toIndex; + final int middleIndex = where < toIndex - fromIndex ? fromIndex + where : toIndex; a[0] = middleIndex == fromIndex ? ShortStream.empty() : new ArrayShortStream(elements, fromIndex, middleIndex, sorted, null); a[1] = middleIndex == toIndex ? ShortStream.empty() : new ArrayShortStream(elements, middleIndex, toIndex, sorted, null); @@ -770,12 +780,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -820,12 +830,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -880,6 +890,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -952,13 +964,9 @@ public short[] toArray() { } @Override - public ShortStream limit(long maxSize) { + public ShortStream limit(final long maxSize) { N.checkArgNotNegative(maxSize, "maxSize"); - if (maxSize >= toIndex - fromIndex) { - return this; - } - return newStream(elements, fromIndex, (int) (fromIndex + maxSize), sorted); } @@ -966,10 +974,6 @@ public ShortStream limit(long maxSize) { public ShortStream skip(long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - if (n >= toIndex - fromIndex) { return newStream(elements, toIndex, toIndex, sorted); } else { @@ -1427,6 +1431,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < cursor - fromIndex ? cursor - (int) n : fromIndex; } @@ -1486,6 +1492,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n < len - cnt ? cnt + (int) n : len; } @@ -1630,6 +1638,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } diff --git a/src/com/landawn/abacus/util/stream/ArrayStream.java b/src/com/landawn/abacus/util/stream/ArrayStream.java index cca300a5..30ab7967 100644 --- a/src/com/landawn/abacus/util/stream/ArrayStream.java +++ b/src/com/landawn/abacus/util/stream/ArrayStream.java @@ -240,9 +240,9 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; - } + N.checkArgNotNegative(n, "n"); + + cursor = n <= (toIndex - cursor) / stepp ? cursor + (int) (n * stepp) : toIndex; } @Override @@ -278,15 +278,17 @@ public R next() { return mapper.apply(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -328,6 +330,8 @@ public A[] toArray(A[] a) { // // // // @Override // // public void skip(long n) { + // // N.checkArgNotNegative(n, "n"); + // // // // cursor = n < count() ? cursor + (int) n * 2 : toIndex; // // } // @@ -376,6 +380,8 @@ public A[] toArray(A[] a) { // // // // @Override // // public void skip(long n) { + // // N.checkArgNotNegative(n, "n"); + // // // // cursor = n < count() ? cursor + (int) n * 3 : toIndex; // // } // @@ -424,29 +430,28 @@ public R next() { return result; } - // @Override - // public long count() { - // if (toIndex - cursor == 0) { - // return 0; - // } else if (toIndex - cursor <= windowSize) { - // return 1; - // } else { - // final long len = (toIndex - cursor) - windowSize; - // return 1 + (len % increment == 0 ? len / increment : len / increment + 1); - // } - // } + // @Override + // public long count() { + // if (toIndex - cursor == 0) { + // return 0; + // } else if (toIndex - cursor <= windowSize) { + // return 1; + // } else { + // final long len = (toIndex - cursor) - windowSize; + // return 1 + (len % increment == 0 ? len / increment : len / increment + 1); + // } + // } // - // @Override - // public void skip(long n) { - // if (n > 0) { - // if (n >= count()) { - // cursor = toIndex; - // } else { - // cursor += n * increment; - // } - // } - // } - + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // if (n >= count()) { + // cursor = toIndex; + // } else { + // cursor += n * increment; + // } + // } }, false, null); } @@ -478,29 +483,28 @@ public R next() { return result; } - // @Override - // public long count() { - // if (toIndex - cursor == 0) { - // return 0; - // } else if (toIndex - cursor <= windowSize) { - // return 1; - // } else { - // final long len = (toIndex - cursor) - windowSize; - // return 1 + (len % increment == 0 ? len / increment : len / increment + 1); - // } - // } + // @Override + // public long count() { + // if (toIndex - cursor == 0) { + // return 0; + // } else if (toIndex - cursor <= windowSize) { + // return 1; + // } else { + // final long len = (toIndex - cursor) - windowSize; + // return 1 + (len % increment == 0 ? len / increment : len / increment + 1); + // } + // } // - // @Override - // public void skip(long n) { - // if (n > 0) { - // if (n >= count()) { - // cursor = toIndex; - // } else { - // cursor += n * increment; - // } - // } - // } - + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // if (n >= count()) { + // cursor = toIndex; + // } else { + // cursor += n * increment; + // } + // } }, false, null); } @@ -534,15 +538,17 @@ public T next() { } } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public long count() { @@ -556,10 +562,14 @@ public long count() { @Override public void skip(long n) { - if (hasNext()) { - next(); - n -= 1; - cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + N.checkArgNotNegative(n, "n"); + + if (n > 0) { + if (hasNext()) { + next(); + n -= 1; + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + } } } @@ -612,15 +622,17 @@ public R next() { } } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -671,15 +683,17 @@ public T next() { } } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -726,15 +740,17 @@ public R next() { } } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public A[] toArray(A[] a) { @@ -772,15 +788,17 @@ public char nextChar() { return mapper.applyAsChar(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public char[] toArray() { @@ -814,15 +832,17 @@ public byte nextByte() { return mapper.applyAsByte(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public byte[] toArray() { @@ -856,15 +876,17 @@ public short nextShort() { return mapper.applyAsShort(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public short[] toArray() { @@ -898,15 +920,17 @@ public int nextInt() { return mapper.applyAsInt(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public int[] toArray() { @@ -940,15 +964,17 @@ public long nextLong() { return mapper.applyAsLong(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public long[] toArray() { @@ -982,15 +1008,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public float[] toArray() { @@ -1024,15 +1052,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements[cursor++]); } - // @Override - // public long count() { - // return toIndex - cursor; - // } + // @Override + // public long count() { + // return toIndex - cursor; + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; - // } + // cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; + // } @Override public double[] toArray() { @@ -1620,6 +1650,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -1644,7 +1676,7 @@ public List next() { throw new NoSuchElementException(); } - return N.asList(N.copyOfRange(elements, cursor, (cursor = size < toIndex - cursor ? cursor + size : toIndex))); + return Stream.createList(N.copyOfRange(elements, cursor, (cursor = size < toIndex - cursor ? cursor + size : toIndex))); } @Override @@ -1655,6 +1687,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -1681,8 +1715,8 @@ public C next() { final C result = collectionSupplier.apply(toIndex - cursor > size ? size : toIndex - cursor); - for (int i = cursor, to = (cursor = size < toIndex - cursor ? cursor + size : toIndex); i < to; i++) { - result.add(elements[i]); + for (int to = (cursor = size < toIndex - cursor ? cursor + size : toIndex); cursor < to; cursor++) { + result.add(elements[cursor]); } return result; @@ -1696,6 +1730,56 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + final long len = toIndex - cursor; + cursor = n <= len / size ? cursor + (int) n * size : toIndex; + } + }, false, null); + } + + @Override + public Stream split(final int size, final Collector collector) { + N.checkArgPositive(size, "size"); + N.checkArgNotNull(collector); + + final Supplier supplier = collector.supplier(); + final BiConsumer accumulator = collector.accumulator(); + final Function finisher = collector.finisher(); + + return newStream(new ObjIteratorEx() { + private int cursor = fromIndex; + + @Override + public boolean hasNext() { + return cursor < toIndex; + } + + @Override + public R next() { + if (cursor >= toIndex) { + throw new NoSuchElementException(); + } + + final A container = supplier.get(); + + for (int to = (cursor = size < toIndex - cursor ? cursor + size : toIndex); cursor < to; cursor++) { + accumulator.accept(container, elements[cursor]); + } + + return finisher.apply(container); + } + + @Override + public long count() { + final long len = toIndex - cursor; + return len % size == 0 ? len / size : len / size + 1; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + final long len = toIndex - cursor; cursor = n <= len / size ? cursor + (int) n * size : toIndex; } @@ -1769,7 +1853,7 @@ public List next() { } } - return N.asList(N.copyOfRange(elements, from, cursor)); + return Stream.createList(N.copyOfRange(elements, from, cursor)); } }, false, null); } @@ -1792,12 +1876,14 @@ public C next() { } final C result = collectionSupplier.get(); + boolean isFirst = true; - while (cursor < toIndex) { - if (result.size() == 0) { + while (isFirst) { + if (isFirst) { preCondition = predicate.test(elements[cursor]); result.add(elements[cursor]); cursor++; + isFirst = false; } else if (predicate.test(elements[cursor]) == preCondition) { result.add(elements[cursor]); cursor++; @@ -1809,22 +1895,116 @@ public C next() { return result; } + }, false, null); + } + + @Override + public Stream split(final Predicate predicate, final Collector collector) { + N.checkArgNotNull(collector); + + final Supplier supplier = collector.supplier(); + final BiConsumer accumulator = collector.accumulator(); + final Function finisher = collector.finisher(); + + return newStream(new ObjIteratorEx() { + private int cursor = fromIndex; + private boolean preCondition = false; + + @Override + public boolean hasNext() { + return cursor < toIndex; + } + @Override + public R next() { + if (cursor >= toIndex) { + throw new NoSuchElementException(); + } + + final A container = supplier.get(); + boolean isFirst = true; + + while (cursor < toIndex) { + if (isFirst) { + preCondition = predicate.test(elements[cursor]); + accumulator.accept(container, elements[cursor]); + cursor++; + isFirst = false; + } else if (predicate.test(elements[cursor]) == preCondition) { + accumulator.accept(container, elements[cursor]); + cursor++; + } else { + + break; + } + } + + return finisher.apply(container); + } }, false, null); } @Override - public Stream> splitAt(final int n) { - N.checkArgNotNegative(n, "n"); + public Stream> splitAt(final int where) { + N.checkArgNotNegative(where, "where"); final Stream[] a = new Stream[2]; - final int middleIndex = n < toIndex - fromIndex ? fromIndex + n : toIndex; + final int middleIndex = where < toIndex - fromIndex ? fromIndex + where : toIndex; a[0] = middleIndex == fromIndex ? (Stream) Stream.empty() : new ArrayStream<>(elements, fromIndex, middleIndex, sorted, cmp, null); a[1] = middleIndex == toIndex ? (Stream) Stream.empty() : new ArrayStream<>(elements, middleIndex, toIndex, sorted, cmp, null); return newStream(a, false, null); } + @Override + public Stream splitAt(final int where, final Collector collector) { + N.checkArgNotNegative(where, "where"); + N.checkArgNotNull(collector, "collector"); + + final Supplier supplier = collector.supplier(); + final BiConsumer accumulator = collector.accumulator(); + final Function finisher = collector.finisher(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public R next() { + if (hasNext()) { + throw new NoSuchElementException(); + } + + final A container = supplier.get(); + final int middleIndex = where < toIndex - fromIndex ? fromIndex + where : toIndex; + + for (int i = cursor == 0 ? fromIndex : middleIndex, to = cursor == 0 ? middleIndex : toIndex; i < to; i++) { + accumulator.accept(container, elements[i]); + } + + cursor++; + + return finisher.apply(container); + } + + @Override + public long count() { + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream> sliding(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); @@ -1864,12 +2044,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } }, false, null); @@ -1893,7 +2073,7 @@ public List next() { throw new NoSuchElementException(); } - final List result = N.asList(N.copyOfRange(elements, cursor, windowSize < toIndex - cursor ? cursor + windowSize : toIndex)); + final List result = Stream.createList(N.copyOfRange(elements, cursor, windowSize < toIndex - cursor ? cursor + windowSize : toIndex)); cursor = increment < toIndex - cursor && windowSize < toIndex - cursor ? cursor + increment : toIndex; @@ -1914,12 +2094,12 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } @@ -1929,6 +2109,7 @@ public void skip(long n) { @Override public > Stream sliding(final int windowSize, final int increment, final IntFunction collectionSupplier) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); + N.checkArgNotNull(collectionSupplier); return newStream(new ObjIteratorEx() { private int cursor = fromIndex; @@ -1969,12 +2150,72 @@ public long count() { @Override public void skip(long n) { - if (n > 0) { - if (n >= count()) { - cursor = toIndex; - } else { - cursor += n * increment; - } + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; + } + } + + }, false, null); + } + + @Override + public Stream sliding(final int windowSize, final int increment, final Collector collector) { + N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); + N.checkArgNotNull(collector); + + final Supplier supplier = collector.supplier(); + final BiConsumer accumulator = collector.accumulator(); + final Function finisher = collector.finisher(); + + return newStream(new ObjIteratorEx() { + private int cursor = fromIndex; + + @Override + public boolean hasNext() { + return cursor < toIndex; + } + + @Override + public R next() { + if (cursor >= toIndex) { + throw new NoSuchElementException(); + } + + final A container = supplier.get(); + + for (int i = cursor, to = windowSize < toIndex - cursor ? cursor + windowSize : toIndex; i < to; i++) { + accumulator.accept(container, elements[i]); + } + + cursor = increment < toIndex - cursor && windowSize < toIndex - cursor ? cursor + increment : toIndex; + + return finisher.apply(container); + } + + @Override + public long count() { + if (toIndex - cursor == 0) { + return 0; + } else if (toIndex - cursor <= windowSize) { + return 1; + } else { + final long len = (toIndex - cursor) - windowSize; + return 1 + (len % increment == 0 ? len / increment : len / increment + 1); + } + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n >= count()) { + cursor = toIndex; + } else { + cursor += n * increment; } } @@ -2030,6 +2271,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -2097,13 +2340,9 @@ public A[] toArray(A[] a) { } @Override - public Stream limit(long maxSize) { + public Stream limit(final long maxSize) { N.checkArgNotNegative(maxSize, "maxSize"); - if (maxSize >= toIndex - fromIndex) { - return this; - } - return newStream(elements, fromIndex, (int) (fromIndex + maxSize), sorted, cmp); } @@ -2111,10 +2350,6 @@ public Stream limit(long maxSize) { public Stream skip(long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - if (n >= toIndex - fromIndex) { return newStream(elements, toIndex, toIndex, sorted, cmp); } else { @@ -2216,7 +2451,7 @@ public List toList() { assertNotClosed(); try { - // return N.asList(N.copyOfRange(elements, fromIndex, toIndex)); + // return Stream.createList(N.copyOfRange(elements, fromIndex, toIndex)); if (fromIndex == 0 && toIndex == elements.length && elements.length > 9) { return new ArrayList<>(Arrays.asList(elements)); @@ -2572,9 +2807,7 @@ public Stream last(final int n) { @Override public Stream skipLast(int n) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } @@ -2670,6 +2903,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < cursor - fromIndex ? cursor - (int) n : fromIndex; } @@ -2729,6 +2964,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n < len - cnt ? cnt + (int) n : len; } diff --git a/src/com/landawn/abacus/util/stream/ByteIteratorEx.java b/src/com/landawn/abacus/util/stream/ByteIteratorEx.java index 0b5ba3f8..b69b05bc 100644 --- a/src/com/landawn/abacus/util/stream/ByteIteratorEx.java +++ b/src/com/landawn/abacus/util/stream/ByteIteratorEx.java @@ -95,6 +95,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -179,6 +181,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -257,6 +261,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -302,6 +308,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + iteratorEx.skip(n); } @@ -337,6 +345,8 @@ public void close() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + while (n > 0 && hasNext()) { nextByte(); n--; diff --git a/src/com/landawn/abacus/util/stream/ByteStream.java b/src/com/landawn/abacus/util/stream/ByteStream.java index e44e4a25..e41991b8 100644 --- a/src/com/landawn/abacus/util/stream/ByteStream.java +++ b/src/com/landawn/abacus/util/stream/ByteStream.java @@ -666,6 +666,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n; } @@ -721,6 +723,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n * by; } @@ -772,6 +776,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n; } @@ -829,6 +835,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n * by; } @@ -879,6 +887,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; } diff --git a/src/com/landawn/abacus/util/stream/CharIteratorEx.java b/src/com/landawn/abacus/util/stream/CharIteratorEx.java index 9bb0b2cb..4e2d2d77 100644 --- a/src/com/landawn/abacus/util/stream/CharIteratorEx.java +++ b/src/com/landawn/abacus/util/stream/CharIteratorEx.java @@ -95,6 +95,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -179,6 +181,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -257,6 +261,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -302,6 +308,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + iteratorEx.skip(n); } @@ -337,6 +345,8 @@ public void close() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + while (n > 0 && hasNext()) { nextChar(); n--; diff --git a/src/com/landawn/abacus/util/stream/CharStream.java b/src/com/landawn/abacus/util/stream/CharStream.java index 2433d4b2..b2e9c692 100644 --- a/src/com/landawn/abacus/util/stream/CharStream.java +++ b/src/com/landawn/abacus/util/stream/CharStream.java @@ -912,6 +912,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n; } @@ -967,6 +969,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n * by; } @@ -1018,6 +1022,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n; } @@ -1075,6 +1081,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n * by; } @@ -1125,6 +1133,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; } diff --git a/src/com/landawn/abacus/util/stream/DoubleIteratorEx.java b/src/com/landawn/abacus/util/stream/DoubleIteratorEx.java index ceddf949..0d6257b0 100644 --- a/src/com/landawn/abacus/util/stream/DoubleIteratorEx.java +++ b/src/com/landawn/abacus/util/stream/DoubleIteratorEx.java @@ -95,6 +95,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -179,6 +181,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -257,6 +261,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -302,6 +308,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + iteratorEx.skip(n); } @@ -337,6 +345,8 @@ public void close() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + while (n > 0 && hasNext()) { nextDouble(); n--; diff --git a/src/com/landawn/abacus/util/stream/DoubleStream.java b/src/com/landawn/abacus/util/stream/DoubleStream.java index 5d3ed4dc..7708ba27 100644 --- a/src/com/landawn/abacus/util/stream/DoubleStream.java +++ b/src/com/landawn/abacus/util/stream/DoubleStream.java @@ -718,6 +718,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -760,6 +762,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; } diff --git a/src/com/landawn/abacus/util/stream/EntryStream.java b/src/com/landawn/abacus/util/stream/EntryStream.java index 7073e72b..85e13636 100644 --- a/src/com/landawn/abacus/util/stream/EntryStream.java +++ b/src/com/landawn/abacus/util/stream/EntryStream.java @@ -847,8 +847,8 @@ public EntryStream skip(long n) { } @SequentialOnly - public EntryStream limit(long n) { - return of(s.limit(n)); + public EntryStream limit(final long maxSize) { + return of(s.limit(maxSize)); } @ParallelSupported diff --git a/src/com/landawn/abacus/util/stream/FloatIteratorEx.java b/src/com/landawn/abacus/util/stream/FloatIteratorEx.java index 7d187fee..babd8137 100644 --- a/src/com/landawn/abacus/util/stream/FloatIteratorEx.java +++ b/src/com/landawn/abacus/util/stream/FloatIteratorEx.java @@ -95,6 +95,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -179,6 +181,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -257,6 +261,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -302,6 +308,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + iteratorEx.skip(n); } @@ -337,6 +345,8 @@ public void close() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + while (n > 0 && hasNext()) { nextFloat(); n--; diff --git a/src/com/landawn/abacus/util/stream/FloatStream.java b/src/com/landawn/abacus/util/stream/FloatStream.java index bbfdbdf3..396772d9 100644 --- a/src/com/landawn/abacus/util/stream/FloatStream.java +++ b/src/com/landawn/abacus/util/stream/FloatStream.java @@ -660,6 +660,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; } diff --git a/src/com/landawn/abacus/util/stream/IntIteratorEx.java b/src/com/landawn/abacus/util/stream/IntIteratorEx.java index 1cdfee89..2b1d36df 100644 --- a/src/com/landawn/abacus/util/stream/IntIteratorEx.java +++ b/src/com/landawn/abacus/util/stream/IntIteratorEx.java @@ -95,6 +95,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -179,6 +181,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -257,6 +261,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -302,6 +308,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + iteratorEx.skip(n); } @@ -337,6 +345,8 @@ public void close() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + while (n > 0 && hasNext()) { nextInt(); n--; diff --git a/src/com/landawn/abacus/util/stream/IntStream.java b/src/com/landawn/abacus/util/stream/IntStream.java index cce49a05..67975946 100644 --- a/src/com/landawn/abacus/util/stream/IntStream.java +++ b/src/com/landawn/abacus/util/stream/IntStream.java @@ -783,6 +783,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -835,6 +837,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -887,6 +891,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -928,6 +934,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n; } @@ -983,6 +991,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n * by; } @@ -1034,6 +1044,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n; } @@ -1091,6 +1103,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n * by; } @@ -1141,6 +1155,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; } diff --git a/src/com/landawn/abacus/util/stream/IteratorByteStream.java b/src/com/landawn/abacus/util/stream/IteratorByteStream.java index 84bd12b2..4b87128b 100644 --- a/src/com/landawn/abacus/util/stream/IteratorByteStream.java +++ b/src/com/landawn/abacus/util/stream/IteratorByteStream.java @@ -219,15 +219,17 @@ public byte nextByte() { return mapper.applyAsByte(elements.nextByte()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -244,15 +246,17 @@ public int nextInt() { return mapper.applyAsInt(elements.nextByte()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -269,15 +273,17 @@ public U next() { return mapper.apply(elements.nextByte()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false, null); } @@ -515,7 +521,9 @@ public long count() { @Override public void skip(long n) { - elements.skip(n >= Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); } }, false, null); } @@ -564,23 +572,90 @@ public ByteList next() { }, false, null); } + @Override + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public ByteStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + ByteStream result = null; + + if (cursor == 0) { + final ByteList list = new ByteList(); + int cnt = 0; + + while (cnt++ < where && elements.hasNext()) { + list.add(elements.nextByte()); + } + + result = new ArrayByteStream(list.array(), 0, list.size(), sorted, null); + } else { + result = new IteratorByteStream(elements, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + elements.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + elements.skip(where); + } else { + elements.skip(Long.MAX_VALUE); + } + } else { + elements.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream slidingToList(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); return newStream(new ObjIteratorEx() { private ByteList prev = null; + private boolean toSkip = false; @Override public boolean hasNext() { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.nextByte(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -606,10 +681,12 @@ public ByteList next() { } } else { final byte[] dest = new byte[windowSize]; - N.copy(prev.trimToSize().array(), windowSize - cnt, dest, 0, cnt); + N.copy(prev.array(), windowSize - cnt, dest, 0, cnt); result = ByteList.of(dest, cnt); } - } else { + } + + if (result == null) { result = new ByteList(windowSize); } @@ -617,9 +694,62 @@ public ByteList next() { result.add(elements.nextByte()); } + toSkip = increment > windowSize; + return prev = result; } + @Override + public long count() { + final int prevSize = increment >= windowSize ? 0 : (prev == null ? 0 : prev.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) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } + + if (increment >= windowSize) { + elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment); + } else { + final ByteList tmp = new ByteList(windowSize); + + if (N.isNullOrEmpty(prev)) { + 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 : (prev == null ? 0 : prev.size()); + + if (m < prevSize) { + tmp.addAll(prev.copy((int) m, prevSize)); + } else { + elements.skip(m - prevSize); + } + } + + int cnt = tmp.size(); + + while (cnt++ < windowSize && elements.hasNext()) { + tmp.add(elements.nextByte()); + } + + prev = tmp; + } + } }, false, null); } @@ -665,6 +795,8 @@ public byte nextByte() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -674,10 +806,6 @@ public void skip(long n) { public ByteStream skip(final long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - return newStream(new ByteIteratorEx() { private boolean skipped = false; @@ -1264,6 +1392,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); diff --git a/src/com/landawn/abacus/util/stream/IteratorCharStream.java b/src/com/landawn/abacus/util/stream/IteratorCharStream.java index 064a9b14..c5323964 100644 --- a/src/com/landawn/abacus/util/stream/IteratorCharStream.java +++ b/src/com/landawn/abacus/util/stream/IteratorCharStream.java @@ -219,15 +219,17 @@ public char nextChar() { return mapper.applyAsChar(elements.nextChar()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -244,15 +246,17 @@ public int nextInt() { return mapper.applyAsInt(elements.nextChar()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -269,15 +273,17 @@ public U next() { return mapper.apply(elements.nextChar()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false, null); } @@ -515,7 +521,9 @@ public long count() { @Override public void skip(long n) { - elements.skip(n >= Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); } }, false, null); } @@ -564,23 +572,90 @@ public CharList next() { }, false, null); } + @Override + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public CharStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + CharStream result = null; + + if (cursor == 0) { + final CharList list = new CharList(); + int cnt = 0; + + while (cnt++ < where && elements.hasNext()) { + list.add(elements.nextChar()); + } + + result = new ArrayCharStream(list.array(), 0, list.size(), sorted, null); + } else { + result = new IteratorCharStream(elements, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + elements.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + elements.skip(where); + } else { + elements.skip(Long.MAX_VALUE); + } + } else { + elements.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream slidingToList(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); return newStream(new ObjIteratorEx() { private CharList prev = null; + private boolean toSkip = false; @Override public boolean hasNext() { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.nextChar(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -606,10 +681,12 @@ public CharList next() { } } else { final char[] dest = new char[windowSize]; - N.copy(prev.trimToSize().array(), windowSize - cnt, dest, 0, cnt); + N.copy(prev.array(), windowSize - cnt, dest, 0, cnt); result = CharList.of(dest, cnt); } - } else { + } + + if (result == null) { result = new CharList(windowSize); } @@ -617,9 +694,62 @@ public CharList next() { result.add(elements.nextChar()); } + toSkip = increment > windowSize; + return prev = result; } + @Override + public long count() { + final int prevSize = increment >= windowSize ? 0 : (prev == null ? 0 : prev.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) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } + + if (increment >= windowSize) { + elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment); + } else { + final CharList tmp = new CharList(windowSize); + + if (N.isNullOrEmpty(prev)) { + 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 : (prev == null ? 0 : prev.size()); + + if (m < prevSize) { + tmp.addAll(prev.copy((int) m, prevSize)); + } else { + elements.skip(m - prevSize); + } + } + + int cnt = tmp.size(); + + while (cnt++ < windowSize && elements.hasNext()) { + tmp.add(elements.nextChar()); + } + + prev = tmp; + } + } }, false, null); } @@ -665,6 +795,8 @@ public char nextChar() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -674,10 +806,6 @@ public void skip(long n) { public CharStream skip(final long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - return newStream(new CharIteratorEx() { private boolean skipped = false; @@ -1264,6 +1392,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); diff --git a/src/com/landawn/abacus/util/stream/IteratorDoubleStream.java b/src/com/landawn/abacus/util/stream/IteratorDoubleStream.java index e2c9cf80..39a722c3 100644 --- a/src/com/landawn/abacus/util/stream/IteratorDoubleStream.java +++ b/src/com/landawn/abacus/util/stream/IteratorDoubleStream.java @@ -231,15 +231,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements.nextDouble()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -256,15 +258,17 @@ public int nextInt() { return mapper.applyAsInt(elements.nextDouble()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -281,15 +285,17 @@ public long nextLong() { return mapper.applyAsLong(elements.nextDouble()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -306,15 +312,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements.nextDouble()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -331,15 +339,17 @@ public U next() { return mapper.apply(elements.nextDouble()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false, null); } @@ -711,7 +721,9 @@ public long count() { @Override public void skip(long n) { - elements.skip(n >= Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); } }, false, null); } @@ -760,23 +772,90 @@ public DoubleList next() { }, false, null); } + @Override + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public DoubleStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + DoubleStream result = null; + + if (cursor == 0) { + final DoubleList list = new DoubleList(); + int cnt = 0; + + while (cnt++ < where && elements.hasNext()) { + list.add(elements.nextDouble()); + } + + result = new ArrayDoubleStream(list.array(), 0, list.size(), sorted, null); + } else { + result = new IteratorDoubleStream(elements, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + elements.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + elements.skip(where); + } else { + elements.skip(Long.MAX_VALUE); + } + } else { + elements.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream slidingToList(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); return newStream(new ObjIteratorEx() { private DoubleList prev = null; + private boolean toSkip = false; @Override public boolean hasNext() { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.nextDouble(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -802,10 +881,12 @@ public DoubleList next() { } } else { final double[] dest = new double[windowSize]; - N.copy(prev.trimToSize().array(), windowSize - cnt, dest, 0, cnt); + N.copy(prev.array(), windowSize - cnt, dest, 0, cnt); result = DoubleList.of(dest, cnt); } - } else { + } + + if (result == null) { result = new DoubleList(windowSize); } @@ -813,8 +894,62 @@ public DoubleList next() { result.add(elements.nextDouble()); } + toSkip = increment > windowSize; + return prev = result; } + + @Override + public long count() { + final int prevSize = increment >= windowSize ? 0 : (prev == null ? 0 : prev.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) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } + + if (increment >= windowSize) { + elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment); + } else { + final DoubleList tmp = new DoubleList(windowSize); + + if (N.isNullOrEmpty(prev)) { + 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 : (prev == null ? 0 : prev.size()); + + if (m < prevSize) { + tmp.addAll(prev.copy((int) m, prevSize)); + } else { + elements.skip(m - prevSize); + } + } + + int cnt = tmp.size(); + + while (cnt++ < windowSize && elements.hasNext()) { + tmp.add(elements.nextDouble()); + } + + prev = tmp; + } + } }, false, null); } @@ -866,6 +1001,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -973,6 +1110,8 @@ public double nextDouble() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -982,10 +1121,6 @@ public void skip(long n) { public DoubleStream skip(final long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - return newStream(new DoubleIteratorEx() { private boolean skipped = false; diff --git a/src/com/landawn/abacus/util/stream/IteratorFloatStream.java b/src/com/landawn/abacus/util/stream/IteratorFloatStream.java index 7ad3f529..5c23e87e 100644 --- a/src/com/landawn/abacus/util/stream/IteratorFloatStream.java +++ b/src/com/landawn/abacus/util/stream/IteratorFloatStream.java @@ -227,15 +227,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements.nextFloat()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -252,15 +254,17 @@ public int nextInt() { return mapper.applyAsInt(elements.nextFloat()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -277,15 +281,17 @@ public long nextLong() { return mapper.applyAsLong(elements.nextFloat()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -302,15 +308,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements.nextFloat()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -327,15 +335,17 @@ public U next() { return mapper.apply(elements.nextFloat()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false, null); } @@ -707,7 +717,9 @@ public long count() { @Override public void skip(long n) { - elements.skip(n >= Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); } }, false, null); } @@ -756,23 +768,90 @@ public FloatList next() { }, false, null); } + @Override + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public FloatStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + FloatStream result = null; + + if (cursor == 0) { + final FloatList list = new FloatList(); + int cnt = 0; + + while (cnt++ < where && elements.hasNext()) { + list.add(elements.nextFloat()); + } + + result = new ArrayFloatStream(list.array(), 0, list.size(), sorted, null); + } else { + result = new IteratorFloatStream(elements, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + elements.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + elements.skip(where); + } else { + elements.skip(Long.MAX_VALUE); + } + } else { + elements.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream slidingToList(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); return newStream(new ObjIteratorEx() { private FloatList prev = null; + private boolean toSkip = false; @Override public boolean hasNext() { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.nextFloat(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -798,10 +877,12 @@ public FloatList next() { } } else { final float[] dest = new float[windowSize]; - N.copy(prev.trimToSize().array(), windowSize - cnt, dest, 0, cnt); + N.copy(prev.array(), windowSize - cnt, dest, 0, cnt); result = FloatList.of(dest, cnt); } - } else { + } + + if (result == null) { result = new FloatList(windowSize); } @@ -809,8 +890,62 @@ public FloatList next() { result.add(elements.nextFloat()); } + toSkip = increment > windowSize; + return prev = result; } + + @Override + public long count() { + final int prevSize = increment >= windowSize ? 0 : (prev == null ? 0 : prev.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) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } + + if (increment >= windowSize) { + elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment); + } else { + final FloatList tmp = new FloatList(windowSize); + + if (N.isNullOrEmpty(prev)) { + 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 : (prev == null ? 0 : prev.size()); + + if (m < prevSize) { + tmp.addAll(prev.copy((int) m, prevSize)); + } else { + elements.skip(m - prevSize); + } + } + + int cnt = tmp.size(); + + while (cnt++ < windowSize && elements.hasNext()) { + tmp.add(elements.nextFloat()); + } + + prev = tmp; + } + } }, false, null); } @@ -862,6 +997,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -969,6 +1106,8 @@ public float nextFloat() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -978,10 +1117,6 @@ public void skip(long n) { public FloatStream skip(final long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - return newStream(new FloatIteratorEx() { private boolean skipped = false; @@ -1528,6 +1663,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); diff --git a/src/com/landawn/abacus/util/stream/IteratorIntStream.java b/src/com/landawn/abacus/util/stream/IteratorIntStream.java index e5813945..0ad41354 100644 --- a/src/com/landawn/abacus/util/stream/IteratorIntStream.java +++ b/src/com/landawn/abacus/util/stream/IteratorIntStream.java @@ -238,15 +238,17 @@ public int nextInt() { return mapper.applyAsInt(elements.nextInt()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -263,15 +265,17 @@ public char nextChar() { return mapper.applyAsChar(elements.nextInt()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -288,15 +292,17 @@ public byte nextByte() { return mapper.applyAsByte(elements.nextInt()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -313,15 +319,17 @@ public short nextShort() { return mapper.applyAsShort(elements.nextInt()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -338,15 +346,17 @@ public long nextLong() { return mapper.applyAsLong(elements.nextInt()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -363,15 +373,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements.nextInt()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -388,15 +400,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements.nextInt()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -413,15 +427,17 @@ public U next() { return mapper.apply(elements.nextInt()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false, null); } @@ -995,7 +1011,9 @@ public long count() { @Override public void skip(long n) { - elements.skip(n >= Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); } }, false, null); } @@ -1044,23 +1062,90 @@ public IntList next() { }, false, null); } + @Override + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public IntStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + IntStream result = null; + + if (cursor == 0) { + final IntList list = new IntList(); + int cnt = 0; + + while (cnt++ < where && elements.hasNext()) { + list.add(elements.nextInt()); + } + + result = new ArrayIntStream(list.array(), 0, list.size(), sorted, null); + } else { + result = new IteratorIntStream(elements, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + elements.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + elements.skip(where); + } else { + elements.skip(Long.MAX_VALUE); + } + } else { + elements.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream slidingToList(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); return newStream(new ObjIteratorEx() { private IntList prev = null; + private boolean toSkip = false; @Override public boolean hasNext() { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.nextInt(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -1086,10 +1171,12 @@ public IntList next() { } } else { final int[] dest = new int[windowSize]; - N.copy(prev.trimToSize().array(), windowSize - cnt, dest, 0, cnt); + N.copy(prev.array(), windowSize - cnt, dest, 0, cnt); result = IntList.of(dest, cnt); } - } else { + } + + if (result == null) { result = new IntList(windowSize); } @@ -1097,8 +1184,62 @@ public IntList next() { result.add(elements.nextInt()); } + toSkip = increment > windowSize; + return prev = result; } + + @Override + public long count() { + final int prevSize = increment >= windowSize ? 0 : (prev == null ? 0 : prev.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) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } + + if (increment >= windowSize) { + elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment); + } else { + final IntList tmp = new IntList(windowSize); + + if (N.isNullOrEmpty(prev)) { + 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 : (prev == null ? 0 : prev.size()); + + if (m < prevSize) { + tmp.addAll(prev.copy((int) m, prevSize)); + } else { + elements.skip(m - prevSize); + } + } + + int cnt = tmp.size(); + + while (cnt++ < windowSize && elements.hasNext()) { + tmp.add(elements.nextInt()); + } + + prev = tmp; + } + } }, false, null); } @@ -1150,6 +1291,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -1257,6 +1400,8 @@ public int nextInt() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -1266,10 +1411,6 @@ public void skip(long n) { public IntStream skip(final long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - return newStream(new IntIteratorEx() { private boolean skipped = false; @@ -1856,6 +1997,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -1881,6 +2024,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -1906,6 +2051,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); diff --git a/src/com/landawn/abacus/util/stream/IteratorLongStream.java b/src/com/landawn/abacus/util/stream/IteratorLongStream.java index fdbdc294..dedc19b5 100644 --- a/src/com/landawn/abacus/util/stream/IteratorLongStream.java +++ b/src/com/landawn/abacus/util/stream/IteratorLongStream.java @@ -232,15 +232,17 @@ public long nextLong() { return mapper.applyAsLong(elements.nextLong()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -257,15 +259,17 @@ public int nextInt() { return mapper.applyAsInt(elements.nextLong()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -282,15 +286,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements.nextLong()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -307,15 +313,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements.nextLong()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -332,15 +340,17 @@ public U next() { return mapper.apply(elements.nextLong()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false, null); } @@ -712,7 +722,9 @@ public long count() { @Override public void skip(long n) { - elements.skip(n >= Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); } }, false, null); } @@ -761,23 +773,90 @@ public LongList next() { }, false, null); } + @Override + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public LongStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + LongStream result = null; + + if (cursor == 0) { + final LongList list = new LongList(); + int cnt = 0; + + while (cnt++ < where && elements.hasNext()) { + list.add(elements.nextLong()); + } + + result = new ArrayLongStream(list.array(), 0, list.size(), sorted, null); + } else { + result = new IteratorLongStream(elements, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + elements.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + elements.skip(where); + } else { + elements.skip(Long.MAX_VALUE); + } + } else { + elements.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream slidingToList(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); return newStream(new ObjIteratorEx() { private LongList prev = null; + private boolean toSkip = false; @Override public boolean hasNext() { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.nextLong(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -803,10 +882,12 @@ public LongList next() { } } else { final long[] dest = new long[windowSize]; - N.copy(prev.trimToSize().array(), windowSize - cnt, dest, 0, cnt); + N.copy(prev.array(), windowSize - cnt, dest, 0, cnt); result = LongList.of(dest, cnt); } - } else { + } + + if (result == null) { result = new LongList(windowSize); } @@ -814,8 +895,62 @@ public LongList next() { result.add(elements.nextLong()); } + toSkip = increment > windowSize; + return prev = result; } + + @Override + public long count() { + final int prevSize = increment >= windowSize ? 0 : (prev == null ? 0 : prev.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) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } + + if (increment >= windowSize) { + elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment); + } else { + final LongList tmp = new LongList(windowSize); + + if (N.isNullOrEmpty(prev)) { + 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 : (prev == null ? 0 : prev.size()); + + if (m < prevSize) { + tmp.addAll(prev.copy((int) m, prevSize)); + } else { + elements.skip(m - prevSize); + } + } + + int cnt = tmp.size(); + + while (cnt++ < windowSize && elements.hasNext()) { + tmp.add(elements.nextLong()); + } + + prev = tmp; + } + } }, false, null); } @@ -867,6 +1002,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -974,6 +1111,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -983,10 +1122,6 @@ public void skip(long n) { public LongStream skip(final long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - return newStream(new LongIteratorEx() { private boolean skipped = false; @@ -1573,6 +1708,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -1598,6 +1735,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); diff --git a/src/com/landawn/abacus/util/stream/IteratorShortStream.java b/src/com/landawn/abacus/util/stream/IteratorShortStream.java index 995febc5..2da96b4c 100644 --- a/src/com/landawn/abacus/util/stream/IteratorShortStream.java +++ b/src/com/landawn/abacus/util/stream/IteratorShortStream.java @@ -224,15 +224,17 @@ public short nextShort() { return mapper.applyAsShort(elements.nextShort()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -249,15 +251,17 @@ public int nextInt() { return mapper.applyAsInt(elements.nextShort()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false); } @@ -274,15 +278,17 @@ public U next() { return mapper.apply(elements.nextShort()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } + // + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // elements.skip(n); + // } }, false, null); } @@ -520,7 +526,9 @@ public long count() { @Override public void skip(long n) { - elements.skip(n >= Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); } }, false, null); } @@ -569,23 +577,90 @@ public ShortList next() { }, false, null); } + @Override + public Stream splitAt(final int where) { + N.checkArgNotNegative(where, "where"); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public ShortStream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + ShortStream result = null; + + if (cursor == 0) { + final ShortList list = new ShortList(); + int cnt = 0; + + while (cnt++ < where && elements.hasNext()) { + list.add(elements.nextShort()); + } + + result = new ArrayShortStream(list.array(), 0, list.size(), sorted, null); + } else { + result = new IteratorShortStream(elements, sorted, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + elements.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + elements.skip(where); + } else { + elements.skip(Long.MAX_VALUE); + } + } else { + elements.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream slidingToList(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); return newStream(new ObjIteratorEx() { private ShortList prev = null; + private boolean toSkip = false; @Override public boolean hasNext() { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.nextShort(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -611,10 +686,12 @@ public ShortList next() { } } else { final short[] dest = new short[windowSize]; - N.copy(prev.trimToSize().array(), windowSize - cnt, dest, 0, cnt); + N.copy(prev.array(), windowSize - cnt, dest, 0, cnt); result = ShortList.of(dest, cnt); } - } else { + } + + if (result == null) { result = new ShortList(windowSize); } @@ -622,9 +699,62 @@ public ShortList next() { result.add(elements.nextShort()); } + toSkip = increment > windowSize; + return prev = result; } + @Override + public long count() { + final int prevSize = increment >= windowSize ? 0 : (prev == null ? 0 : prev.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) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } + + if (increment >= windowSize) { + elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment); + } else { + final ShortList tmp = new ShortList(windowSize); + + if (N.isNullOrEmpty(prev)) { + 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 : (prev == null ? 0 : prev.size()); + + if (m < prevSize) { + tmp.addAll(prev.copy((int) m, prevSize)); + } else { + elements.skip(m - prevSize); + } + } + + int cnt = tmp.size(); + + while (cnt++ < windowSize && elements.hasNext()) { + tmp.add(elements.nextShort()); + } + + prev = tmp; + } + } }, false, null); } @@ -676,6 +806,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -784,6 +916,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); @@ -793,10 +927,6 @@ public void skip(long n) { public ShortStream skip(final long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - return newStream(new ShortIteratorEx() { private boolean skipped = false; @@ -1383,6 +1513,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted); diff --git a/src/com/landawn/abacus/util/stream/IteratorStream.java b/src/com/landawn/abacus/util/stream/IteratorStream.java index 06f456b6..faea18d4 100644 --- a/src/com/landawn/abacus/util/stream/IteratorStream.java +++ b/src/com/landawn/abacus/util/stream/IteratorStream.java @@ -39,7 +39,6 @@ import com.landawn.abacus.util.DoubleIterator; import com.landawn.abacus.util.FloatIterator; import com.landawn.abacus.util.IntIterator; -import com.landawn.abacus.util.Iterators; import com.landawn.abacus.util.LongIterator; import com.landawn.abacus.util.LongMultiset; import com.landawn.abacus.util.Multimap; @@ -244,15 +243,17 @@ public R next() { return mapper.apply(elements.next()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false, null); } @@ -291,7 +292,9 @@ public R next() { // // // @Override // // public void skip(long n) { - // // elements.skip(n >= Long.MAX_VALUE / 2 ? Long.MAX_VALUE : n * 2); + // // N.checkArgNotNegative(n, "n"); + // // + // // elements.skip(n > Long.MAX_VALUE / 2 ? Long.MAX_VALUE : n * 2); // // } // // // // @Override @@ -345,7 +348,9 @@ public R next() { // // // @Override // // public void skip(long n) { - // // elements.skip(n >= Long.MAX_VALUE / 3 ? Long.MAX_VALUE : n * 3); + // // N.checkArgNotNegative(n, "n"); + // // + // // elements.skip(n > Long.MAX_VALUE / 3 ? Long.MAX_VALUE : n * 3); // // } // // // // @Override @@ -500,6 +505,8 @@ public T next() { // @Override // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // // if (n > 0) { // isFirst = false; // } @@ -516,6 +523,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (n > 0) { if (hasNext()) { next(); @@ -569,6 +578,8 @@ public R next() { // // @Override // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // // if (n > 0) { // isFirst = false; // } @@ -599,15 +610,17 @@ public T next() { } } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false, null); } @@ -633,15 +646,17 @@ public R next() { } } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false, null); } @@ -658,15 +673,17 @@ public char nextChar() { return mapper.applyAsChar(elements.next()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -683,15 +700,17 @@ public byte nextByte() { return mapper.applyAsByte(elements.next()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -708,15 +727,17 @@ public short nextShort() { return mapper.applyAsShort(elements.next()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -733,15 +754,17 @@ public int nextInt() { return mapper.applyAsInt(elements.next()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -758,15 +781,17 @@ public long nextLong() { return mapper.applyAsLong(elements.next()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -783,15 +808,17 @@ public float nextFloat() { return mapper.applyAsFloat(elements.next()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -808,15 +835,17 @@ public double nextDouble() { return mapper.applyAsDouble(elements.next()); } - // @Override - // public long count() { - // return elements.count(); - // } + // @Override + // public long count() { + // return elements.count(); + // } // - // @Override - // public void skip(long n) { - // elements.skip(n); - // } + // @Override + // public void skip(long n) { + // N.checkArgNotNegative(n, "n"); + // + // elements.skip(n); + // } }, false); } @@ -1391,7 +1420,56 @@ public long count() { @Override public void skip(long n) { - elements.skip(n >= Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); + } + }, false, null); + } + + @Override + public Stream split(final int size, final Collector collector) { + N.checkArgPositive(size, "size"); + N.checkArgNotNull(collector); + + final Supplier supplier = collector.supplier(); + final BiConsumer accumulator = collector.accumulator(); + final Function finisher = collector.finisher(); + + return newStream(new ObjIteratorEx() { + @Override + public boolean hasNext() { + return elements.hasNext(); + } + + @Override + public R next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + final A container = supplier.get(); + int cnt = 0; + + while (cnt < size && elements.hasNext()) { + accumulator.accept(container, elements.next()); + cnt++; + } + + return finisher.apply(container); + } + + @Override + public long count() { + final long len = elements.count(); + return len % size == 0 ? len / size : len / size + 1; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + elements.skip(n > Long.MAX_VALUE / size ? Long.MAX_VALUE : n * size); } }, false, null); } @@ -1414,16 +1492,18 @@ public C next() { } final C result = collectionSupplier.get(); + boolean isFirst = true; if (next == NONE) { next = elements.next(); } while (next != NONE) { - if (result.size() == 0) { + if (isFirst) { result.add(next); preCondition = predicate.test(next); next = elements.hasNext() ? elements.next() : (T) NONE; + isFirst = false; } else if (predicate.test(next) == preCondition) { result.add(next); next = elements.hasNext() ? elements.next() : (T) NONE; @@ -1439,23 +1519,212 @@ public C next() { }, false, null); } + @Override + public Stream split(final Predicate predicate, final Collector collector) { + N.checkArgNotNull(predicate); + N.checkArgNotNull(collector); + + final Supplier supplier = collector.supplier(); + final BiConsumer accumulator = collector.accumulator(); + final Function finisher = collector.finisher(); + + return newStream(new ObjIteratorEx() { + private T next = (T) NONE; + private boolean preCondition = false; + + @Override + public boolean hasNext() { + return next != NONE || elements.hasNext(); + } + + @Override + public R next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + final A container = supplier.get(); + boolean isFirst = true; + + if (next == NONE) { + next = elements.next(); + } + + while (next != NONE) { + if (isFirst) { + accumulator.accept(container, next); + preCondition = predicate.test(next); + next = elements.hasNext() ? elements.next() : (T) NONE; + isFirst = false; + } else if (predicate.test(next) == preCondition) { + accumulator.accept(container, next); + next = elements.hasNext() ? elements.next() : (T) NONE; + } else { + + break; + } + } + + return finisher.apply(container); + } + + }, false, null); + } + + @Override + public Stream> splitAt(final int where) { + N.checkArgNotNegative(where, "where"); + + return newStream(new ObjIteratorEx>() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public Stream next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + Stream result = null; + + if (cursor == 0) { + final List list = new ArrayList<>(); + int cnt = 0; + + while (cnt++ < where && elements.hasNext()) { + list.add(elements.next()); + } + + result = new ArrayStream(Stream.toArray(list), 0, list.size(), sorted, cmp, null); + } else { + result = new IteratorStream(elements, sorted, cmp, null); + } + + cursor++; + + return result; + } + + @Override + public long count() { + elements.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + elements.skip(where); + } else { + elements.skip(Long.MAX_VALUE); + } + } else { + elements.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + + @Override + public Stream splitAt(final int where, final Collector collector) { + N.checkArgNotNegative(where, "where"); + N.checkArgNotNull(collector, "collector"); + + final Supplier supplier = collector.supplier(); + final BiConsumer accumulator = collector.accumulator(); + final Function finisher = collector.finisher(); + + return newStream(new ObjIteratorEx() { + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor < 2; + } + + @Override + public R next() { + if (hasNext()) { + throw new NoSuchElementException(); + } + + final A container = supplier.get(); + + if (cursor == 0) { + int cnt = 0; + + while (cnt++ < where && elements.hasNext()) { + accumulator.accept(container, elements.next()); + } + } else { + while (elements.hasNext()) { + accumulator.accept(container, elements.next()); + } + } + + cursor++; + + return finisher.apply(container); + } + + @Override + public long count() { + elements.count(); + + return 2 - cursor; + } + + @Override + public void skip(long n) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } else if (n == 1) { + if (cursor == 0) { + elements.skip(where); + } else { + elements.skip(Long.MAX_VALUE); + } + } else { + elements.skip(Long.MAX_VALUE); + } + + cursor = n >= 2 ? 2 : cursor + (int) n; + } + }, false, null); + } + @Override public Stream> slidingToList(final int windowSize, final int increment) { N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); return newStream(new ObjIteratorEx>() { private List prev = null; + private boolean toSkip = false; @Override public boolean hasNext() { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.next(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -1486,8 +1755,62 @@ public List next() { result.add(elements.next()); } + toSkip = increment > windowSize; + return prev = result; } + + @Override + public long count() { + final int prevSize = increment >= windowSize ? 0 : (prev == null ? 0 : prev.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) { + N.checkArgNotNegative(n, "n"); + + if (n == 0) { + return; + } + + if (increment >= windowSize) { + elements.skip(n > Long.MAX_VALUE / increment ? Long.MAX_VALUE : n * increment); + } else { + final List tmp = new ArrayList<>(windowSize); + + if (N.isNullOrEmpty(prev)) { + 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 : (prev == null ? 0 : prev.size()); + + if (m < prevSize) { + tmp.addAll(prev.subList((int) m, prevSize)); + } else { + elements.skip(m - prevSize); + } + } + + int cnt = tmp.size(); + + while (cnt++ < windowSize && elements.hasNext()) { + tmp.add(elements.next()); + } + + prev = tmp; + } + } }, false, null); } @@ -1496,18 +1819,19 @@ public > Stream sliding(final int windowSize, final i N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); return newStream(new ObjIteratorEx() { - private C prev = null; + private Deque queue = null; + private boolean toSkip = false; @Override public boolean hasNext() { - if (prev != null && increment > windowSize) { + if (toSkip) { int skipNum = increment - windowSize; while (skipNum-- > 0 && elements.hasNext()) { elements.next(); } - prev = null; + toSkip = false; } return elements.hasNext(); @@ -1519,28 +1843,234 @@ public C next() { throw new NoSuchElementException(); } + if (queue == null) { + queue = new ArrayDeque<>(N.max(0, windowSize - increment)); + } + final C result = collectionSupplier.apply(windowSize); int cnt = 0; - if (prev != null && increment < windowSize) { - if (prev instanceof ArrayList) { - result.addAll(((ArrayList) prev).subList(windowSize - cnt, prev.size())); + if (queue.size() > 0 && increment < windowSize) { + cnt = queue.size(); + + for (T e : queue) { + result.add(e); + } + + if (queue.size() <= increment) { + queue.clear(); } else { - cnt = windowSize - increment; + for (int i = 0; i < increment; i++) { + queue.removeFirst(); + } + } + } + + T next = null; - final Iterator iter = Iterators.skip(prev.iterator(), windowSize - cnt); + while (cnt++ < windowSize && elements.hasNext()) { + next = elements.next(); + result.add(next); - while (iter.hasNext()) { - result.add(iter.next()); + if (cnt > increment) { + queue.add(next); + } + } + + toSkip = increment > windowSize; + + return result; + } + + @Override + public long count() { + 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) { + N.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); + } + + @Override + public Stream sliding(final int windowSize, final int increment, final Collector collector) { + N.checkArgument(windowSize > 0 && increment > 0, "'windowSize'=%s and 'increment'=%s must not be less than 1", windowSize, increment); + N.checkArgNotNull(collector); + + final Supplier supplier = collector.supplier(); + final BiConsumer accumulator = collector.accumulator(); + final Function finisher = collector.finisher(); + + return newStream(new ObjIteratorEx() { + private Deque queue = null; + private boolean toSkip = false; + + @Override + public boolean hasNext() { + if (toSkip) { + int skipNum = increment - windowSize; + + while (skipNum-- > 0 && elements.hasNext()) { + elements.next(); + } + + toSkip = false; + } + + return elements.hasNext(); + } + + @Override + public R next() { + if (hasNext() == false) { + throw new NoSuchElementException(); + } + + if (queue == null) { + queue = new ArrayDeque<>(N.max(0, windowSize - increment)); + } + + final A container = supplier.get(); + int cnt = 0; + + if (queue.size() > 0 && increment < windowSize) { + 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()) { - result.add(elements.next()); + next = elements.next(); + accumulator.accept(container, next); + + if (cnt > increment) { + queue.add(next); + } } - return prev = result; + toSkip = increment > windowSize; + + return finisher.apply(container); + } + + @Override + public long count() { + 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) { + N.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); } @@ -1588,6 +2118,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (initialized == false) { init(); } @@ -1690,6 +2222,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + elements.skip(n); } }, sorted, cmp); @@ -1699,10 +2233,6 @@ public void skip(long n) { public Stream skip(final long n) { N.checkArgNotNegative(n, "n"); - if (n == 0) { - return this; - } - return newStream(new ObjIteratorEx() { private boolean skipped = false; @@ -2235,9 +2765,7 @@ private void init() { @Override public Stream skipLast(final int n) { - N.checkArgNotNegative(n, "n"); - - if (n == 0) { + if (n <= 0) { return this; } diff --git a/src/com/landawn/abacus/util/stream/LongIteratorEx.java b/src/com/landawn/abacus/util/stream/LongIteratorEx.java index c7e4ba99..8f31311a 100644 --- a/src/com/landawn/abacus/util/stream/LongIteratorEx.java +++ b/src/com/landawn/abacus/util/stream/LongIteratorEx.java @@ -95,6 +95,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -179,6 +181,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -257,6 +261,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -302,6 +308,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + iteratorEx.skip(n); } @@ -337,6 +345,8 @@ public void close() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + while (n > 0 && hasNext()) { nextLong(); n--; diff --git a/src/com/landawn/abacus/util/stream/LongStream.java b/src/com/landawn/abacus/util/stream/LongStream.java index 53301c9a..d0e8d7fe 100644 --- a/src/com/landawn/abacus/util/stream/LongStream.java +++ b/src/com/landawn/abacus/util/stream/LongStream.java @@ -723,6 +723,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - n; next += n; } @@ -790,6 +792,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - n; next += n * by; } @@ -845,6 +849,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - n; next += n; } @@ -914,6 +920,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - n; next += n * by; } @@ -964,6 +972,8 @@ public long nextLong() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; } diff --git a/src/com/landawn/abacus/util/stream/ObjIteratorEx.java b/src/com/landawn/abacus/util/stream/ObjIteratorEx.java index 43dfe98e..29e4d12f 100644 --- a/src/com/landawn/abacus/util/stream/ObjIteratorEx.java +++ b/src/com/landawn/abacus/util/stream/ObjIteratorEx.java @@ -92,6 +92,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -211,6 +213,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -289,6 +293,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -316,6 +322,8 @@ private void init() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + while (n > 0 && hasNext()) { next(); n--; diff --git a/src/com/landawn/abacus/util/stream/ShortIteratorEx.java b/src/com/landawn/abacus/util/stream/ShortIteratorEx.java index b06d9eb9..91170a44 100644 --- a/src/com/landawn/abacus/util/stream/ShortIteratorEx.java +++ b/src/com/landawn/abacus/util/stream/ShortIteratorEx.java @@ -95,6 +95,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -179,6 +181,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -257,6 +261,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + if (isInitialized == false) { init(); } @@ -302,6 +308,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + iteratorEx.skip(n); } @@ -337,6 +345,8 @@ public void close() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + while (n > 0 && hasNext()) { nextShort(); n--; diff --git a/src/com/landawn/abacus/util/stream/ShortStream.java b/src/com/landawn/abacus/util/stream/ShortStream.java index b7ff13b3..afeaf230 100644 --- a/src/com/landawn/abacus/util/stream/ShortStream.java +++ b/src/com/landawn/abacus/util/stream/ShortStream.java @@ -642,6 +642,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n; } @@ -697,6 +699,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n * by; } @@ -748,6 +752,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n; } @@ -805,6 +811,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; next += n * by; } @@ -855,6 +863,8 @@ public short nextShort() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; } diff --git a/src/com/landawn/abacus/util/stream/Stream.java b/src/com/landawn/abacus/util/stream/Stream.java index cd75bf2e..6a87ac4b 100644 --- a/src/com/landawn/abacus/util/stream/Stream.java +++ b/src/com/landawn/abacus/util/stream/Stream.java @@ -310,24 +310,45 @@ public Stream select(Class targetType) { @ParallelSupported public abstract CharStream flatMapToChar(Function mapper); + @ParallelSupported + public abstract CharStream flattMapToChar(Function mapper); + @ParallelSupported public abstract ByteStream flatMapToByte(Function mapper); + @ParallelSupported + public abstract ByteStream flattMapToByte(Function mapper); + @ParallelSupported public abstract ShortStream flatMapToShort(Function mapper); + @ParallelSupported + public abstract ShortStream flattMapToShort(Function mapper); + @ParallelSupported public abstract IntStream flatMapToInt(Function mapper); + @ParallelSupported + public abstract IntStream flattMapToInt(Function mapper); + @ParallelSupported public abstract LongStream flatMapToLong(Function mapper); + @ParallelSupported + public abstract LongStream flattMapToLong(Function mapper); + @ParallelSupported public abstract FloatStream flatMapToFloat(Function mapper); + @ParallelSupported + public abstract FloatStream flattMapToFloat(Function mapper); + @ParallelSupported public abstract DoubleStream flatMapToDouble(Function mapper); + @ParallelSupported + public abstract DoubleStream flattMapToDouble(Function mapper); + @ParallelSupported public abstract EntryStream flatMapToEntry(Function>> mapper); @@ -624,9 +645,6 @@ public abstract Stream collapse(final BiPredicate c @SequentialOnly public abstract Stream> splitToSet(int size); - @SequentialOnly - public abstract Stream> splitToSet(Predicate predicate); - /** * Returns Stream of Stream with consecutive sub sequences of the elements, each of the same size (the final sequence may be smaller). * @@ -640,15 +658,36 @@ public abstract Stream collapse(final BiPredicate c @SequentialOnly public abstract > Stream split(int size, IntFunction collectionSupplier); + @SequentialOnly + public abstract Stream split(int size, Collector collector); + + @SequentialOnly + public abstract Stream> splitToSet(Predicate predicate); + @SequentialOnly public abstract > Stream split(Predicate predicate, Supplier collectionSupplier); + @SequentialOnly + public abstract Stream split(Predicate predicate, Collector collector); + + @SequentialOnly + public abstract Stream splitAt(int where, Collector collector); + + @SequentialOnly + public abstract Stream splitBy(Predicate where, Collector collector); + @SequentialOnly public abstract > Stream sliding(int windowSize, IntFunction collectionSupplier); @SequentialOnly public abstract > Stream sliding(int windowSize, int increment, IntFunction collectionSupplier); + @SequentialOnly + public abstract Stream sliding(int windowSize, Collector collector); + + @SequentialOnly + public abstract Stream sliding(int windowSize, int increment, Collector collector); + /** * Stream.of(1).intersperse(9) --> [1] * Stream.of(1, 2, 3).intersperse(9) --> [1, 9, 2, 9, 3] @@ -1049,11 +1088,11 @@ public abstract , M extends Multimap> M t public abstract DataSet toDataSet(); /** - * @param isFirstHeader + * @param isFirstTitle * @return */ @SequentialOnly - public abstract DataSet toDataSet(boolean isFirstHeader); + public abstract DataSet toDataSet(boolean isFirstTitle); /** * @@ -1682,7 +1721,7 @@ public static Stream of(final Collection c, int startIndex, try { array = (T[]) listElementDataField.get(c); - } catch (Exception e) { + } catch (Throwable e) { // ignore; isListElementDataFieldGettable = false; } @@ -1850,6 +1889,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1905,6 +1946,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -1960,6 +2003,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -2015,6 +2060,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -2070,6 +2117,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -2125,6 +2174,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -2180,6 +2231,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -2235,6 +2288,8 @@ public long count() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cursor = n < toIndex - cursor ? cursor + (int) n : toIndex; } @@ -2510,6 +2565,8 @@ public T next() { @Override public void skip(long n) { + N.checkArgNotNegative(n, "n"); + cnt = n >= cnt ? 0 : cnt - (int) n; } diff --git a/src/com/landawn/abacus/util/stream/StreamBase.java b/src/com/landawn/abacus/util/stream/StreamBase.java index 9614cf22..9f5bf67e 100644 --- a/src/com/landawn/abacus/util/stream/StreamBase.java +++ b/src/com/landawn/abacus/util/stream/StreamBase.java @@ -550,6 +550,8 @@ public void accept(Object t, Object u) { } }; + static volatile boolean isListElementDataFieldGettable = true; + static volatile boolean isListElementDataFieldSettable = true; static final Field listElementDataField; static final Field listSizeField; @@ -558,7 +560,7 @@ public void accept(Object t, Object u) { try { tmp = ArrayList.class.getDeclaredField("elementData"); - } catch (Exception e) { + } catch (Throwable e) { // ignore. } @@ -572,7 +574,7 @@ public void accept(Object t, Object u) { try { tmp = ArrayList.class.getDeclaredField("size"); - } catch (Exception e) { + } catch (Throwable e) { // ignore. } @@ -583,8 +585,6 @@ public void accept(Object t, Object u) { } } - static volatile boolean isListElementDataFieldGettable = true; - final Deque closeHandlers; final boolean sorted; final Comparator cmp;; @@ -1231,7 +1231,7 @@ static T[] toArray(Collection c) { if (isListElementDataFieldGettable && listElementDataField != null && c instanceof ArrayList) { try { return (T[]) listElementDataField.get(c); - } catch (Exception e) { + } catch (Throwable e) { // ignore; isListElementDataFieldGettable = false; } @@ -1240,6 +1240,29 @@ static T[] toArray(Collection c) { return (T[]) c.toArray(); } + @SafeVarargs + static List createList(final T... a) { + if (N.isNullOrEmpty(a)) { + return new ArrayList<>(); + } + + if (isListElementDataFieldSettable && listElementDataField != null && listSizeField != null) { + final List list = new ArrayList<>(); + + try { + listElementDataField.set(list, a); + listSizeField.set(list, a.length); + + return list; + } catch (Throwable e) { + // ignore; + isListElementDataFieldSettable = false; + } + } + + return N.asList(a); + } + static int sum(final char[] a) { if (a == null || a.length == 0) { return 0;