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 de.cuioss.test.generator.internal.net.java.quickcheck.Generator;
020import de.cuioss.test.generator.internal.net.java.quickcheck.GeneratorException;
021import de.cuioss.tools.base.Preconditions;
022
023/**
024 * Base class for generators which can reject the values generated by their
025 * wrapped generator. This will be tried until the maximum number of tries is
026 * reached.
027 */
028public abstract class VetoableGenerator<T> implements Generator<T> {
029
030    private final Generator<? extends T> generator;
031    private final int maxTries;
032
033    // TODO this could be a bit high
034    // for runs = 200 this means 20000 tries for the worst case
035    public static final int DEFAULT_MAX_TRIES = 100;
036
037    public static final int MIN_TRIES = 1;
038
039    protected VetoableGenerator(Generator<? extends T> generator) {
040        this(generator, DEFAULT_MAX_TRIES);
041    }
042
043    protected VetoableGenerator(Generator<? extends T> generator, int maxTries) {
044        Preconditions.checkArgument(MIN_TRIES <= maxTries, "maxTries");
045        this.generator = generator;
046        this.maxTries = maxTries;
047    }
048
049    @Override
050    public T next() throws GeneratorException {
051        for (int idx = 0; idx < maxTries; idx++) {
052            T value = generator.next();
053            if (tryValue(value))
054                return value;
055        }
056        throw new GeneratorException(
057                "Failed to generate another value after [%s] tries (generator: %s)".formatted(maxTries, generator),
058                generator);
059    }
060
061    /**
062     * @return true to accept the current value.
063     */
064    protected abstract boolean tryValue(T value);
065}