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.optim;

  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.  * @since 3.0
  27.  */
  28. public class PointVectorValuePair extends Pair<double[], double[]> implements Serializable {
  29.     /** Serializable UID. */
  30.     private static final long serialVersionUID = 20120513L;

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

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

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

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

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

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

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

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

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

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