PointVectorValuePair.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 vectorial value of an objective function at
  22.  * that point.
  23.  *
  24.  * @see PointValuePair
  25.  * @see org.apache.commons.math3.analysis.MultivariateVectorFunction
  26.  * @deprecated As of 3.1 (to be removed in 4.0).
  27.  * @since 3.0
  28.  */
  29. @Deprecated
  30. public class PointVectorValuePair 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 PointVectorValuePair(final double[] point,
  41.                                 final double[] value) {
  42.         this(point, value, true);
  43.     }

  44.     /**
  45.      * Build 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 arrays will be copied,
  50.      * otherwise they will be referenced.
  51.      */
  52.     public PointVectorValuePair(final double[] point,
  53.                                 final double[] value,
  54.                                 final boolean copyArray) {
  55.         super(copyArray ?
  56.               ((point == null) ? null :
  57.                point.clone()) :
  58.               point,
  59.               copyArray ?
  60.               ((value == null) ? null :
  61.                value.clone()) :
  62.               value);
  63.     }

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

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

  81.     /**
  82.      * Gets the value of the objective function.
  83.      *
  84.      * @return a copy of the stored value of the objective function.
  85.      */
  86.     @Override
  87.     public double[] getValue() {
  88.         final double[] v = super.getValue();
  89.         return v == null ? null : v.clone();
  90.     }

  91.     /**
  92.      * Gets a reference to the value of the objective function.
  93.      *
  94.      * @return a reference to the internal array storing the value of
  95.      * the objective function.
  96.      */
  97.     public double[] getValueRef() {
  98.         return super.getValue();
  99.     }

  100.     /**
  101.      * Replace the instance with a data transfer object for serialization.
  102.      * @return data transfer object that will be serialized
  103.      */
  104.     private Object writeReplace() {
  105.         return new DataTransferObject(getKey(), getValue());
  106.     }

  107.     /** Internal class used only for serialization. */
  108.     private static class DataTransferObject implements Serializable {
  109.         /** Serializable UID. */
  110.         private static final long serialVersionUID = 20120513L;
  111.         /**
  112.          * Point coordinates.
  113.          * @Serial
  114.          */
  115.         private final double[] point;
  116.         /**
  117.          * Value of the objective function at the point.
  118.          * @Serial
  119.          */
  120.         private final double[] value;

  121.         /** Simple constructor.
  122.          * @param point Point coordinates.
  123.          * @param value Value of the objective function at the point.
  124.          */
  125.         public DataTransferObject(final double[] point, final double[] value) {
  126.             this.point = point.clone();
  127.             this.value = value.clone();
  128.         }

  129.         /** Replace the deserialized data transfer object with a {@link PointValuePair}.
  130.          * @return replacement {@link PointValuePair}
  131.          */
  132.         private Object readResolve() {
  133.             return new PointVectorValuePair(point, value, false);
  134.         }
  135.     }
  136. }