001/* 002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.cuioss.test.generator.internal.net.java.quickcheck.generator.iterable; 017 018import de.cuioss.test.generator.internal.net.java.quickcheck.Generator; 019import de.cuioss.test.generator.internal.net.java.quickcheck.QuickCheck; 020import de.cuioss.test.generator.internal.net.java.quickcheck.generator.Generators; 021 022import java.util.Iterator; 023import java.util.NoSuchElementException; 024 025import static java.util.Objects.requireNonNull; 026 027public final class Iterables { 028 029 private Iterables() { 030 } 031 032 /** 033 * Convert a generator into a {@link Iterable iterable}.<br> 034 * 035 * The generator will be run {@link QuickCheck#MAX_NUMBER_OF_RUNS} times. 036 */ 037 public static <T> Iterable<T> toIterable(final Generator<T> generator) { 038 return Generators.toIterable(generator); 039 } 040 041 /** 042 * Convert a generator into an {@link Iterable iterable}. 043 * 044 * @param numberOfRuns to execute the runner 045 */ 046 public static <T> Iterable<T> toIterable(final Generator<T> generator, final int numberOfRuns) { 047 return Generators.toIterable(generator, numberOfRuns); 048 } 049 050 /** 051 * Calculate the size of an {@link Iterable}. 052 * <p> 053 * The size of an {@link Iterable} is the number of {@link Iterator#next()} 054 * calls not throwing a {@link NoSuchElementException}. 055 * </p> 056 * 057 * @param iterable to calculate the size of 058 */ 059 public static <T> int sizeOf(Iterable<T> iterable) { 060 requireNonNull(iterable, "iterable"); 061 int size = 0; 062 // noinspection StatementWithEmptyBody 063 for (Iterator<T> iter = iterable.iterator(); iter.hasNext(); iter.next(), size++) 064 ; 065 return size; 066 } 067}