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 vectorial value of an objective function at
024 * that point.
025 *
026 * @see PointValuePair
027 * @see org.apache.commons.math3.analysis.MultivariateVectorFunction
028 * @since 3.0
029 */
030public class PointVectorValuePair 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 PointVectorValuePair(final double[] point,
042                                final double[] value) {
043        this(point, value, true);
044    }
045
046    /**
047     * Build 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 arrays will be copied,
052     * otherwise they will be referenced.
053     */
054    public PointVectorValuePair(final double[] point,
055                                final double[] value,
056                                final boolean copyArray) {
057        super(copyArray ?
058              ((point == null) ? null :
059               point.clone()) :
060              point,
061              copyArray ?
062              ((value == null) ? null :
063               value.clone()) :
064              value);
065    }
066
067    /**
068     * Gets the point.
069     *
070     * @return a copy of the stored point.
071     */
072    public double[] getPoint() {
073        final double[] p = getKey();
074        return p == null ? null : p.clone();
075    }
076
077    /**
078     * Gets a reference to the point.
079     *
080     * @return a reference to the internal array storing the point.
081     */
082    public double[] getPointRef() {
083        return getKey();
084    }
085
086    /**
087     * Gets the value of the objective function.
088     *
089     * @return a copy of the stored value of the objective function.
090     */
091    @Override
092    public double[] getValue() {
093        final double[] v = super.getValue();
094        return v == null ? null : v.clone();
095    }
096
097    /**
098     * Gets a reference to the value of the objective function.
099     *
100     * @return a reference to the internal array storing the value of
101     * the objective function.
102     */
103    public double[] getValueRef() {
104        return super.getValue();
105    }
106
107    /**
108     * Replace the instance with a data transfer object for serialization.
109     * @return data transfer object that will be serialized
110     */
111    private Object writeReplace() {
112        return new DataTransferObject(getKey(), getValue());
113    }
114
115    /** Internal class used only for serialization. */
116    private static class DataTransferObject implements Serializable {
117        /** Serializable UID. */
118        private static final long serialVersionUID = 20120513L;
119        /**
120         * Point coordinates.
121         * @Serial
122         */
123        private final double[] point;
124        /**
125         * Value of the objective function at the point.
126         * @Serial
127         */
128        private final double[] value;
129
130        /** Simple constructor.
131         * @param point Point coordinates.
132         * @param value Value of the objective function at the point.
133         */
134        public DataTransferObject(final double[] point, final double[] value) {
135            this.point = point.clone();
136            this.value = value.clone();
137        }
138
139        /** Replace the deserialized data transfer object with a {@link PointValuePair}.
140         * @return replacement {@link PointValuePair}
141         */
142        private Object readResolve() {
143            return new PointVectorValuePair(point, value, false);
144        }
145    }
146}