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; 020 021import java.util.Objects; 022 023import de.cuioss.test.generator.internal.net.java.quickcheck.Generator; 024import de.cuioss.test.generator.internal.net.java.quickcheck.generator.distribution.Distribution; 025 026public class DoubleGenerator implements Generator<Double> { 027 028 private final double min; 029 private final double max; 030 private final Distribution distribution; 031 032 public DoubleGenerator() { 033 this(Double.MIN_VALUE, Double.MAX_VALUE); 034 } 035 036 public DoubleGenerator(double min, double max) { 037 this(min, max, Distribution.UNIFORM); 038 } 039 040 public DoubleGenerator(double min, double max, Distribution dist) { 041 checkArgument(max >= min, "min"); 042 Objects.requireNonNull(dist, "dist"); 043 044 this.min = min; 045 this.max = max; 046 this.distribution = dist; 047 } 048 049 @Override 050 public Double next() { 051 return nextDouble(); 052 } 053 054 public double nextDouble() { 055 return this.distribution.nextRandomNumber() * (this.max - this.min) + this.min; 056 } 057 058 @Override 059 public String toString() { 060 return "%s[min=%s, max=%s, distribution=%s".formatted(getClass().getSimpleName(), min, max, distribution); 061 } 062}