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;
018
019import java.util.Arrays;
020
021import org.apache.commons.math4.legacy.core.Pair;
022
023/**
024 * This class holds a point and the value of an objective function at
025 * that point.
026 *
027 * @see PointVectorValuePair
028 * @see org.apache.commons.math4.legacy.analysis.MultivariateFunction
029 * @since 3.0
030 */
031public final class PointValuePair extends Pair<double[], Double> {
032    /**
033     * Builds a point/objective function value pair.
034     *
035     * @param point Point coordinates. This instance will store
036     * a copy of the array, not the array passed as argument.
037     * @param value Value of the objective function at the point.
038     */
039    public PointValuePair(final double[] point,
040                          final double value) {
041        this(point, value, true);
042    }
043
044    /**
045     * Builds a point/objective function value pair.
046     *
047     * @param point Point coordinates.
048     * @param value Value of the objective function at the point.
049     * @param copyArray if {@code true}, the input array will be copied,
050     * otherwise it will be referenced.
051     */
052    public PointValuePair(final double[] point,
053                          final double value,
054                          final boolean copyArray) {
055        super(copyArray ? ((point == null) ? null :
056                           point.clone()) :
057              point,
058              value);
059    }
060
061    /**
062     * Gets the point.
063     *
064     * @return a copy of the stored point.
065     */
066    public double[] getPoint() {
067        final double[] p = getKey();
068        return p == null ? null : p.clone();
069    }
070
071    /**
072     * Gets a reference to the point.
073     *
074     * @return a reference to the internal array storing the point.
075     */
076    public double[] getPointRef() {
077        return getKey();
078    }
079
080    @Override
081    public boolean equals(Object o) {
082        if (o instanceof PointValuePair) {
083            final PointValuePair other = (PointValuePair) o;
084
085            return getValue().equals(other.getValue()) &&
086                Arrays.equals(getPointRef(),
087                              other.getPointRef());
088        }
089
090        return false;
091    }
092
093    @Override
094    public int hashCode() {
095        return Arrays.hashCode(getKey()) + 31 * Double.hashCode(getValue());
096    }
097
098    @Override
099    public String toString() {
100        return "[" + Arrays.toString(getPointRef()) + ", " + getValue() + "]";
101    }
102}