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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim;

  22. import java.io.Serializable;

  23. import org.hipparchus.util.Pair;

  24. /**
  25.  * This class holds a point and the value of an objective function at
  26.  * that point.
  27.  *
  28.  * @see PointVectorValuePair
  29.  * @see org.hipparchus.analysis.MultivariateFunction
  30.  */
  31. public class PointValuePair extends Pair<double[], Double> implements Serializable {
  32.     /** Serializable UID. */
  33.     private static final long serialVersionUID = 20120513L;

  34.     /**
  35.      * Builds a point/objective function value pair.
  36.      *
  37.      * @param point Point coordinates. This instance will store
  38.      * a copy of the array, not the array passed as argument.
  39.      * @param value Value of the objective function at the point.
  40.      */
  41.     public PointValuePair(final double[] point,
  42.                           final double value) {
  43.         this(point, value, true);
  44.     }

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

  61.     /**
  62.      * Gets the point.
  63.      *
  64.      * @return a copy of the stored point.
  65.      */
  66.     public double[] getPoint() {
  67.         final double[] p = getKey();
  68.         return p == null ? null : p.clone();
  69.     }

  70.     /**
  71.      * Gets a reference to the point.
  72.      *
  73.      * @return a reference to the internal array storing the point.
  74.      */
  75.     public double[] getPointRef() {
  76.         return getKey();
  77.     }

  78.     /**
  79.      * Replace the instance with a data transfer object for serialization.
  80.      * @return data transfer object that will be serialized
  81.      */
  82.     private Object writeReplace() {
  83.         return new DataTransferObject(getKey(), getValue());
  84.     }

  85.     /** Internal class used only for serialization. */
  86.     private static class DataTransferObject implements Serializable {
  87.         /** Serializable UID. */
  88.         private static final long serialVersionUID = 20120513L;
  89.         /**
  90.          * Point coordinates.
  91.          * @Serial
  92.          */
  93.         private final double[] point;
  94.         /**
  95.          * Value of the objective function at the point.
  96.          * @Serial
  97.          */
  98.         private final double value;

  99.         /** Simple constructor.
  100.          * @param point Point coordinates.
  101.          * @param value Value of the objective function at the point.
  102.          */
  103.         DataTransferObject(final double[] point, final double value) {
  104.             this.point = point.clone();
  105.             this.value = value;
  106.         }

  107.         /** Replace the deserialized data transfer object with a {@link PointValuePair}.
  108.          * @return replacement {@link PointValuePair}
  109.          */
  110.         private Object readResolve() {
  111.             return new PointValuePair(point, value, false);
  112.         }
  113.     }
  114. }