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

  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.  * @since 3.0
  27.  */
  28. public class PointValuePair 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 PointValuePair(final double[] point,
  39.                           final double value) {
  40.         this(point, value, true);
  41.     }

  42.     /**
  43.      * Builds 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 array will be copied,
  48.      * otherwise it will be referenced.
  49.      */
  50.     public PointValuePair(final double[] point,
  51.                           final double value,
  52.                           final boolean copyArray) {
  53.         super(copyArray ? ((point == null) ? null :
  54.                            point.clone()) :
  55.               point,
  56.               value);
  57.     }

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

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

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

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

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

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