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   * @version $Id: PointValuePair.java 1435539 2013-01-19 13:27:24Z tn $
29   * @since 3.0
30   */
31  public class PointValuePair extends Pair<double[], Double> implements Serializable {
32      /** Serializable UID. */
33      private static final long serialVersionUID = 20120513L;
34  
35      /**
36       * Builds a point/objective function value pair.
37       *
38       * @param point Point coordinates. This instance will store
39       * a copy of the array, not the array passed as argument.
40       * @param value Value of the objective function at the point.
41       */
42      public PointValuePair(final double[] point,
43                            final double value) {
44          this(point, value, true);
45      }
46  
47      /**
48       * Builds a point/objective function value pair.
49       *
50       * @param point Point coordinates.
51       * @param value Value of the objective function at the point.
52       * @param copyArray if {@code true}, the input array will be copied,
53       * otherwise it will be referenced.
54       */
55      public PointValuePair(final double[] point,
56                            final double value,
57                            final boolean copyArray) {
58          super(copyArray ? ((point == null) ? null :
59                             point.clone()) :
60                point,
61                value);
62      }
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      /**
75       * Gets a reference to the point.
76       *
77       * @return a reference to the internal array storing the point.
78       */
79      public double[] getPointRef() {
80          return getKey();
81      }
82  
83      /**
84       * Replace the instance with a data transfer object for serialization.
85       * @return data transfer object that will be serialized
86       */
87      private Object writeReplace() {
88          return new DataTransferObject(getKey(), getValue());
89      }
90  
91      /** Internal class used only for serialization. */
92      private static class DataTransferObject implements Serializable {
93          /** Serializable UID. */
94          private static final long serialVersionUID = 20120513L;
95          /**
96           * Point coordinates.
97           * @Serial
98           */
99          private final double[] point;
100         /**
101          * Value of the objective function at the point.
102          * @Serial
103          */
104         private final double value;
105 
106         /** Simple constructor.
107          * @param point Point coordinates.
108          * @param value Value of the objective function at the point.
109          */
110         public DataTransferObject(final double[] point, final double value) {
111             this.point = point.clone();
112             this.value = value;
113         }
114 
115         /** Replace the deserialized data transfer object with a {@link PointValuePair}.
116          * @return replacement {@link PointValuePair}
117          */
118         private Object readResolve() {
119             return new PointValuePair(point, value, false);
120         }
121     }
122 }