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