001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.0).
003 * Copyright (c) 2007-2024 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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 *
017 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.internal.util;
021
022import static java.lang.String.format;
023
024/**
025 * Some helper methods for creating hash codes and comparing values.
026 *
027 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
028 * @since 1.0
029 * @version 5.2
030 */
031public final class Requires {
032        private Requires() {}
033
034        /**
035         * Check if the specified value is not negative.
036         *
037         * @param value the value to check.
038         * @param message the exception message.
039         * @return the given value.
040         * @throws IllegalArgumentException if {@code value < 0}.
041         */
042        public static double nonNegative(final double value, final String message) {
043                if (value < 0) {
044                        throw new IllegalArgumentException(format(
045                                "%s must not be negative: %f.", message, value
046                        ));
047                }
048                return value;
049        }
050
051        /**
052         * Check if the specified value is not negative.
053         *
054         * @param value the value to check.
055         * @return the given value.
056         * @throws IllegalArgumentException if {@code value < 0}.
057         */
058        public static double nonNegative(final double value) {
059                return nonNegative(value, "Value");
060        }
061
062        /**
063         * Check if the given integer is negative.
064         *
065         * @param length the value to check.
066         * @return the length input
067         * @throws NegativeArraySizeException if the given {@code length} is smaller
068         *                than zero.
069         */
070        public static int nonNegative(final int length) {
071                if (length < 0) {
072                        throw new NegativeArraySizeException(
073                                "Length must be greater than zero, but was " + length + ". "
074                        );
075                }
076                return length;
077        }
078
079        /**
080         * Require the given {@code value} to be positive (&gt; 0).
081         * @param value the value to check
082         * @return the given value
083         * @throws IllegalArgumentException if the given {@code value} is smaller than
084         *         or equal zero.
085         */
086        public static int positive(final int value) {
087                if (value <= 0) {
088                        throw new IllegalArgumentException(format(
089                                "Value is not positive: %d", value
090                        ));
091                }
092                return value;
093        }
094
095        public static long positive(final long value) {
096                if (value <= 0) {
097                        throw new IllegalArgumentException(format(
098                                "Value is not positive: %d", value
099                        ));
100                }
101                return value;
102        }
103
104        public static double positive(final double value) {
105                if (Double.compare(value, 0) <= 0) {
106                        throw new IllegalArgumentException(format(
107                                "Value is not positive: %f", value
108                        ));
109                }
110                return value;
111        }
112
113        /**
114         * Check if the given double value is within the closed range {@code [0, 1]}.
115         *
116         * @param p the probability to check.
117         * @return p if it is a valid probability.
118         * @throws IllegalArgumentException if {@code p} is not a valid probability.
119         */
120        public static double probability(final double p) {
121                if (p < 0.0 || p > 1.0) {
122                        throw new IllegalArgumentException(format(
123                                "The given probability is not in the range [0, 1]: %f", p
124                        ));
125                }
126                return p;
127        }
128
129}