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 */
48 public static DoubleRange of(final double fromInclusive, final double toInclusive) {
49 return of(Double.valueOf(fromInclusive), Double.valueOf(toInclusive));
50 }
51
52 /**
53 * Creates a range with the specified minimum and maximum values (both inclusive).
54 *
55 * <p>
56 * The range uses the natural ordering of the elements to determine where values lie in the range.
57 * </p>
58 *
59 * <p>
60 * The arguments may be passed in the order (min,max) or (max,min). The getMinimum and getMaximum methods will return the correct values.
61 * </p>
62 *
63 * @param fromInclusive the first value that defines the edge of the range, inclusive.
64 * @param toInclusive the second value that defines the edge of the range, inclusive.
65 * @return the range object, not null.
66 * @throws IllegalArgumentException if either element is null.
67 */
68 public static DoubleRange of(final Double fromInclusive, final Double toInclusive) {
69 return new DoubleRange(fromInclusive, toInclusive);
70 }
71
72 /**
73 * Creates an instance.
74 *
75 * @param number1 the first element, not null
76 * @param number2 the second element, not null
77 * @throws NullPointerException when element1 is null.
78 * @throws NullPointerException when element2 is null.
79 */
80 private DoubleRange(final Double number1, final Double number2) {
81 super(number1, number2, null);
82 }
83
84 /**
85 * Fits the given value into this range by returning the given value or, if out of bounds, the range minimum if
86 * below, or the range maximum if above.
87 *
88 * <pre>{@code
89 * LongRange range = LongRange.of(16, 64);
90 * range.fit(-9) --> 16
91 * range.fit(0) --> 16
92 * range.fit(15) --> 16
93 * range.fit(16) --> 16
94 * range.fit(17) --> 17
95 * ...
96 * range.fit(63) --> 63
97 * range.fit(64) --> 64
98 * range.fit(99) --> 64
99 * }</pre>
100 *
101 * @param element the element to test.
102 * @return the minimum, the element, or the maximum depending on the element's location relative to the range.
103 * @since 3.19.0
104 */
105 public double fit(final double element) {
106 return super.fit(element).doubleValue();
107 }
108
109 }