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.stat.descriptive.rank;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.math3.exception.MathIllegalArgumentException;
022    import org.apache.commons.math3.exception.NullArgumentException;
023    import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic;
024    import org.apache.commons.math3.util.MathUtils;
025    
026    /**
027     * Returns the minimum of the available values.
028     * <p>
029     * <ul>
030     * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
031     * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
032     * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
033     * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
034     * </ul></p>
035     * <p>
036     * <strong>Note that this implementation is not synchronized.</strong> If
037     * multiple threads access an instance of this class concurrently, and at least
038     * one of the threads invokes the <code>increment()</code> or
039     * <code>clear()</code> method, it must be synchronized externally.</p>
040     *
041     * @version $Id: Min.java 1416643 2012-12-03 19:37:14Z tn $
042     */
043    public class Min extends AbstractStorelessUnivariateStatistic implements Serializable {
044    
045        /** Serializable version identifier */
046        private static final long serialVersionUID = -2941995784909003131L;
047    
048        /**Number of values that have been added */
049        private long n;
050    
051        /**Current value of the statistic */
052        private double value;
053    
054        /**
055         * Create a Min instance
056         */
057        public Min() {
058            n = 0;
059            value = Double.NaN;
060        }
061    
062        /**
063         * Copy constructor, creates a new {@code Min} identical
064         * to the {@code original}
065         *
066         * @param original the {@code Min} instance to copy
067         * @throws NullArgumentException if original is null
068         */
069        public Min(Min original) throws NullArgumentException {
070            copy(original, this);
071        }
072    
073        /**
074         * {@inheritDoc}
075         */
076        @Override
077        public void increment(final double d) {
078            if (d < value || Double.isNaN(value)) {
079                value = d;
080            }
081            n++;
082        }
083    
084        /**
085         * {@inheritDoc}
086         */
087        @Override
088        public void clear() {
089            value = Double.NaN;
090            n = 0;
091        }
092    
093        /**
094         * {@inheritDoc}
095         */
096        @Override
097        public double getResult() {
098            return value;
099        }
100    
101        /**
102         * {@inheritDoc}
103         */
104        public long getN() {
105            return n;
106        }
107    
108        /**
109         * Returns the minimum of the entries in the specified portion of
110         * the input array, or <code>Double.NaN</code> if the designated subarray
111         * is empty.
112         * <p>
113         * Throws <code>MathIllegalArgumentException</code> if the array is null or
114         * the array index parameters are not valid.</p>
115         * <p>
116         * <ul>
117         * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
118         * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
119         * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
120         * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
121         * </ul> </p>
122         *
123         * @param values the input array
124         * @param begin index of the first array element to include
125         * @param length the number of elements to include
126         * @return the minimum of the values or Double.NaN if length = 0
127         * @throws MathIllegalArgumentException if the array is null or the array index
128         *  parameters are not valid
129         */
130        @Override
131        public double evaluate(final double[] values,final int begin, final int length)
132        throws MathIllegalArgumentException {
133            double min = Double.NaN;
134            if (test(values, begin, length)) {
135                min = values[begin];
136                for (int i = begin; i < begin + length; i++) {
137                    if (!Double.isNaN(values[i])) {
138                        min = (min < values[i]) ? min : values[i];
139                    }
140                }
141            }
142            return min;
143        }
144    
145        /**
146         * {@inheritDoc}
147         */
148        @Override
149        public Min copy() {
150            Min result = new Min();
151            // No try-catch or advertised exception - args are non-null
152            copy(this, result);
153            return result;
154        }
155    
156        /**
157         * Copies source to dest.
158         * <p>Neither source nor dest can be null.</p>
159         *
160         * @param source Min to copy
161         * @param dest Min to copy to
162         * @throws NullArgumentException if either source or dest is null
163         */
164        public static void copy(Min source, Min dest)
165            throws NullArgumentException {
166            MathUtils.checkNotNull(source);
167            MathUtils.checkNotNull(dest);
168            dest.setData(source.getDataRef());
169            dest.n = source.n;
170            dest.value = source.value;
171        }
172    }