    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     */
    048    public static DoubleRange of(final double fromInclusive, final double toInclusive) {
    049        return of(Double.valueOf(fromInclusive), Double.valueOf(toInclusive));
    050    }
    051
    052    /**
    053     * Creates a range with the specified minimum and maximum values (both inclusive).
    054     *
    055     * <p>
    056     * The range uses the natural ordering of the elements to determine where values lie in the range.
    057     * </p>
    058     *
    059     * <p>
    060     * The arguments may be passed in the order (min,max) or (max,min). The getMinimum and getMaximum methods will return the correct values.
    061     * </p>
    062     *
    063     * @param fromInclusive the first value that defines the edge of the range, inclusive.
    064     * @param toInclusive the second value that defines the edge of the range, inclusive.
    065     * @return the range object, not null.
    066     * @throws IllegalArgumentException if either element is null.
    067     */
    068    public static DoubleRange of(final Double fromInclusive, final Double toInclusive) {
    069        return new DoubleRange(fromInclusive, toInclusive);
    070    }
    071
    072    /**
    073     * Creates an instance.
    074     *
    075     * @param number1 the first element, not null
    076     * @param number2 the second element, not null
    077     * @throws NullPointerException when element1 is null.
    078     * @throws NullPointerException when element2 is null.
    079     */
    080    private DoubleRange(final Double number1, final Double number2) {
    081        super(number1, number2, null);
    082    }
    083
    084    /**
    085     * Fits the given value into this range by returning the given value or, if out of bounds, the range minimum if
    086     * below, or the range maximum if above.
    087     *
    088     * <pre>{@code
    089     * LongRange range = LongRange.of(16, 64);
    090     * range.fit(-9) -->  16
    091     * range.fit(0)  -->  16
    092     * range.fit(15) -->  16
    093     * range.fit(16) -->  16
    094     * range.fit(17) -->  17
    095     * ...
    096     * range.fit(63) -->  63
    097     * range.fit(64) -->  64
    098     * range.fit(99) -->  64
    099     * }</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}