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.math3.optim;
018
019import java.io.Serializable;
020import org.apache.commons.math3.util.Pair;
021
022/**
023 * This class holds a point and the value of an objective function at
024 * that point.
025 *
026 * @see PointVectorValuePair
027 * @see org.apache.commons.math3.analysis.MultivariateFunction
028 * @since 3.0
029 */
030public class PointValuePair extends Pair<double[], Double> implements Serializable {
031    /** Serializable UID. */
032    private static final long serialVersionUID = 20120513L;
033
034    /**
035     * Builds a point/objective function value pair.
036     *
037     * @param point Point coordinates. This instance will store
038     * a copy of the array, not the array passed as argument.
039     * @param value Value of the objective function at the point.
040     */
041    public PointValuePair(final double[] point,
042                          final double value) {
043        this(point, value, true);
044    }
045
046    /**
047     * Builds a point/objective function value pair.
048     *
049     * @param point Point coordinates.
050     * @param value Value of the objective function at the point.
051     * @param copyArray if {@code true}, the input array will be copied,
052     * otherwise it will be referenced.
053     */
054    public PointValuePair(final double[] point,
055                          final double value,
056                          final boolean copyArray) {
057        super(copyArray ? ((point == null) ? null :
058                           point.clone()) :
059              point,
060              value);
061    }
062
063    /**
064     * Gets the point.
065     *
066     * @return a copy of the stored point.
067     */
068    public double[] getPoint() {
069        final double[] p = getKey();
070        return p == null ? null : p.clone();
071    }
072
073    /**
074     * Gets a reference to the point.
075     *
076     * @return a reference to the internal array storing the point.
077     */
078    public double[] getPointRef() {
079        return getKey();
080    }
081
082    /**
083     * Replace the instance with a data transfer object for serialization.
084     * @return data transfer object that will be serialized
085     */
086    private Object writeReplace() {
087        return new DataTransferObject(getKey(), getValue());
088    }
089
090    /** Internal class used only for serialization. */
091    private static class DataTransferObject implements Serializable {
092        /** Serializable UID. */
093        private static final long serialVersionUID = 20120513L;
094        /**
095         * Point coordinates.
096         * @Serial
097         */
098        private final double[] point;
099        /**
100         * Value of the objective function at the point.
101         * @Serial
102         */
103        private final double value;
104
105        /** Simple constructor.
106         * @param point Point coordinates.
107         * @param value Value of the objective function at the point.
108         */
109        DataTransferObject(final double[] point, final double value) {
110            this.point = point.clone();
111            this.value = value;
112        }
113
114        /** Replace the deserialized data transfer object with a {@link PointValuePair}.
115         * @return replacement {@link PointValuePair}
116         */
117        private Object readResolve() {
118            return new PointValuePair(point, value, false);
119        }
120    }
121}