001/*
002 * contributor license agreements. See the NOTICE file distributed with
003 * this work for additional information regarding copyright ownership.
004 * The ASF licenses this file to You under the Apache License, Version 2.0
005 * (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Licensed to the author under one or more
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 de.cuioss.tools.base.Preconditions.checkArgument;
020import static java.lang.String.format;
021
022import java.util.Objects;
023
024import de.cuioss.test.generator.internal.net.java.quickcheck.Generator;
025import de.cuioss.test.generator.internal.net.java.quickcheck.generator.distribution.Distribution;
026
027public class DoubleGenerator implements Generator<Double> {
028
029    private final double min;
030    private final double max;
031    private final Distribution distribution;
032
033    public DoubleGenerator() {
034        this(Double.MIN_VALUE, Double.MAX_VALUE);
035    }
036
037    public DoubleGenerator(double min, double max) {
038        this(min, max, Distribution.UNIFORM);
039    }
040
041    public DoubleGenerator(double min, double max, Distribution dist) {
042        checkArgument(max >= min, "min");
043        Objects.requireNonNull(dist, "dist");
044
045        this.min = min;
046        this.max = max;
047        this.distribution = dist;
048    }
049
050    @Override
051    public Double next() {
052        return nextDouble();
053    }
054
055    public double nextDouble() {
056        return this.distribution.nextRandomNumber() * (this.max - this.min) + this.min;
057    }
058
059    @Override
060    public String toString() {
061        return format("%s[min=%s, max=%s, distribution=%s", getClass().getSimpleName(), min, max, distribution);
062    }
063}