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.math4.legacy.optim;
18
19 import java.util.Arrays;
20
21 import org.apache.commons.math4.legacy.core.Pair;
22
23 /**
24 * This class holds a point and the value of an objective function at
25 * that point.
26 *
27 * @see PointVectorValuePair
28 * @see org.apache.commons.math4.legacy.analysis.MultivariateFunction
29 * @since 3.0
30 */
31 public final class PointValuePair extends Pair<double[], Double> {
32 /**
33 * Builds a point/objective function value pair.
34 *
35 * @param point Point coordinates. This instance will store
36 * a copy of the array, not the array passed as argument.
37 * @param value Value of the objective function at the point.
38 */
39 public PointValuePair(final double[] point,
40 final double value) {
41 this(point, value, true);
42 }
43
44 /**
45 * Builds a point/objective function value pair.
46 *
47 * @param point Point coordinates.
48 * @param value Value of the objective function at the point.
49 * @param copyArray if {@code true}, the input array will be copied,
50 * otherwise it will be referenced.
51 */
52 public PointValuePair(final double[] point,
53 final double value,
54 final boolean copyArray) {
55 super(copyArray ? ((point == null) ? null :
56 point.clone()) :
57 point,
58 value);
59 }
60
61 /**
62 * Gets the point.
63 *
64 * @return a copy of the stored point.
65 */
66 public double[] getPoint() {
67 final double[] p = getKey();
68 return p == null ? null : p.clone();
69 }
70
71 /**
72 * Gets a reference to the point.
73 *
74 * @return a reference to the internal array storing the point.
75 */
76 public double[] getPointRef() {
77 return getKey();
78 }
79
80 @Override
81 public boolean equals(Object o) {
82 if (o instanceof PointValuePair) {
83 final PointValuePair other = (PointValuePair) o;
84
85 return getValue().equals(other.getValue()) &&
86 Arrays.equals(getPointRef(),
87 other.getPointRef());
88 }
89
90 return false;
91 }
92
93 @Override
94 public int hashCode() {
95 return Arrays.hashCode(getKey()) + 31 * Double.hashCode(getValue());
96 }
97
98 @Override
99 public String toString() {
100 return "[" + Arrays.toString(getPointRef()) + ", " + getValue() + "]";
101 }
102 }