Java Code Examples for java.util.Spliterator#IMMUTABLE

The following examples show how to use java.util.Spliterator#IMMUTABLE . 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: Stream.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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}.
 *
 * <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 <T> the type of stream elements
 * @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 Stream}
 */
public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) {
    Objects.requireNonNull(f);
    Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE,
           Spliterator.ORDERED | Spliterator.IMMUTABLE) {
        T prev;
        boolean started;

        @Override
        public boolean tryAdvance(Consumer<? super T> action) {
            Objects.requireNonNull(action);
            T t;
            if (started)
                t = f.apply(prev);
            else {
                t = seed;
                started = true;
            }
            action.accept(prev = t);
            return true;
        }
    };
    return StreamSupport.stream(spliterator, false);
}
 
Example 2
Source File: Streamer.java    From FHIR with Apache License 2.0 5 votes vote down vote up
/**
 * Wrap the JDBC {@link ResultSet} as a stream object. Note that the stream has to be consumed within
 * the boundaries of the statement execution
 * @param rs
 * @return
 */
public Stream<Row> wrap(final ResultSet rs) throws SQLException {
    ResultSetMetaData md = rs.getMetaData();
    final String[] columnNames = new String[md.getColumnCount()];
    for (int i=0; i<columnNames.length; i++) {
        columnNames[i] = md.getColumnName(i+1);
    }
    
    Spliterator<Row> s = new Spliterators.AbstractSpliterator<Row>(Long.MAX_VALUE, Spliterator.NONNULL | Spliterator.IMMUTABLE) {

        @Override
        public boolean tryAdvance(Consumer<? super Row> action) {
            try {
                if (!rs.next()) {
                    return false;
                }
                
                // wrap the current result record as a Row which is then handed
                // off to the stream
                action.accept(new Row(rs, columnNames));
                return true;
            }
            catch (SQLException x) {
                // ouch
                logger.log(Level.SEVERE, x.getMessage(), x);
                return false;
            }
        }
    };
    
    // Create a sequential stream of the rows from the result set
    return StreamSupport.stream(s, false);
}
 
Example 3
Source File: ThreadLocalRandom.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public int characteristics() {
    return (Spliterator.SIZED | Spliterator.SUBSIZED |
            Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
 
Example 4
Source File: Streams.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int characteristics() {
    return Spliterator.SIZED | Spliterator.SUBSIZED |
           Spliterator.ORDERED | Spliterator.IMMUTABLE;
}
 
Example 5
Source File: SystemModuleFinders.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public int characteristics() {
    return Spliterator.DISTINCT + Spliterator.NONNULL + Spliterator.IMMUTABLE;
}
 
Example 6
Source File: ThreadLocalRandom.java    From desugar_jdk_libs with GNU General Public License v2.0 4 votes vote down vote up
public int characteristics() {
    return (Spliterator.SIZED | Spliterator.SUBSIZED |
            Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
 
Example 7
Source File: ThreadLocalRandom.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public int characteristics() {
    return (Spliterator.SIZED | Spliterator.SUBSIZED |
            Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
 
Example 8
Source File: Streams.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int characteristics() {
    return Spliterator.SIZED | Spliterator.SUBSIZED |
           Spliterator.ORDERED | Spliterator.IMMUTABLE;
}
 
Example 9
Source File: ThreadLocalRandom.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public int characteristics() {
    return (Spliterator.SIZED | Spliterator.SUBSIZED |
            Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
 
Example 10
Source File: Streams.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int characteristics() {
    return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
           Spliterator.IMMUTABLE | Spliterator.NONNULL |
           Spliterator.DISTINCT | Spliterator.SORTED;
}
 
Example 11
Source File: ThreadLocalRandom.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public int characteristics() {
    return (Spliterator.SIZED | Spliterator.SUBSIZED |
            Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
 
Example 12
Source File: Streams.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int characteristics() {
    return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
           Spliterator.IMMUTABLE | Spliterator.NONNULL |
           Spliterator.DISTINCT | Spliterator.SORTED;
}
 
Example 13
Source File: Streams.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int characteristics() {
    return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
           Spliterator.IMMUTABLE | Spliterator.NONNULL |
           Spliterator.DISTINCT | Spliterator.SORTED;
}
 
Example 14
Source File: Streams.java    From desugar_jdk_libs with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int characteristics() {
    return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
           Spliterator.IMMUTABLE | Spliterator.NONNULL |
           Spliterator.DISTINCT | Spliterator.SORTED;
}
 
Example 15
Source File: Streams.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public int characteristics() {
    return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
           Spliterator.IMMUTABLE | Spliterator.NONNULL |
           Spliterator.DISTINCT | Spliterator.SORTED;
}
 
Example 16
Source File: Streams.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int characteristics() {
    return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED |
           Spliterator.IMMUTABLE | Spliterator.NONNULL |
           Spliterator.DISTINCT | Spliterator.SORTED;
}
 
Example 17
Source File: SplittableRandomTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public int characteristics() {
    return (Spliterator.SIZED | Spliterator.SUBSIZED |
            Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
 
Example 18
Source File: ThreadLocalRandom.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public int characteristics() {
    return (Spliterator.SIZED | Spliterator.SUBSIZED |
            Spliterator.NONNULL | Spliterator.IMMUTABLE);
}
 
Example 19
Source File: ClassLoader.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns a stream whose elements are the URLs of all the resources with
 * the given name. A resource is some data (images, audio, text, etc) that
 * can be accessed by class code in a way that is independent of the
 * location of the code.
 *
 * <p> The name of a resource is a {@code /}-separated path name that
 * identifies the resource.
 *
 * <p> The resources will be located when the returned stream is evaluated.
 * If the evaluation results in an {@code IOException} then the I/O
 * exception is wrapped in an {@link UncheckedIOException} that is then
 * thrown.
 *
 * <p> Resources in named modules are subject to the encapsulation rules
 * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
 * Additionally, and except for the special case where the resource has a
 * name ending with "{@code .class}", this method will only find resources in
 * packages of named modules when the package is {@link Module#isOpen(String)
 * opened} unconditionally (even if the caller of this method is in the
 * same module as the resource). </p>
 *
 * @implSpec The default implementation invokes {@link #getResources(String)
 * getResources} to find all the resources with the given name and returns
 * a stream with the elements in the enumeration as the source.
 *
 * @apiNote When overriding this method it is recommended that an
 * implementation ensures that any delegation is consistent with the {@link
 * #getResource(java.lang.String) getResource(String)} method. This should
 * ensure that the first element returned by the stream is the same
 * resource that the {@code getResource(String)} method would return.
 *
 * @param  name
 *         The resource name
 *
 * @return  A stream of resource {@link java.net.URL URL} objects. If no
 *          resources could  be found, the stream will be empty. Resources
 *          for which a {@code URL} cannot be constructed, are in a package
 *          that is not opened unconditionally, or access to the resource
 *          is denied by the security manager, will not be in the stream.
 *
 * @throws  NullPointerException If {@code name} is {@code null}
 *
 * @since  9
 */
public Stream<URL> resources(String name) {
    Objects.requireNonNull(name);
    int characteristics = Spliterator.NONNULL | Spliterator.IMMUTABLE;
    Supplier<Spliterator<URL>> si = () -> {
        try {
            return Spliterators.spliteratorUnknownSize(
                getResources(name).asIterator(), characteristics);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    };
    return StreamSupport.stream(si, characteristics, false);
}
 
Example 20
Source File: ClassLoader.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a stream whose elements are the URLs of all the resources with
 * the given name. A resource is some data (images, audio, text, etc) that
 * can be accessed by class code in a way that is independent of the
 * location of the code.
 *
 * <p> The name of a resource is a {@code /}-separated path name that
 * identifies the resource.
 *
 * <p> The resources will be located when the returned stream is evaluated.
 * If the evaluation results in an {@code IOException} then the I/O
 * exception is wrapped in an {@link UncheckedIOException} that is then
 * thrown.
 *
 * <p> Resources in named modules are subject to the encapsulation rules
 * specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
 * Additionally, and except for the special case where the resource has a
 * name ending with "{@code .class}", this method will only find resources in
 * packages of named modules when the package is {@link Module#isOpen(String)
 * opened} unconditionally (even if the caller of this method is in the
 * same module as the resource). </p>
 *
 * @implSpec The default implementation invokes {@link #getResources(String)
 * getResources} to find all the resources with the given name and returns
 * a stream with the elements in the enumeration as the source.
 *
 * @apiNote When overriding this method it is recommended that an
 * implementation ensures that any delegation is consistent with the {@link
 * #getResource(java.lang.String) getResource(String)} method. This should
 * ensure that the first element returned by the stream is the same
 * resource that the {@code getResource(String)} method would return.
 *
 * @param  name
 *         The resource name
 *
 * @return  A stream of resource {@link java.net.URL URL} objects. If no
 *          resources could  be found, the stream will be empty. Resources
 *          for which a {@code URL} cannot be constructed, are in a package
 *          that is not opened unconditionally, or access to the resource
 *          is denied by the security manager, will not be in the stream.
 *
 * @throws  NullPointerException If {@code name} is {@code null}
 *
 * @since  9
 */
public Stream<URL> resources(String name) {
    Objects.requireNonNull(name);
    int characteristics = Spliterator.NONNULL | Spliterator.IMMUTABLE;
    Supplier<Spliterator<URL>> si = () -> {
        try {
            return Spliterators.spliteratorUnknownSize(
                getResources(name).asIterator(), characteristics);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    };
    return StreamSupport.stream(si, characteristics, false);
}