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