Java Code Examples for java.util.Spliterators#spliterator()

The following examples show how to use java.util.Spliterators#spliterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Loader.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Stream<URL> resources(String name) {
    Objects.requireNonNull(name);
    // ordering not specified
    int characteristics = (Spliterator.NONNULL | Spliterator.IMMUTABLE |
                           Spliterator.SIZED | Spliterator.SUBSIZED);
    Supplier<Spliterator<URL>> supplier = () -> {
        try {
            List<URL> urls = findResourcesAsList(name);
            return Spliterators.spliterator(urls, characteristics);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    };
    Stream<URL> s1 = StreamSupport.stream(supplier, characteristics, false);
    Stream<URL> s2 = parent.resources(name);
    return Stream.concat(s1, s2);
}
 
Example 2
Source File: LinkedBlockingQueue.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public Spliterator<E> trySplit() {
    Node<E> h;
    final LinkedBlockingQueue<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.head.next) != null) &&
        h.next != null) {
        Object[] a = new Object[n];
        int i = 0;
        Node<E> p = current;
        q.fullyLock();
        try {
            if (p != null || (p = q.head.next) != null) {
                do {
                    if ((a[i] = p.item) != null)
                        ++i;
                } while ((p = p.next) != null && i < n);
            }
        } finally {
            q.fullyUnlock();
        }
        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;
}
 
Example 3
Source File: TrieSetMultimap_HHAMT.java    From capsule with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private Spliterator<io.usethesource.capsule.Set.Immutable<V>> valueCollectionsSpliterator() {
  /*
   * TODO: specialize between mutable / SetMultimap.Immutable<K, V> ({@see Spliterator.IMMUTABLE})
   */
  int characteristics = Spliterator.NONNULL | Spliterator.SIZED | Spliterator.SUBSIZED;
  return Spliterators.spliterator(new SetMultimapValueIterator<>(rootNode), size(),
      characteristics);
}
 
Example 4
Source File: CompactHashMap.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public Spliterator<V> spliterator() {
  if (needsAllocArrays()) {
    return Spliterators.spliterator(new Object[0], Spliterator.ORDERED);
  }
  return Spliterators.spliterator(values, 0, size, Spliterator.ORDERED);
}
 
Example 5
Source File: SpliteratorsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testSpliteratorObjectArray() {
    String[] array = { "a", "b", "c", "d", "e", "f", "g", "h" };
    ArrayList<String> expectedValues = new ArrayList<>(Arrays.asList(array));

    Spliterator<String> sp = Spliterators.spliterator(array, 0);
    assertEquals(8, sp.estimateSize());
    assertEquals(8, sp.getExactSizeIfKnown());

    sp = Spliterators.spliterator(array, 0);
    SpliteratorTester.runBasicIterationTests(sp, expectedValues);

    sp = Spliterators.spliterator(array, 0);
    SpliteratorTester.testSpliteratorNPE(sp);

    sp = Spliterators.spliterator(array, 0);
    SpliteratorTester.runBasicSplitTests(sp, expectedValues, String::compareTo);

    sp = Spliterators.spliterator(array, 0);
    SpliteratorTester.runSizedTests(sp, 8);

    sp = Spliterators.spliterator(array, 0);
    SpliteratorTester.runSubSizedTests(sp, 8);

    // Assert the spliterator inherits any characteristics we ask it to.
    sp = Spliterators.spliterator(array, Spliterator.ORDERED);
    assertTrue(sp.hasCharacteristics(Spliterator.ORDERED));
}
 
Example 6
Source File: LinkedBlockingDeque.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: BaseResult.java    From requery with Apache License 2.0 5 votes vote down vote up
@Override
public Stream<E> stream() {
    final CloseableIterator<E> iterator = createIterator();
    Spliterator<E> spliterator = maxSize == null ?
        Spliterators.spliteratorUnknownSize(iterator, 0) :
        Spliterators.spliterator(iterator, maxSize, 0);
    return StreamSupport.stream(spliterator, false).onClose(new Runnable() {
        @Override
        public void run() {
            iterator.close();
        }
    });
}
 
Example 8
Source File: SpliteratorsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testSpliterator_sizedIterator() {
    String[] array = { "a", "b", "c", "d", "e", "f", "g", "h" };
    List<String> asList = Arrays.asList(array);
    ArrayList<String> expectedValues = new ArrayList<>(asList);

    Spliterator<String> sp = Spliterators.spliterator(asList.iterator(), 8 /* size */, 0);
    assertEquals(8, sp.estimateSize());
    assertEquals(8, sp.getExactSizeIfKnown());

    sp = Spliterators.spliterator(asList.iterator(), 8 /* size */, 0);
    SpliteratorTester.runBasicIterationTests(sp, expectedValues);

    sp = Spliterators.spliterator(asList.iterator(), 8 /* size */, 0);
    SpliteratorTester.testSpliteratorNPE(sp);

    sp = Spliterators.spliterator(asList.iterator(), 8 /* size */, 0);
    SpliteratorTester.runBasicSplitTests(sp, expectedValues, String::compareTo);

    sp = Spliterators.spliterator(asList.iterator(), 8 /* size */, 0);
    SpliteratorTester.runSizedTests(sp, 8);

    sp = Spliterators.spliterator(asList.iterator(), 8 /* size */, 0);
    SpliteratorTester.runSubSizedTests(sp, 8);

    // Assert the spliterator inherits any characteristics we ask it to.
    sp = Spliterators.spliterator(array, Spliterator.ORDERED);
    assertTrue(sp.hasCharacteristics(Spliterator.ORDERED));
}
 
Example 9
Source File: DistinctOpTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: EnumSelector.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns a spliterator of all the allowed elements in this EnumSelector.
 *
 * @return The spliterator.
 * @throws IllegalStateException If the EnumSelector has not been {@link #lock() locked}.
 */
@Override
public Spliterator<T> spliterator() {
	checkReadable();
	Set<T> set = toSet();
	return Spliterators.spliterator(set.iterator(), set.size(),
			Spliterator.DISTINCT | Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE);
}
 
Example 11
Source File: CopyOnWriteArrayList.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: LinkedBlockingDeque.java    From j2objc with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: LinkedBlockingQueue.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public Spliterator<E> trySplit() {
    Node<E> h;
    final LinkedBlockingQueue<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.head.next) != null) &&
        h.next != null) {
        Object[] a = new Object[n];
        int i = 0;
        Node<E> p = current;
        q.fullyLock();
        try {
            if (p != null || (p = q.head.next) != null) {
                do {
                    if ((a[i] = p.item) != null)
                        ++i;
                } while ((p = p.next) != null && i < n);
            }
        } finally {
            q.fullyUnlock();
        }
        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;
}
 
Example 14
Source File: CharacterList.java    From asteria-3.0 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Spliterator<E> spliterator() {
    return Spliterators.spliterator(characters, Spliterator.ORDERED);
}
 
Example 15
Source File: CraftingGrid.java    From NOVA-Core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
default Spliterator<Item> spliterator() {
	return Spliterators.spliterator(iterator(), size(), Spliterator.NONNULL | Spliterator.ORDERED | Spliterator.SORTED);
}
 
Example 16
Source File: LinkedHashMapFixed.java    From streamsupport with GNU General Public License v2.0 4 votes vote down vote up
@IgnoreJava8API
@Override
public Spliterator<Map.Entry<K, V>> spliterator() {
    return Spliterators.spliterator(this, Spliterator.SIZED | Spliterator.ORDERED | Spliterator.DISTINCT);
}
 
Example 17
Source File: ArrayBlockingQueue.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a {@link Spliterator} over the elements in this queue.
 *
 * <p>The returned spliterator is
 * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
 *
 * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
 * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
 *
 * @implNote
 * The {@code Spliterator} implements {@code trySplit} to permit limited
 * parallelism.
 *
 * @return a {@code Spliterator} over the elements in this queue
 * @since 1.8
 */
public Spliterator<E> spliterator() {
    return Spliterators.spliterator
        (this, Spliterator.ORDERED | Spliterator.NONNULL |
         Spliterator.CONCURRENT);
}
 
Example 18
Source File: CopyOnWriteArraySet.java    From jdk8u-jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a {@link Spliterator} over the elements in this set in the order
 * in which these elements were added.
 *
 * <p>The {@code Spliterator} reports {@link Spliterator#IMMUTABLE},
 * {@link Spliterator#DISTINCT}, {@link Spliterator#SIZED}, and
 * {@link Spliterator#SUBSIZED}.
 *
 * <p>The spliterator provides a snapshot of the state of the set
 * when the spliterator was constructed. No synchronization is needed while
 * operating on the spliterator.
 *
 * @return a {@code Spliterator} over the elements in this set
 * @since 1.8
 */
public Spliterator<E> spliterator() {
    return Spliterators.spliterator
        (al.getArray(), Spliterator.IMMUTABLE | Spliterator.DISTINCT);
}
 
Example 19
Source File: ArrayBlockingQueue.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a {@link Spliterator} over the elements in this queue.
 *
 * <p>The returned spliterator is
 * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
 *
 * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
 * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
 *
 * @implNote
 * The {@code Spliterator} implements {@code trySplit} to permit limited
 * parallelism.
 *
 * @return a {@code Spliterator} over the elements in this queue
 * @since 1.8
 */
public Spliterator<E> spliterator() {
    return Spliterators.spliterator
        (this, Spliterator.ORDERED | Spliterator.NONNULL |
         Spliterator.CONCURRENT);
}
 
Example 20
Source File: CopyOnWriteArraySet.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a {@link Spliterator} over the elements in this set in the order
 * in which these elements were added.
 *
 * <p>The {@code Spliterator} reports {@link Spliterator#IMMUTABLE},
 * {@link Spliterator#DISTINCT}, {@link Spliterator#SIZED}, and
 * {@link Spliterator#SUBSIZED}.
 *
 * <p>The spliterator provides a snapshot of the state of the set
 * when the spliterator was constructed. No synchronization is needed while
 * operating on the spliterator.
 *
 * @return a {@code Spliterator} over the elements in this set
 * @since 1.8
 */
public Spliterator<E> spliterator() {
    return Spliterators.spliterator
        (al.getArray(), Spliterator.IMMUTABLE | Spliterator.DISTINCT);
}