001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.optim;
018
019import java.io.Serializable;
020
021import org.apache.commons.math4.util.Pair;
022
023/**
024 * This class holds a point and the value of an objective function at
025 * that point.
026 *
027 * @see PointVectorValuePair
028 * @see org.apache.commons.math4.analysis.MultivariateFunction
029 * @since 3.0
030 */
031public class PointValuePair extends Pair<double[], Double> implements Serializable {
032    /** Serializable UID. */
033    private static final long serialVersionUID = 20120513L;
034
035    /**
036     * Builds a point/objective function value pair.
037     *
038     * @param point Point coordinates. This instance will store
039     * a copy of the array, not the array passed as argument.
040     * @param value Value of the objective function at the point.
041     */
042    public PointValuePair(final double[] point,
043                          final double value) {
044        this(point, value, true);
045    }
046
047    /**
048     * Builds a point/objective function value pair.
049     *
050     * @param point Point coordinates.
051     * @param value Value of the objective function at the point.
052     * @param copyArray if {@code true}, the input array will be copied,
053     * otherwise it will be referenced.
054     */
055    public PointValuePair(final double[] point,
056                          final double value,
057                          final boolean copyArray) {
058        super(copyArray ? ((point == null) ? null :
059                           point.clone()) :
060              point,
061              value);
062    }
063
064    /**
065     * Gets the point.
066     *
067     * @return a copy of the stored point.
068     */
069    public double[] getPoint() {
070        final double[] p = getKey();
071        return p == null ? null : p.clone();
072    }
073
074    /**
075     * Gets a reference to the point.
076     *
077     * @return a reference to the internal array storing the point.
078     */
079    public double[] getPointRef() {
080        return getKey();
081    }
082
083    /**
084     * Replace the instance with a data transfer object for serialization.
085     * @return data transfer object that will be serialized
086     */
087    private Object writeReplace() {
088        return new DataTransferObject(getKey(), getValue());
089    }
090
091    /** Internal class used only for serialization. */
092    private static class DataTransferObject implements Serializable {
093        /** Serializable UID. */
094        private static final long serialVersionUID = 20120513L;
095        /**
096         * Point coordinates.
097         * @Serial
098         */
099        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        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}