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.math3.optimization;

  18. import java.io.Serializable;

  19. import org.apache.commons.math3.util.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.math3.analysis.MultivariateFunction
  26.  * @deprecated As of 3.1 (to be removed in 4.0).
  27.  * @since 3.0
  28.  */
  29. @Deprecated
  30. public class PointValuePair extends Pair<double[], Double> implements Serializable {

  31.     /** Serializable UID. */
  32.     private static final long serialVersionUID = 20120513L;

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

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

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

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

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

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

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

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

  112.     }

  113. }