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 *      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 */
017package org.apache.commons.math3.geometry.euclidean.oned;
018
019import org.apache.commons.math3.geometry.partitioning.Region.Location;
020import org.apache.commons.math3.exception.NumberIsTooSmallException;
021import org.apache.commons.math3.exception.util.LocalizedFormats;
022
023
024/** This class represents a 1D interval.
025 * @see IntervalsSet
026 * @since 3.0
027 */
028public class Interval {
029
030    /** The lower bound of the interval. */
031    private final double lower;
032
033    /** The upper bound of the interval. */
034    private final double upper;
035
036    /** Simple constructor.
037     * @param lower lower bound of the interval
038     * @param upper upper bound of the interval
039     */
040    public Interval(final double lower, final double upper) {
041        if (upper < lower) {
042            throw new NumberIsTooSmallException(LocalizedFormats.ENDPOINTS_NOT_AN_INTERVAL,
043                                                upper, lower, true);
044        }
045        this.lower = lower;
046        this.upper = upper;
047    }
048
049    /** Get the lower bound of the interval.
050     * @return lower bound of the interval
051     * @since 3.1
052     */
053    public double getInf() {
054        return lower;
055    }
056
057    /** Get the lower bound of the interval.
058     * @return lower bound of the interval
059     * @deprecated as of 3.1, replaced by {@link #getInf()}
060     */
061    @Deprecated
062    public double getLower() {
063        return getInf();
064    }
065
066    /** Get the upper bound of the interval.
067     * @return upper bound of the interval
068     * @since 3.1
069     */
070    public double getSup() {
071        return upper;
072    }
073
074    /** Get the upper bound of the interval.
075     * @return upper bound of the interval
076     * @deprecated as of 3.1, replaced by {@link #getSup()}
077     */
078    @Deprecated
079    public double getUpper() {
080        return getSup();
081    }
082
083    /** Get the size of the interval.
084     * @return size of the interval
085     * @since 3.1
086     */
087    public double getSize() {
088        return upper - lower;
089    }
090
091    /** Get the length of the interval.
092     * @return length of the interval
093     * @deprecated as of 3.1, replaced by {@link #getSize()}
094     */
095    @Deprecated
096    public double getLength() {
097        return getSize();
098    }
099
100    /** Get the barycenter of the interval.
101     * @return barycenter of the interval
102     * @since 3.1
103     */
104    public double getBarycenter() {
105        return 0.5 * (lower + upper);
106    }
107
108    /** Get the midpoint of the interval.
109     * @return midpoint of the interval
110     * @deprecated as of 3.1, replaced by {@link #getBarycenter()}
111     */
112    @Deprecated
113    public double getMidPoint() {
114        return getBarycenter();
115    }
116
117    /** Check a point with respect to the interval.
118     * @param point point to check
119     * @param tolerance tolerance below which points are considered to
120     * belong to the boundary
121     * @return a code representing the point status: either {@link
122     * Location#INSIDE}, {@link Location#OUTSIDE} or {@link Location#BOUNDARY}
123     * @since 3.1
124     */
125    public Location checkPoint(final double point, final double tolerance) {
126        if (point < lower - tolerance || point > upper + tolerance) {
127            return Location.OUTSIDE;
128        } else if (point > lower + tolerance && point < upper - tolerance) {
129            return Location.INSIDE;
130        } else {
131            return Location.BOUNDARY;
132        }
133    }
134
135}