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