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  
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 vectorial value of an objective function at
26   * that point.
27   *
28   * @see PointValuePair
29   * @see org.apache.commons.math3.analysis.MultivariateVectorFunction
30   * @version $Id: PointVectorValuePair.java 1422230 2012-12-15 12:11:13Z erans $
31   * @deprecated As of 3.1 (to be removed in 4.0).
32   * @since 3.0
33   */
34  @Deprecated
35  public class PointVectorValuePair extends Pair<double[], double[]> implements Serializable {
36  
37      /** Serializable UID. */
38      private static final long serialVersionUID = 20120513L;
39  
40      /**
41       * Builds a point/objective function value pair.
42       *
43       * @param point Point coordinates. This instance will store
44       * a copy of the array, not the array passed as argument.
45       * @param value Value of the objective function at the point.
46       */
47      public PointVectorValuePair(final double[] point,
48                                  final double[] value) {
49          this(point, value, true);
50      }
51  
52      /**
53       * Build a point/objective function value pair.
54       *
55       * @param point Point coordinates.
56       * @param value Value of the objective function at the point.
57       * @param copyArray if {@code true}, the input arrays will be copied,
58       * otherwise they will be referenced.
59       */
60      public PointVectorValuePair(final double[] point,
61                                  final double[] value,
62                                  final boolean copyArray) {
63          super(copyArray ?
64                ((point == null) ? null :
65                 point.clone()) :
66                point,
67                copyArray ?
68                ((value == null) ? null :
69                 value.clone()) :
70                value);
71      }
72  
73      /**
74       * Gets the point.
75       *
76       * @return a copy of the stored point.
77       */
78      public double[] getPoint() {
79          final double[] p = getKey();
80          return p == null ? null : p.clone();
81      }
82  
83      /**
84       * Gets a reference to the point.
85       *
86       * @return a reference to the internal array storing the point.
87       */
88      public double[] getPointRef() {
89          return getKey();
90      }
91  
92      /**
93       * Gets the value of the objective function.
94       *
95       * @return a copy of the stored value of the objective function.
96       */
97      @Override
98      public double[] getValue() {
99          final double[] v = super.getValue();
100         return v == null ? null : v.clone();
101     }
102 
103     /**
104      * Gets a reference to the value of the objective function.
105      *
106      * @return a reference to the internal array storing the value of
107      * the objective function.
108      */
109     public double[] getValueRef() {
110         return super.getValue();
111     }
112 
113     /**
114      * Replace the instance with a data transfer object for serialization.
115      * @return data transfer object that will be serialized
116      */
117     private Object writeReplace() {
118         return new DataTransferObject(getKey(), getValue());
119     }
120 
121     /** Internal class used only for serialization. */
122     private static class DataTransferObject implements Serializable {
123         /** Serializable UID. */
124         private static final long serialVersionUID = 20120513L;
125         /**
126          * Point coordinates.
127          * @Serial
128          */
129         private final double[] point;
130         /**
131          * Value of the objective function at the point.
132          * @Serial
133          */
134         private final double[] value;
135 
136         /** Simple constructor.
137          * @param point Point coordinates.
138          * @param value Value of the objective function at the point.
139          */
140         public DataTransferObject(final double[] point, final double[] value) {
141             this.point = point.clone();
142             this.value = value.clone();
143         }
144 
145         /** Replace the deserialized data transfer object with a {@link PointValuePair}.
146          * @return replacement {@link PointValuePair}
147          */
148         private Object readResolve() {
149             return new PointVectorValuePair(point, value, false);
150         }
151     }
152 }