View Javadoc
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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.optim;
23  
24  import java.io.Serializable;
25  
26  import org.hipparchus.util.Pair;
27  
28  /**
29   * This class holds a point and the value of an objective function at
30   * that point.
31   *
32   * @see PointVectorValuePair
33   * @see org.hipparchus.analysis.MultivariateFunction
34   */
35  public class PointValuePair extends Pair<double[], Double> implements Serializable {
36      /** Serializable UID. */
37      private static final long serialVersionUID = 20120513L;
38  
39      /**
40       * Builds a point/objective function value pair.
41       *
42       * @param point Point coordinates. This instance will store
43       * a copy of the array, not the array passed as argument.
44       * @param value Value of the objective function at the point.
45       */
46      public PointValuePair(final double[] point,
47                            final double value) {
48          this(point, value, true);
49      }
50  
51      /**
52       * Builds a point/objective function value pair.
53       *
54       * @param point Point coordinates.
55       * @param value Value of the objective function at the point.
56       * @param copyArray if {@code true}, the input array will be copied,
57       * otherwise it will be referenced.
58       */
59      public PointValuePair(final double[] point,
60                            final double value,
61                            final boolean copyArray) {
62          super(copyArray ? ((point == null) ? null :
63                             point.clone()) :
64                point,
65                value);
66      }
67  
68      /**
69       * Gets the point.
70       *
71       * @return a copy of the stored point.
72       */
73      public double[] getPoint() {
74          final double[] p = getKey();
75          return p == null ? null : p.clone();
76      }
77  
78      /**
79       * Gets a reference to the point.
80       *
81       * @return a reference to the internal array storing the point.
82       */
83      public double[] getPointRef() {
84          return getKey();
85      }
86  
87      /**
88       * Replace the instance with a data transfer object for serialization.
89       * @return data transfer object that will be serialized
90       */
91      private Object writeReplace() {
92          return new DataTransferObject(getKey(), getValue());
93      }
94  
95      /** Internal class used only for serialization. */
96      private static class DataTransferObject implements Serializable {
97          /** Serializable UID. */
98          private static final long serialVersionUID = 20120513L;
99          /**
100          * Point coordinates.
101          * @Serial
102          */
103         private final double[] point;
104         /**
105          * Value of the objective function at the point.
106          * @Serial
107          */
108         private final double value;
109 
110         /** Simple constructor.
111          * @param point Point coordinates.
112          * @param value Value of the objective function at the point.
113          */
114         DataTransferObject(final double[] point, final double value) {
115             this.point = point.clone();
116             this.value = value;
117         }
118 
119         /** Replace the deserialized data transfer object with a {@link PointValuePair}.
120          * @return replacement {@link PointValuePair}
121          */
122         private Object readResolve() {
123             return new PointValuePair(point, value, false);
124         }
125     }
126 }