Java Stream Operation containsOnly(Stream stream, long size)

Here you can find the source of containsOnly(Stream stream, long size)

Description

Helper which lazily checks if a Stream contains the number specified WARNING: This consumes the stream rendering it unusable afterwards

License

Open Source License

Parameter

Parameter Description
stream the Stream to check the count against
size the expected number of elements in the stream

Return

true if the expected size is found

Declaration

public static boolean containsOnly(Stream stream, long size) 

Method Source Code

//package com.java2s;
/*//from   ww  w  .jav a  2 s  .c  om
 * Grakn - A Distributed Semantic Database
 * Copyright (C) 2016  Grakn Labs Limited
 *
 * Grakn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Grakn is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
 *
 */

import java.util.Iterator;

import java.util.stream.Stream;

public class Main {
    /**
     * Helper which lazily checks if a {@link Stream} contains the number specified
     * WARNING: This consumes the stream rendering it unusable afterwards
     *
     * @param stream the {@link Stream} to check the count against
     * @param size the expected number of elements in the stream
     * @return true if the expected size is found
     */
    public static boolean containsOnly(Stream stream, long size) {
        long count = 0L;
        Iterator it = stream.iterator();

        while (it.hasNext()) {
            it.next();
            if (++count > size)
                return false;
        }

        return size == count;
    }
}

Related

  1. cat(Stream... streams)
  2. characterStream2(String s)
  3. commaSeparated(Stream stream)
  4. concat(IntStream... streams)
  5. concat(Stream... streams)
  6. createStream(final Iterator iterator)
  7. endsWith(Stream stream, Iterable iterable)
  8. enumerationAsStream(Enumeration e)
  9. equals(Stream first, Stream second)