001/* 002 * Licensed to the author under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package de.cuioss.test.generator.internal.net.java.quickcheck.generator.support; 018 019import static java.lang.Math.max; 020 021import java.util.Iterator; 022import java.util.Objects; 023 024import de.cuioss.test.generator.internal.net.java.quickcheck.Generator; 025 026public class IteratorGenerator<T> implements Generator<Iterator<T>> { 027 028 public static final int MIN_SIZE = 0; 029 030 // why call the default size max_size if this size does not limit the upper 031 // bound for all lists? ListGenerator(Generator, int int) does not define 032 // any limit on max, so i think max_size does not reflect what the value 033 // is used for. previously it was named default_size but should better 034 // be named like default_max_size 035 public static final int MAX_SIZE = 10; 036 037 private final Generator<? extends T> content; 038 private final Generator<Integer> size; 039 040 public IteratorGenerator(Generator<? extends T> content) { 041 this(content, MIN_SIZE, MAX_SIZE); 042 } 043 044 public IteratorGenerator(Generator<? extends T> content, int min, int max) { 045 this(content, new IntegerGenerator(min, max)); 046 } 047 048 public IteratorGenerator(Generator<? extends T> content, Generator<Integer> size) { 049 Objects.requireNonNull(content, "content"); 050 Objects.requireNonNull(size, "size"); 051 052 this.content = content; 053 this.size = size; 054 } 055 056 @Override 057 public Iterator<T> next() { 058 final int localSize = max(MIN_SIZE, this.size.next()); 059 return new Iterator<>() { 060 061 private int i; 062 063 @Override 064 public boolean hasNext() { 065 return i < localSize; 066 } 067 068 @Override 069 @SuppressWarnings("java:S2272") // owolff: For generator by design 070 public T next() { 071 i++; 072 return content.next(); 073 074 } 075 076 @Override 077 public void remove() { 078 throw new UnsupportedOperationException("remove not supported."); 079 } 080 }; 081 } 082}