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 value of an objective function at
26   * that point.
27   *
28   * @see PointVectorValuePair
29   * @see org.apache.commons.math3.analysis.MultivariateFunction
30   * @version $Id: PointValuePair.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 PointValuePair 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 PointValuePair(final double[] point,
48                            final double value) {
49          this(point, value, true);
50      }
51  
52      /**
53       * Builds 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 array will be copied,
58       * otherwise it will be referenced.
59       */
60      public PointValuePair(final double[] point,
61                            final double value,
62                            final boolean copyArray) {
63          super(copyArray ? ((point == null) ? null :
64                             point.clone()) :
65                point,
66                value);
67      }
68  
69      /**
70       * Gets the point.
71       *
72       * @return a copy of the stored point.
73       */
74      public double[] getPoint() {
75          final double[] p = getKey();
76          return p == null ? null : p.clone();
77      }
78  
79      /**
80       * Gets a reference to the point.
81       *
82       * @return a reference to the internal array storing the point.
83       */
84      public double[] getPointRef() {
85          return getKey();
86      }
87  
88      /**
89       * Replace the instance with a data transfer object for serialization.
90       * @return data transfer object that will be serialized
91       */
92      private Object writeReplace() {
93          return new DataTransferObject(getKey(), getValue());
94      }
95  
96      /** Internal class used only for serialization. */
97      private static class DataTransferObject implements Serializable {
98          /** Serializable UID. */
99          private static final long serialVersionUID = 20120513L;
100         /**
101          * Point coordinates.
102          * @Serial
103          */
104         private final double[] point;
105         /**
106          * Value of the objective function at the point.
107          * @Serial
108          */
109         private final double value;
110 
111         /** Simple constructor.
112          * @param point Point coordinates.
113          * @param value Value of the objective function at the point.
114          */
115         public DataTransferObject(final double[] point, final double value) {
116             this.point = point.clone();
117             this.value = value;
118         }
119 
120         /** Replace the deserialized data transfer object with a {@link PointValuePair}.
121          * @return replacement {@link PointValuePair}
122          */
123         private Object readResolve() {
124             return new PointValuePair(point, value, false);
125         }
126 
127     }
128 
129 }