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 
18 package org.apache.commons.math3.optimization;
19 
20 import java.io.Serializable;
21 
22 import org.apache.commons.math3.util.Pair;
23 
24 /**
25  * This class holds a point and the value of an objective function at
26  * that point.
27  *
28  * @see PointVectorValuePair
29  * @see org.apache.commons.math3.analysis.MultivariateFunction
30  * @deprecated As of 3.1 (to be removed in 4.0).
31  * @since 3.0
32  */
33 @Deprecated
34 public class PointValuePair extends Pair<double[], Double> implements Serializable {
35 
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      */
PointValuePair(final double[] point, final double value)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      */
PointValuePair(final double[] point, final double value, final boolean copyArray)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      */
getPoint()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      */
getPointRef()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      */
writeReplace()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          */
DataTransferObject(final double[] point, final double value)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          */
readResolve()122         private Object readResolve() {
123             return new PointValuePair(point, value, false);
124         }
125 
126     }
127 
128 }
129