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.math4.legacy.optim.univariate;
018
019import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
020import org.apache.commons.math4.legacy.exception.OutOfRangeException;
021import org.apache.commons.math4.legacy.optim.OptimizationData;
022
023/**
024 * Search interval and (optional) start value.
025 * <br>
026 * Immutable class.
027 *
028 * @since 3.1
029 */
030public class SearchInterval implements OptimizationData {
031    /** Lower bound. */
032    private final double lower;
033    /** Upper bound. */
034    private final double upper;
035    /** Start value. */
036    private final double start;
037
038    /**
039     * @param lo Lower bound.
040     * @param hi Upper bound.
041     * @param init Start value.
042     * @throws NumberIsTooLargeException if {@code lo >= hi}.
043     * @throws OutOfRangeException if {@code init < lo} or {@code init > hi}.
044     */
045    public SearchInterval(double lo,
046                          double hi,
047                          double init) {
048        if (lo >= hi) {
049            throw new NumberIsTooLargeException(lo, hi, false);
050        }
051        if (init < lo ||
052            init > hi) {
053            throw new OutOfRangeException(init, lo, hi);
054        }
055
056        lower = lo;
057        upper = hi;
058        start = init;
059    }
060
061    /**
062     * @param lo Lower bound.
063     * @param hi Upper bound.
064     * @throws NumberIsTooLargeException if {@code lo >= hi}.
065     */
066    public SearchInterval(double lo,
067                          double hi) {
068        this(lo, hi, 0.5 * (lo + hi));
069    }
070
071    /**
072     * Gets the lower bound.
073     *
074     * @return the lower bound.
075     */
076    public double getMin() {
077        return lower;
078    }
079    /**
080     * Gets the upper bound.
081     *
082     * @return the upper bound.
083     */
084    public double getMax() {
085        return upper;
086    }
087    /**
088     * Gets the start value.
089     *
090     * @return the start value.
091     */
092    public double getStartValue() {
093        return start;
094    }
095}