Here you can find the source of containsOnly(Stream stream, long size)
Parameter | Description |
---|---|
stream | the Stream to check the count against |
size | the expected number of elements in the stream |
public static boolean containsOnly(Stream stream, long size)
//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; } }