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