    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
    018    package org.apache.commons.math3.optimization;
    019
    020    import java.io.Serializable;
    021
    022    import org.apache.commons.math3.util.Pair;
    023
    024    /**
    025     * This class holds a point and the vectorial value of an objective function at
    026     * that point.
    027     *
    028     * @see PointValuePair
    029     * @see org.apache.commons.math3.analysis.MultivariateVectorFunction
    030     * @version $Id: PointVectorValuePair.java 1422230 2012-12-15 12:11:13Z erans $
    031     * @deprecated As of 3.1 (to be removed in 4.0).
    032     * @since 3.0
    033     */
    034    @Deprecated
    035    public class PointVectorValuePair extends Pair<double[], double[]> implements Serializable {
    036
    037        /** Serializable UID. */
    038        private static final long serialVersionUID = 20120513L;
    039
    040        /**
    041         * Builds a point/objective function value pair.
    042         *
    043         * @param point Point coordinates. This instance will store
    044         * a copy of the array, not the array passed as argument.
    045         * @param value Value of the objective function at the point.
    046         */
    047        public PointVectorValuePair(final double[] point,
    048                                    final double[] value) {
    049            this(point, value, true);
    050        }
    051
    052        /**
    053         * Build a point/objective function value pair.
    054         *
    055         * @param point Point coordinates.
    056         * @param value Value of the objective function at the point.
    057         * @param copyArray if {@code true}, the input arrays will be copied,
    058         * otherwise they will be referenced.
    059         */
    060        public PointVectorValuePair(final double[] point,
    061                                    final double[] value,
    062                                    final boolean copyArray) {
    063            super(copyArray ?
    064                  ((point == null) ? null :
    065                   point.clone()) :
    066                  point,
    067                  copyArray ?
    068                  ((value == null) ? null :
    069                   value.clone()) :
    070                  value);
    071        }
    072
    073        /**
    074         * Gets the point.
    075         *
    076         * @return a copy of the stored point.
    077         */
    078        public double[] getPoint() {
    079            final double[] p = getKey();
    080            return p == null ? null : p.clone();
    081        }
    082
    083        /**
    084         * Gets a reference to the point.
    085         *
    086         * @return a reference to the internal array storing the point.
    087         */
    088        public double[] getPointRef() {
    089            return getKey();
    090        }
    091
    092        /**
    093         * Gets the value of the objective function.
    094         *
    095         * @return a copy of the stored value of the objective function.
    096         */
    097        @Override
    098        public double[] getValue() {
    099            final double[] v = super.getValue();
    100            return v == null ? null : v.clone();
    101        }
    102
    103        /**
    104         * Gets a reference to the value of the objective function.
    105         *
    106         * @return a reference to the internal array storing the value of
    107         * the objective function.
    108         */
    109        public double[] getValueRef() {
    110            return super.getValue();
    111        }
    112
    113        /**
    114         * Replace the instance with a data transfer object for serialization.
    115         * @return data transfer object that will be serialized
    116         */
    117        private Object writeReplace() {
    118            return new DataTransferObject(getKey(), getValue());
    119        }
    120
    121        /** Internal class used only for serialization. */
    122        private static class DataTransferObject implements Serializable {
    123            /** Serializable UID. */
    124            private static final long serialVersionUID = 20120513L;
    125            /**
    126             * Point coordinates.
    127             * @Serial
    128             */
    129            private final double[] point;
    130            /**
    131             * Value of the objective function at the point.
    132             * @Serial
    133             */
    134            private final double[] value;
    135
    136            /** Simple constructor.
    137             * @param point Point coordinates.
    138             * @param value Value of the objective function at the point.
    139             */
    140            public DataTransferObject(final double[] point, final double[] value) {
    141                this.point = point.clone();
    142                this.value = value.clone();
    143            }
    144
    145            /** Replace the deserialized data transfer object with a {@link PointValuePair}.
    146             * @return replacement {@link PointValuePair}
    147             */
    148            private Object readResolve() {
    149                return new PointVectorValuePair(point, value, false);
    150            }
    151        }
    152    }