Java 类java.util.Spliterators 实例源码

项目:openjdk-jdk10    文件:StreamAndSpliterator.java   
@Test
public void testSpliteratorAIOBEsFromSpliterators() {
    // origin > fence
    assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 1, 0, 0));

    // bad origin
    assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, -1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, -1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, -1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, -1, 0, 0));

    // bad fence
    assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 0, 1, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 0, 1, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 0, 1, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 0, 1, 0));
}
项目:jdk8u-jdk    文件:StreamAndSpliterator.java   
@Test
public void testSpliteratorAIOBEsFromSpliterators() {
    // origin > fence
    assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 1, 0, 0));

    // bad origin
    assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, -1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, -1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, -1, 0, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, -1, 0, 0));

    // bad fence
    assertThrowsAIOOB(() -> Spliterators.spliterator(new int[]{}, 0, 1, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new long[]{}, 0, 1, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new double[]{}, 0, 1, 0));
    assertThrowsAIOOB(() -> Spliterators.spliterator(new String[]{}, 0, 1, 0));
}
项目:openjdk-jdk10    文件:ConcurrentLinkedDeque.java   
public Spliterator<E> trySplit() {
    Node<E> p, q;
    if ((p = current()) == null || (q = p.next) == null)
        return null;
    int i = 0, n = batch = Math.min(batch + 1, MAX_BATCH);
    Object[] a = null;
    do {
        final E e;
        if ((e = p.item) != null) {
            if (a == null)
                a = new Object[n];
            a[i++] = e;
        }
        if (p == (p = q))
            p = first();
    } while (p != null && (q = p.next) != null && i < n);
    setCurrent(p);
    return (i == 0) ? null :
        Spliterators.spliterator(a, 0, i, (Spliterator.ORDERED |
                                           Spliterator.NONNULL |
                                           Spliterator.CONCURRENT));
}
项目:OpenJSharp    文件:Stream.java   
/**
 * Returns an infinite sequential ordered {@code Stream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code Stream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param <T> the type of stream elements
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code Stream}
 */
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
    Objects.requireNonNull(f);
    final Iterator<T> iterator = new Iterator<T>() {
        @SuppressWarnings("unchecked")
        T t = (T) Streams.NONE;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public T next() {
            return t = (t == Streams.NONE) ? seed : f.apply(t);
        }
    };
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE), false);
}
项目:OpenJSharp    文件:LongStream.java   
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
        long t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public long nextLong() {
            long v = t;
            t = f.applyAsLong(t);
            return v;
        }
    };
    return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
项目:OpenJSharp    文件:DoubleStream.java   
/**
 * Returns an infinite sequential ordered {@code DoubleStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code DoubleStream}
 * will be the provided {@code seed}.  For {@code n > 0}, the element at
 * position {@code n}, will be the result of applying the function {@code f}
 *  to the element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code DoubleStream}
 */
public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfDouble iterator = new PrimitiveIterator.OfDouble() {
        double t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public double nextDouble() {
            double v = t;
            t = f.applyAsDouble(t);
            return v;
        }
    };
    return StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
项目:openjdk-jdk10    文件:LongStream.java   
/**
 * Returns an infinite sequential ordered {@code LongStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code LongStream} will
 * be the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * <p>The action of applying {@code f} for one element
 * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
 * the action of applying {@code f} for subsequent elements.  For any given
 * element the action may be performed in whatever thread the library
 * chooses.
 *
 * @param seed the initial element
 * @param f a function to be applied to the previous element to produce
 *          a new element
 * @return a new sequential {@code LongStream}
 */
public static LongStream iterate(final long seed, final LongUnaryOperator f) {
    Objects.requireNonNull(f);
    Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
           Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
        long prev;
        boolean started;

        @Override
        public boolean tryAdvance(LongConsumer action) {
            Objects.requireNonNull(action);
            long t;
            if (started)
                t = f.applyAsLong(prev);
            else {
                t = seed;
                started = true;
            }
            action.accept(prev = t);
            return true;
        }
    };
    return StreamSupport.longStream(spliterator, false);
}
项目:iostream    文件:StreamInputAdapter.java   
/**
 * Adapted from StackOverflow {@linkplain so http://stackoverflow.com/questions/20746429/limit-a-stream-by-a-predicate}
 *
 * @param splitr the original Spliterator
 * @param predicate the predicate
 * @return a Spliterator.OfInt
 */
private static Spliterator.OfInt takeIntWhile(Spliterator.OfInt splitr, IntPredicate predicate) {
    return new Spliterators.AbstractIntSpliterator(splitr.estimateSize(), 0) {
        boolean stillGoing = true;

        @Override
        public boolean tryAdvance(IntConsumer consumer) {
            if (stillGoing) {
                boolean hadNext = splitr.tryAdvance((int elem) -> {
                    if (predicate.test(elem)) {
                        consumer.accept(elem);
                    } else {
                        stillGoing = false;
                    }
                });
                return hadNext && stillGoing;
            }
            return false;
        }
    };
}
项目:jdk8u-jdk    文件:Stream.java   
/**
 * Returns an infinite sequential ordered {@code Stream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code Stream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param <T> the type of stream elements
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return a new sequential {@code Stream}
 */
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
    Objects.requireNonNull(f);
    final Iterator<T> iterator = new Iterator<T>() {
        @SuppressWarnings("unchecked")
        T t = (T) Streams.NONE;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public T next() {
            return t = (t == Streams.NONE) ? seed : f.apply(t);
        }
    };
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE), false);
}
项目:jdk8u-jdk    文件:IntStream.java   
/**
 * Returns an infinite sequential ordered {@code IntStream} produced by iterative
 * application of a function {@code f} to an initial element {@code seed},
 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
 * {@code f(f(seed))}, etc.
 *
 * <p>The first element (position {@code 0}) in the {@code IntStream} will be
 * the provided {@code seed}.  For {@code n > 0}, the element at position
 * {@code n}, will be the result of applying the function {@code f} to the
 * element at position {@code n - 1}.
 *
 * @param seed the initial element
 * @param f a function to be applied to to the previous element to produce
 *          a new element
 * @return A new sequential {@code IntStream}
 */
public static IntStream iterate(final int seed, final IntUnaryOperator f) {
    Objects.requireNonNull(f);
    final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
        int t = seed;

        @Override
        public boolean hasNext() {
            return true;
        }

        @Override
        public int nextInt() {
            int v = t;
            t = f.applyAsInt(t);
            return v;
        }
    };
    return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
            iterator,
            Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
项目:jdk8u-jdk    文件:BufferedReader.java   
/**
 * Returns a {@code Stream}, the elements of which are lines read from
 * this {@code BufferedReader}.  The {@link Stream} is lazily populated,
 * i.e., read only occurs during the
 * <a href="../util/stream/package-summary.html#StreamOps">terminal
 * stream operation</a>.
 *
 * <p> The reader must not be operated on during the execution of the
 * terminal stream operation. Otherwise, the result of the terminal stream
 * operation is undefined.
 *
 * <p> After execution of the terminal stream operation there are no
 * guarantees that the reader will be at a specific position from which to
 * read the next character or line.
 *
 * <p> If an {@link IOException} is thrown when accessing the underlying
 * {@code BufferedReader}, it is wrapped in an {@link
 * UncheckedIOException} which will be thrown from the {@code Stream}
 * method that caused the read to take place. This method will return a
 * Stream if invoked on a BufferedReader that is closed. Any operation on
 * that stream that requires reading from the BufferedReader after it is
 * closed, will cause an UncheckedIOException to be thrown.
 *
 * @return a {@code Stream<String>} providing the lines of text
 *         described by this {@code BufferedReader}
 *
 * @since 1.8
 */
public Stream<String> lines() {
    Iterator<String> iter = new Iterator<String>() {
        String nextLine = null;

        @Override
        public boolean hasNext() {
            if (nextLine != null) {
                return true;
            } else {
                try {
                    nextLine = readLine();
                    return (nextLine != null);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        }

        @Override
        public String next() {
            if (nextLine != null || hasNext()) {
                String line = nextLine;
                nextLine = null;
                return line;
            } else {
                throw new NoSuchElementException();
            }
        }
    };
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
            iter, Spliterator.ORDERED | Spliterator.NONNULL), false);
}
项目:aem-epic-tool    文件:DataUtil.java   
public static <T> Stream<T> enumerationAsStream(Enumeration<T> e) {
    return StreamSupport.stream(
            Spliterators.spliteratorUnknownSize(
                    new Iterator<T>() {
                public T next() {
                    return e.nextElement();
                }

                public boolean hasNext() {
                    return e.hasMoreElements();
                }
            }, Spliterator.ORDERED), false);
}
项目:openjdk-jdk10    文件:StreamAndSpliterator.java   
@Test
public void testSpliteratorNPEsFromSpliterators() {
    assertThrowsNPE(() -> Spliterators.spliterator((int[]) null, 0, 0, 0));
    assertThrowsNPE(() -> Spliterators.spliterator((long[]) null, 0, 0, 0));
    assertThrowsNPE(() -> Spliterators.spliterator((double[]) null, 0, 0, 0));
    assertThrowsNPE(() -> Spliterators.spliterator((String[]) null, 0, 0, 0));
}
项目:guava-mock    文件:Streams.java   
/**
 * Returns a stream in which each element is the result of passing the corresponding elementY of
 * each of {@code streamA} and {@code streamB} to {@code function}.
 *
 * <p>For example:
 *
 * <pre>{@code
 * Streams.zip(
 *   Stream.of("foo1", "foo2", "foo3"),
 *   Stream.of("bar1", "bar2"),
 *   (arg1, arg2) -> arg1 + ":" + arg2)
 * }</pre>
 *
 * <p>will return {@code Stream.of("foo1:bar1", "foo2:bar2")}.
 *
 * <p>The resulting stream will only be as long as the shorter of the two input streams; if one
 * stream is longer, its extra elements will be ignored.
 *
 * <p>Note that if you are calling {@link Stream#forEach} on the resulting stream, you might want
 * to consider using {@link #forEachPair} instead of this method.
 *
 * <p><b>Performance note:</b> The resulting stream is not <a
 * href="http://gee.cs.oswego.edu/dl/html/StreamParallelGuidance.html">efficiently splittable</a>.
 * This may harm parallel performance.
 */
public static <A, B, R> Stream<R> zip(
    Stream<A> streamA, Stream<B> streamB, BiFunction<? super A, ? super B, R> function) {
  checkNotNull(streamA);
  checkNotNull(streamB);
  checkNotNull(function);
  boolean isParallel = streamA.isParallel() || streamB.isParallel(); // same as Stream.concat
  Spliterator<A> splitrA = streamA.spliterator();
  Spliterator<B> splitrB = streamB.spliterator();
  int characteristics =
      splitrA.characteristics()
          & splitrB.characteristics()
          & (Spliterator.SIZED | Spliterator.ORDERED);
  Iterator<A> itrA = Spliterators.iterator(splitrA);
  Iterator<B> itrB = Spliterators.iterator(splitrB);
  return StreamSupport.stream(
      new AbstractSpliterator<R>(
          Math.min(splitrA.estimateSize(), splitrB.estimateSize()), characteristics) {
        @Override
        public boolean tryAdvance(Consumer<? super R> action) {
          if (itrA.hasNext() && itrB.hasNext()) {
            action.accept(function.apply(itrA.next(), itrB.next()));
            return true;
          }
          return false;
        }
      },
      isParallel);
}
项目:guava-mock    文件:StandardTable.java   
@Override
Spliterator<Entry<C, V>> entrySpliterator() {
  Map<C, V> map = backingRowMap();
  if (map == null) {
    return Spliterators.emptySpliterator();
  }
  return CollectSpliterators.map(map.entrySet().spliterator(), this::wrapEntry);
}
项目:openjdk-jdk10    文件:BufferedReader.java   
/**
 * Returns a {@code Stream}, the elements of which are lines read from
 * this {@code BufferedReader}.  The {@link Stream} is lazily populated,
 * i.e., read only occurs during the
 * <a href="../util/stream/package-summary.html#StreamOps">terminal
 * stream operation</a>.
 *
 * <p> The reader must not be operated on during the execution of the
 * terminal stream operation. Otherwise, the result of the terminal stream
 * operation is undefined.
 *
 * <p> After execution of the terminal stream operation there are no
 * guarantees that the reader will be at a specific position from which to
 * read the next character or line.
 *
 * <p> If an {@link IOException} is thrown when accessing the underlying
 * {@code BufferedReader}, it is wrapped in an {@link
 * UncheckedIOException} which will be thrown from the {@code Stream}
 * method that caused the read to take place. This method will return a
 * Stream if invoked on a BufferedReader that is closed. Any operation on
 * that stream that requires reading from the BufferedReader after it is
 * closed, will cause an UncheckedIOException to be thrown.
 *
 * @return a {@code Stream<String>} providing the lines of text
 *         described by this {@code BufferedReader}
 *
 * @since 1.8
 */
public Stream<String> lines() {
    Iterator<String> iter = new Iterator<>() {
        String nextLine = null;

        @Override
        public boolean hasNext() {
            if (nextLine != null) {
                return true;
            } else {
                try {
                    nextLine = readLine();
                    return (nextLine != null);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        }

        @Override
        public String next() {
            if (nextLine != null || hasNext()) {
                String line = nextLine;
                nextLine = null;
                return line;
            } else {
                throw new NoSuchElementException();
            }
        }
    };
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
            iter, Spliterator.ORDERED | Spliterator.NONNULL), false);
}
项目:prngine    文件:TestData.java   
/**
 * Return a stream with the data lines.
 *
 * @return a stream with the data lines
 */
public Stream<String[]> stream() {
    final DataIterator iterator = new DataIterator(getResourcePath());
    final Spliterator<String[]> spliterator = Spliterators
        .spliteratorUnknownSize(iterator, 0);

    return StreamSupport
        .stream(spliterator, false)
        .onClose(iterator::close);
}
项目:jdk8u-jdk    文件:LinkedBlockingDeque.java   
public Spliterator<E> trySplit() {
    Node<E> h;
    final LinkedBlockingDeque<E> q = this.queue;
    int b = batch;
    int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
    if (!exhausted &&
        ((h = current) != null || (h = q.first) != null) &&
        h.next != null) {
        Object[] a = new Object[n];
        final ReentrantLock lock = q.lock;
        int i = 0;
        Node<E> p = current;
        lock.lock();
        try {
            if (p != null || (p = q.first) != null) {
                do {
                    if ((a[i] = p.item) != null)
                        ++i;
                } while ((p = p.next) != null && i < n);
            }
        } finally {
            lock.unlock();
        }
        if ((current = p) == null) {
            est = 0L;
            exhausted = true;
        }
        else if ((est -= i) < 0L)
            est = 0L;
        if (i > 0) {
            batch = i;
            return Spliterators.spliterator
                (a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
                 Spliterator.CONCURRENT);
        }
    }
    return null;
}
项目:aws-sdk-java-v2    文件:ShapeModelReflector.java   
/**
 * Get the value for the streaming member in the {@link JsonNode}.
 */
public String getStreamingMemberValue() {
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(input.fields(), Spliterator.ORDERED), false)
                        .filter(f -> model.getShapes().get(shapeName)
                                          .getMemberByC2jName(f.getKey())
                                          .getHttp().getIsStreaming())
                        .map(f -> f.getValue().asText())
                        .findFirst()
                        .orElseThrow(() -> new IllegalStateException("Streaming member not found in " + shapeName));
}
项目:openjdk-jdk10    文件:DistinctOpTest.java   
SortedTestData(List<T> coll) {
    super("SortedTestData", coll,
          c -> StreamSupport.stream(Spliterators.spliterator(c.toArray(), Spliterator.ORDERED | Spliterator.SORTED), false),
          c -> StreamSupport.stream(Spliterators.spliterator(c.toArray(), Spliterator.ORDERED | Spliterator.SORTED), true),
          c -> Spliterators.spliterator(c.toArray(), Spliterator.ORDERED | Spliterator.SORTED),
          List::size);
}
项目:openjdk-jdk10    文件:NetworkInterface.java   
private static <T> Stream<T> streamFromArray(T[] a) {
    return StreamSupport.stream(
            Spliterators.spliterator(
                    a,
                    Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL),
            false);
}
项目:OpenJSharp    文件:LinkedBlockingDeque.java   
public Spliterator<E> trySplit() {
    Node<E> h;
    final LinkedBlockingDeque<E> q = this.queue;
    int b = batch;
    int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
    if (!exhausted &&
        ((h = current) != null || (h = q.first) != null) &&
        h.next != null) {
        Object[] a = new Object[n];
        final ReentrantLock lock = q.lock;
        int i = 0;
        Node<E> p = current;
        lock.lock();
        try {
            if (p != null || (p = q.first) != null) {
                do {
                    if ((a[i] = p.item) != null)
                        ++i;
                } while ((p = p.next) != null && i < n);
            }
        } finally {
            lock.unlock();
        }
        if ((current = p) == null) {
            est = 0L;
            exhausted = true;
        }
        else if ((est -= i) < 0L)
            est = 0L;
        if (i > 0) {
            batch = i;
            return Spliterators.spliterator
                (a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
                 Spliterator.CONCURRENT);
        }
    }
    return null;
}
项目:OpenJSharp    文件:LinkedTransferQueue.java   
public Spliterator<E> trySplit() {
    Node p;
    final LinkedTransferQueue<E> q = this.queue;
    int b = batch;
    int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
    if (!exhausted &&
        ((p = current) != null || (p = q.firstDataNode()) != null) &&
        p.next != null) {
        Object[] a = new Object[n];
        int i = 0;
        do {
            if ((a[i] = p.item) != null)
                ++i;
            if (p == (p = p.next))
                p = q.firstDataNode();
        } while (p != null && i < n);
        if ((current = p) == null)
            exhausted = true;
        if (i > 0) {
            batch = i;
            return Spliterators.spliterator
                (a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
                 Spliterator.CONCURRENT);
        }
    }
    return null;
}
项目:athena    文件:StatisticsWebResource.java   
/**
 * Gets load statistics for all links or for a specific link.
 *
 * @onos.rsModel StatisticsFlowsLink
 * @param deviceId (optional) device ID for a specific link
 * @param port (optional) port number for a specified link
 * @return 200 OK with JSON encoded array of Load objects
 */
@GET
@Path("flows/link")
@Produces(MediaType.APPLICATION_JSON)
public Response getLoads(@QueryParam("device") String deviceId,
                         @QueryParam("port") String port) {
    Iterable<Link> links;

    if (deviceId == null || port == null) {
        links = get(LinkService.class).getLinks();
    } else {
        ConnectPoint connectPoint = new ConnectPoint(deviceId(deviceId),
                portNumber(port));
        links = get(LinkService.class).getLinks(connectPoint);
    }
    ObjectNode result = mapper().createObjectNode();
    ArrayNode loads = mapper().createArrayNode();
    JsonCodec<Load> loadCodec = codec(Load.class);
    StatisticService statsService = getService(StatisticService.class);

    StreamSupport.stream(Spliterators.spliteratorUnknownSize(
            links.iterator(), Spliterator.ORDERED), false)
            .forEach(link -> {
                ObjectNode loadNode = loadCodec.encode(statsService.load(link), this);

                UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
                        .path("links")
                        .queryParam("device", link.src().deviceId().toString())
                        .queryParam("port", link.src().port().toString());
                loadNode.put("link", locationBuilder.build().toString());
                loads.add(loadNode);
            });
    result.set("loads", loads);
    return ok(result).build();
}
项目:openjdk-jdk10    文件:LinkedBlockingQueue.java   
public Spliterator<E> trySplit() {
    Node<E> h;
    if (!exhausted &&
        ((h = current) != null || (h = head.next) != null)
        && h.next != null) {
        int n = batch = Math.min(batch + 1, MAX_BATCH);
        Object[] a = new Object[n];
        int i = 0;
        Node<E> p = current;
        fullyLock();
        try {
            if (p != null || (p = head.next) != null)
                for (; p != null && i < n; p = succ(p))
                    if ((a[i] = p.item) != null)
                        i++;
        } finally {
            fullyUnlock();
        }
        if ((current = p) == null) {
            est = 0L;
            exhausted = true;
        }
        else if ((est -= i) < 0L)
            est = 0L;
        if (i > 0)
            return Spliterators.spliterator
                (a, 0, i, (Spliterator.ORDERED |
                           Spliterator.NONNULL |
                           Spliterator.CONCURRENT));
    }
    return null;
}
项目:OpenJSharp    文件:CopyOnWriteArrayList.java   
public Spliterator<E> spliterator() {
    int lo = offset;
    int hi = offset + size;
    Object[] a = expectedArray;
    if (l.getArray() != a)
        throw new ConcurrentModificationException();
    if (lo < 0 || hi > a.length)
        throw new IndexOutOfBoundsException();
    return Spliterators.spliterator
        (a, lo, hi, Spliterator.IMMUTABLE | Spliterator.ORDERED);
}
项目:openjdk-jdk10    文件:LinkedBlockingDeque.java   
public Spliterator<E> trySplit() {
    Node<E> h;
    if (!exhausted &&
        ((h = current) != null || (h = first) != null)
        && h.next != null) {
        int n = batch = Math.min(batch + 1, MAX_BATCH);
        Object[] a = new Object[n];
        final ReentrantLock lock = LinkedBlockingDeque.this.lock;
        int i = 0;
        Node<E> p = current;
        lock.lock();
        try {
            if (p != null || (p = first) != null)
                for (; p != null && i < n; p = succ(p))
                    if ((a[i] = p.item) != null)
                        i++;
        } finally {
            lock.unlock();
        }
        if ((current = p) == null) {
            est = 0L;
            exhausted = true;
        }
        else if ((est -= i) < 0L)
            est = 0L;
        if (i > 0)
            return Spliterators.spliterator
                (a, 0, i, (Spliterator.ORDERED |
                           Spliterator.NONNULL |
                           Spliterator.CONCURRENT));
    }
    return null;
}
项目:OpenJSharp    文件:BufferedReader.java   
/**
 * Returns a {@code Stream}, the elements of which are lines read from
 * this {@code BufferedReader}.  The {@link Stream} is lazily populated,
 * i.e., read only occurs during the
 * <a href="../util/stream/package-summary.html#StreamOps">terminal
 * stream operation</a>.
 *
 * <p> The reader must not be operated on during the execution of the
 * terminal stream operation. Otherwise, the result of the terminal stream
 * operation is undefined.
 *
 * <p> After execution of the terminal stream operation there are no
 * guarantees that the reader will be at a specific position from which to
 * read the next character or line.
 *
 * <p> If an {@link IOException} is thrown when accessing the underlying
 * {@code BufferedReader}, it is wrapped in an {@link
 * UncheckedIOException} which will be thrown from the {@code Stream}
 * method that caused the read to take place. This method will return a
 * Stream if invoked on a BufferedReader that is closed. Any operation on
 * that stream that requires reading from the BufferedReader after it is
 * closed, will cause an UncheckedIOException to be thrown.
 *
 * @return a {@code Stream<String>} providing the lines of text
 *         described by this {@code BufferedReader}
 *
 * @since 1.8
 */
public Stream<String> lines() {
    Iterator<String> iter = new Iterator<String>() {
        String nextLine = null;

        @Override
        public boolean hasNext() {
            if (nextLine != null) {
                return true;
            } else {
                try {
                    nextLine = readLine();
                    return (nextLine != null);
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
        }

        @Override
        public String next() {
            if (nextLine != null || hasNext()) {
                String line = nextLine;
                nextLine = null;
                return line;
            } else {
                throw new NoSuchElementException();
            }
        }
    };
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
            iter, Spliterator.ORDERED | Spliterator.NONNULL), false);
}
项目:holon-core    文件:ConversionUtils.java   
/**
 * Convert given {@link Enumeration} into a {@link Stream}.
 * @param <T> Enumeration elements type
 * @param e Enumeration to convert
 * @return Stream on enumeration
 */
public static <T> Stream<T> enumerationAsStream(Enumeration<T> e) {
    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(new Iterator<T>() {
        @Override
        public T next() {
            return e.nextElement();
        }

        @Override
        public boolean hasNext() {
            return e.hasMoreElements();
        }
    }, Spliterator.ORDERED), false);
}
项目:jdk8u-jdk    文件:CharSequence.java   
/**
 * Returns a stream of {@code int} zero-extending the {@code char} values
 * from this sequence.  Any char which maps to a <a
 * href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
 * point</a> is passed through uninterpreted.
 *
 * <p>If the sequence is mutated while the stream is being read, the
 * result is undefined.
 *
 * @return an IntStream of char values from this sequence
 * @since 1.8
 */
public default IntStream chars() {
    class CharIterator implements PrimitiveIterator.OfInt {
        int cur = 0;

        public boolean hasNext() {
            return cur < length();
        }

        public int nextInt() {
            if (hasNext()) {
                return charAt(cur++);
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void forEachRemaining(IntConsumer block) {
            for (; cur < length(); cur++) {
                block.accept(charAt(cur));
            }
        }
    }

    return StreamSupport.intStream(() ->
            Spliterators.spliterator(
                    new CharIterator(),
                    length(),
                    Spliterator.ORDERED),
            Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,
            false);
}
项目:Parseux    文件:CsvAsDTO.java   
/**
 * Constructor.
 *
 * @param iterator {@link Iterator} of {@link String}, each is a concat of CSV row
 * @param dtoClass DTO
 */
public CsvAsDTO(final Iterator<String> iterator, final Class<T> dtoClass) {
    this(StreamSupport.stream(
            Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false
        ), dtoClass
    );
}
项目:openjdk-jdk10    文件:LinkedTransferQueue.java   
public Spliterator<E> trySplit() {
    Node p, q;
    if ((p = current()) == null || (q = p.next) == null)
        return null;
    int i = 0, n = batch = Math.min(batch + 1, MAX_BATCH);
    Object[] a = null;
    do {
        final Object item = p.item;
        if (p.isData) {
            if (item != null) {
                if (a == null)
                    a = new Object[n];
                a[i++] = item;
            }
        } else if (item == null) {
            p = null;
            break;
        }
        if (p == (p = q))
            p = firstDataNode();
    } while (p != null && (q = p.next) != null && i < n);
    setCurrent(p);
    return (i == 0) ? null :
        Spliterators.spliterator(a, 0, i, (Spliterator.ORDERED |
                                           Spliterator.NONNULL |
                                           Spliterator.CONCURRENT));
}
项目:openjdk-jdk10    文件:CopyOnWriteArrayList.java   
public Spliterator<E> spliterator() {
    synchronized (l.lock) {
        return Spliterators.spliterator(
                getArrayChecked(), offset, offset + size,
                Spliterator.IMMUTABLE | Spliterator.ORDERED);
    }
}
项目:n4js    文件:TestSuite.java   
@Override
public Spliterator<TestCase> spliterator() {
    return Spliterators.spliterator(iterator(), Iterators.size(iterator()), 0);
}
项目:n4js    文件:ExtendedClassesIterable.java   
@Override
public Spliterator<TClass> spliterator() {
    return Spliterators.spliteratorUnknownSize(iterator(), Spliterator.DISTINCT | Spliterator.NONNULL);
}
项目:n4js    文件:SuperInterfacesIterable.java   
@Override
public Spliterator<TInterface> spliterator() {
    return Spliterators.spliteratorUnknownSize(iterator(), Spliterator.DISTINCT | Spliterator.NONNULL);
}
项目:openjdk-jdk10    文件:TestData.java   
@Override
public PrimitiveIterator.OfLong iterator() {
    return Spliterators.iterator(spliterator());
}
项目:Stargraph    文件:DataProvider.java   
@Override
public Spliterator<T> spliterator() {
    return Spliterators.spliteratorUnknownSize(dataIt, Spliterator.NONNULL);
}
项目:googles-monorepo-demo    文件:ImmutableMap.java   
Spliterator<Entry<K, V>> entrySpliterator() {
  return Spliterators.spliterator(
      entryIterator(),
      size(),
      Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.IMMUTABLE | Spliterator.ORDERED);
}
项目:mug    文件:Parallelizer.java   
private static <T> Stream<T> stream(Iterator<? extends T> it) {
  return StreamSupport.stream(
      Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false);
}