001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://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 018package org.apache.commons.lang3; 019 020/** 021 * Specializes {@link NumberRange} for {@link Double}s. 022 * 023 * <p> 024 * This class is not designed to interoperate with other NumberRanges 025 * </p> 026 * 027 * @since 3.13.0 028 */ 029public final class DoubleRange extends NumberRange<Double> { 030 031 private static final long serialVersionUID = 1L; 032 033 /** 034 * Creates a range with the specified minimum and maximum values (both inclusive). 035 * 036 * <p> 037 * The range uses the natural ordering of the elements to determine where values lie in the range. 038 * </p> 039 * 040 * <p> 041 * The arguments may be passed in the order (min, max) or (max,min). The getMinimum and getMaximum methods will return the correct values. 042 * </p> 043 * 044 * @param fromInclusive The first value that defines the edge of the range, inclusive. 045 * @param toInclusive The second value that defines the edge of the range, inclusive. 046 * @return The range object, not null. 047 * @throws IllegalArgumentException Thrown if either value is NaN. 048 */ 049 public static DoubleRange of(final double fromInclusive, final double toInclusive) { 050 return of(Double.valueOf(fromInclusive), Double.valueOf(toInclusive)); 051 } 052 053 /** 054 * Creates a range with the specified minimum and maximum values (both inclusive). 055 * 056 * <p> 057 * The range uses the natural ordering of the elements to determine where values lie in the range. 058 * </p> 059 * 060 * <p> 061 * The arguments may be passed in the order (min, max) or (max,min). The getMinimum and getMaximum methods will return the correct values. 062 * </p> 063 * 064 * @param fromInclusive The first value that defines the edge of the range, inclusive. 065 * @param toInclusive The second value that defines the edge of the range, inclusive. 066 * @return The range object, not null. 067 * @throws NullPointerException Thrown if either element is null. 068 * @throws IllegalArgumentException Thrown if either element is NaN. 069 */ 070 public static DoubleRange of(final Double fromInclusive, final Double toInclusive) { 071 return new DoubleRange(fromInclusive, toInclusive); 072 } 073 074 /** 075 * Creates an instance. 076 * 077 * @param number1 The first element, not null. 078 * @param number2 The second element, not null. 079 * @throws NullPointerException Thrown when element1 is null. 080 * @throws NullPointerException Thrown when element2 is null. 081 * @throws IllegalArgumentException Thrown when element1 or element2 is NaN. 082 */ 083 private DoubleRange(final Double number1, final Double number2) { 084 super(number1, number2, null); 085 } 086 087 /** 088 * Fits the given value into this range by returning the given value or, if out of bounds, the range minimum if 089 * below, or the range maximum if above. 090 * 091 * <pre>{@code 092 * LongRange range = LongRange.of(16, 64); 093 * range.fit(-9) --> 16 094 * range.fit(0) --> 16 095 * range.fit(15) --> 16 096 * range.fit(16) --> 16 097 * range.fit(17) --> 17 098 * ... 099 * range.fit(63) --> 63 100 * range.fit(64) --> 64 101 * range.fit(99) --> 64 102 * }</pre> 103 * 104 * @param element The element to test. 105 * @return The minimum, the element, or the maximum depending on the element's location relative to the range. 106 * @since 3.19.0 107 */ 108 public double fit(final double element) { 109 return super.fit(element).doubleValue(); 110 } 111 112}