PointValuePair.java

  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.  *      http://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. package org.apache.commons.math4.legacy.optim;

  18. import java.util.Arrays;

  19. import org.apache.commons.math4.legacy.core.Pair;

  20. /**
  21.  * This class holds a point and the value of an objective function at
  22.  * that point.
  23.  *
  24.  * @see PointVectorValuePair
  25.  * @see org.apache.commons.math4.legacy.analysis.MultivariateFunction
  26.  * @since 3.0
  27.  */
  28. public final class PointValuePair extends Pair<double[], Double> {
  29.     /**
  30.      * Builds a point/objective function value pair.
  31.      *
  32.      * @param point Point coordinates. This instance will store
  33.      * a copy of the array, not the array passed as argument.
  34.      * @param value Value of the objective function at the point.
  35.      */
  36.     public PointValuePair(final double[] point,
  37.                           final double value) {
  38.         this(point, value, true);
  39.     }

  40.     /**
  41.      * Builds a point/objective function value pair.
  42.      *
  43.      * @param point Point coordinates.
  44.      * @param value Value of the objective function at the point.
  45.      * @param copyArray if {@code true}, the input array will be copied,
  46.      * otherwise it will be referenced.
  47.      */
  48.     public PointValuePair(final double[] point,
  49.                           final double value,
  50.                           final boolean copyArray) {
  51.         super(copyArray ? ((point == null) ? null :
  52.                            point.clone()) :
  53.               point,
  54.               value);
  55.     }

  56.     /**
  57.      * Gets the point.
  58.      *
  59.      * @return a copy of the stored point.
  60.      */
  61.     public double[] getPoint() {
  62.         final double[] p = getKey();
  63.         return p == null ? null : p.clone();
  64.     }

  65.     /**
  66.      * Gets a reference to the point.
  67.      *
  68.      * @return a reference to the internal array storing the point.
  69.      */
  70.     public double[] getPointRef() {
  71.         return getKey();
  72.     }

  73.     @Override
  74.     public boolean equals(Object o) {
  75.         if (o instanceof PointValuePair) {
  76.             final PointValuePair other = (PointValuePair) o;

  77.             return getValue().equals(other.getValue()) &&
  78.                 Arrays.equals(getPointRef(),
  79.                               other.getPointRef());
  80.         }

  81.         return false;
  82.     }

  83.     @Override
  84.     public int hashCode() {
  85.         return Arrays.hashCode(getKey()) + 31 * Double.hashCode(getValue());
  86.     }

  87.     @Override
  88.     public String toString() {
  89.         return "[" + Arrays.toString(getPointRef()) + ", " + getValue() + "]";
  90.     }
  91. }