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