001 /*
002 * Java Genetic Algorithm Library (jenetics-3.5.0).
003 * Copyright (c) 2007-2016 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@gmx.at)
019 */
020 package org.jenetics.internal.util;
021
022 import static java.lang.String.format;
023
024 import java.util.function.Supplier;
025
026 /**
027 * Some helper methods for creating hash codes and comparing values.
028 *
029 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
030 * @since 1.0
031 * @version 1.6
032 */
033 public final class require {
034 private require() { noInstance(); }
035
036
037 /**
038 * Calling the constructor of an {@code StaticObject} will always throw an
039 * {@link AssertionError}.
040 *
041 * @throws AssertionError always.
042 */
043 public static void noInstance() {
044 String message = "Object instantiation is not allowed";
045
046 final StackTraceElement[] trace = Thread.currentThread().getStackTrace();
047 if (trace.length >= 3) {
048 message = format(
049 "Instantiation of '%s' is not allowed.",
050 trace[2].getClassName()
051 );
052 }
053
054 throw new AssertionError(message);
055 }
056
057 /**
058 * Check if the specified value is not negative.
059 *
060 * @param value the value to check.
061 * @param message the exception message.
062 * @return the given value.
063 * @throws IllegalArgumentException if {@code value < 0}.
064 */
065 public static double nonNegative(final double value, final String message) {
066 if (value < 0) {
067 throw new IllegalArgumentException(format(
068 "%s must not negative: %f.", message, value
069 ));
070 }
071 return value;
072 }
073
074 /**
075 * Check if the specified value is not negative.
076 *
077 * @param value the value to check.
078 * @return the given value.
079 * @throws IllegalArgumentException if {@code value < 0}.
080 */
081 public static double nonNegative(final double value) {
082 return nonNegative(value, "Value");
083 }
084
085 /**
086 * Check if the given integer is negative.
087 *
088 * @param length the value to check.
089 * @throws NegativeArraySizeException if the given {@code length} is smaller
090 * than zero.
091 */
092 public static int nonNegative(final int length) {
093 if (length < 0) {
094 throw new NegativeArraySizeException(
095 "Length must be greater than zero, but was " + length + ". "
096 );
097 }
098 return length;
099 }
100
101 /**
102 * Require the given {@code value} to be positive (>: 0).
103 * @param value the value to check
104 * @return the given value
105 * @throws IllegalArgumentException if the given {@code value} is smaller than
106 * or equal zero.
107 */
108 public static int positive(final int value) {
109 if (value <= 0) {
110 throw new IllegalArgumentException(format(
111 "Value is not positive: %d", value
112 ));
113 }
114 return value;
115 }
116
117 public static long positive(final long value) {
118 if (value <= 0) {
119 throw new IllegalArgumentException(format(
120 "Value is not positive: %d", value
121 ));
122 }
123 return value;
124 }
125
126 /**
127 * Check if the given double value is within the closed range {@code [0, 1]}.
128 *
129 * @param p the probability to check.
130 * @return p if it is a valid probability.
131 * @throws IllegalArgumentException if {@code p} is not a valid probability.
132 */
133 public static double probability(final double p) {
134 if (p < 0.0 || p > 1.0) {
135 throw new IllegalArgumentException(format(
136 "The given probability is not in the range [0, 1]: %f", p
137 ));
138 }
139 return p;
140 }
141
142 public static <T> T safe(final Supplier<T> access) {
143 try {
144 return access.get();
145 } catch (NullPointerException ignore) {
146 return null;
147 }
148 }
149
150 }
|