001/* 002 * Java Genetic Algorithm Library (jenetics-7.2.0). 003 * Copyright (c) 2007-2023 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.util; 021 022import static java.lang.String.format; 023import static io.jenetics.internal.util.Hashes.hash; 024 025import java.io.DataInput; 026import java.io.DataOutput; 027import java.io.IOException; 028import java.io.InvalidObjectException; 029import java.io.ObjectInputStream; 030import java.io.Serial; 031import java.io.Serializable; 032 033/** 034 * Double range class. 035 * 036 * @implNote 037 * This class is immutable and thread-safe. 038 * 039 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 040 * @version 6.0 041 * @since 3.2 042 */ 043public final /*record*/ class DoubleRange implements Serializable { 044 045 @Serial 046 private static final long serialVersionUID = 2L; 047 048 private final double _min; 049 private final double _max; 050 051 private DoubleRange(final double min, final double max) { 052 if (min > max) { 053 throw new IllegalArgumentException(format( 054 "Min greater than max: %s > %s", min, max 055 )); 056 } 057 058 _min = min; 059 _max = max; 060 } 061 062 /** 063 * Return the minimum value of the double range. 064 * 065 * @return the minimum value of the double range 066 */ 067 public double min() { 068 return _min; 069 } 070 071 /** 072 * Return the maximum value of the double range. 073 * 074 * @return the maximum value of the double range 075 */ 076 public double max() { 077 return _max; 078 } 079 080 /** 081 * Create a new {@code DoubleRange} object with the given {@code min} and 082 * {@code max} values. 083 * 084 * @param min the lower bound of the double range 085 * @param max the upper bound of the double range 086 * @return a new {@code DoubleRange} object 087 * @throws IllegalArgumentException if {@code min > max} 088 */ 089 public static DoubleRange of(final double min, final double max) { 090 return new DoubleRange(min, max); 091 } 092 093 @Override 094 public int hashCode() { 095 return hash(_min, hash(_max)); 096 } 097 098 @Override 099 public boolean equals(final Object obj) { 100 return obj == this || 101 obj instanceof DoubleRange other && 102 Double.compare(_min, other._min) == 0 && 103 Double.compare(_max, other._max) == 0; 104 } 105 106 @Override 107 public String toString() { 108 return "[" + _min + ", " + _max + "]"; 109 } 110 111 112 /* ************************************************************************* 113 * Java object serialization 114 * ************************************************************************/ 115 116 @Serial 117 private Object writeReplace() { 118 return new SerialProxy(SerialProxy.DOUBLE_RANGE, this); 119 } 120 121 @Serial 122 private void readObject(final ObjectInputStream stream) 123 throws InvalidObjectException 124 { 125 throw new InvalidObjectException("Serialization proxy required."); 126 } 127 128 void write(final DataOutput out) throws IOException { 129 out.writeDouble(_min); 130 out.writeDouble(_max); 131 } 132 133 static DoubleRange read(final DataInput in) throws IOException { 134 return of(in.readDouble(), in.readDouble()); 135 } 136 137}