View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3;
19  
20  /**
21   * Specializes {@link NumberRange} for {@link Double}s.
22   *
23   * <p>
24   * This class is not designed to interoperate with other NumberRanges
25   * </p>
26   *
27   * @since 3.13.0
28   */
29  public final class DoubleRange extends NumberRange<Double> {
30  
31      private static final long serialVersionUID = 1L;
32  
33      /**
34       * Creates a range with the specified minimum and maximum values (both inclusive).
35       *
36       * <p>
37       * The range uses the natural ordering of the elements to determine where values lie in the range.
38       * </p>
39       *
40       * <p>
41       * The arguments may be passed in the order (min, max) or (max,min). The getMinimum and getMaximum methods will return the correct values.
42       * </p>
43       *
44       * @param fromInclusive The first value that defines the edge of the range, inclusive.
45       * @param toInclusive The second value that defines the edge of the range, inclusive.
46       * @return The range object, not null.
47       * @throws IllegalArgumentException Thrown if either value is NaN.
48       */
49      public static DoubleRange of(final double fromInclusive, final double toInclusive) {
50          return of(Double.valueOf(fromInclusive), Double.valueOf(toInclusive));
51      }
52  
53      /**
54       * Creates a range with the specified minimum and maximum values (both inclusive).
55       *
56       * <p>
57       * The range uses the natural ordering of the elements to determine where values lie in the range.
58       * </p>
59       *
60       * <p>
61       * The arguments may be passed in the order (min, max) or (max,min). The getMinimum and getMaximum methods will return the correct values.
62       * </p>
63       *
64       * @param fromInclusive The first value that defines the edge of the range, inclusive.
65       * @param toInclusive The second value that defines the edge of the range, inclusive.
66       * @return The range object, not null.
67       * @throws NullPointerException Thrown if either element is null.
68       * @throws IllegalArgumentException Thrown if either element is NaN.
69       */
70      public static DoubleRange of(final Double fromInclusive, final Double toInclusive) {
71          return new DoubleRange(fromInclusive, toInclusive);
72      }
73  
74      /**
75       * Creates an instance.
76       *
77       * @param number1 The first element, not null.
78       * @param number2 The second element, not null.
79       * @throws NullPointerException Thrown when element1 is null.
80       * @throws NullPointerException Thrown when element2 is null.
81       * @throws IllegalArgumentException Thrown when element1 or element2 is NaN.
82       */
83      private DoubleRange(final Double number1, final Double number2) {
84          super(number1, number2, null);
85      }
86  
87      /**
88       * Fits the given value into this range by returning the given value or, if out of bounds, the range minimum if
89       * below, or the range maximum if above.
90       *
91       * <pre>{@code
92       * LongRange range = LongRange.of(16, 64);
93       * range.fit(-9) -->  16
94       * range.fit(0)  -->  16
95       * range.fit(15) -->  16
96       * range.fit(16) -->  16
97       * range.fit(17) -->  17
98       * ...
99       * 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 }