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.math4.stat.descriptive.rank;
018
019import java.io.Serializable;
020
021import org.apache.commons.math4.exception.MathIllegalArgumentException;
022import org.apache.commons.math4.exception.NullArgumentException;
023import org.apache.commons.math4.stat.descriptive.AbstractStorelessUnivariateStatistic;
024import org.apache.commons.math4.util.MathArrays;
025import org.apache.commons.math4.util.MathUtils;
026
027/**
028 * Returns the minimum of the available values.
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>
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 */
041public class Min extends AbstractStorelessUnivariateStatistic implements Serializable {
042
043    /** Serializable version identifier */
044    private static final long serialVersionUID = 20150412L;
045
046    /**Number of values that have been added */
047    private long n;
048
049    /**Current value of the statistic */
050    private double value;
051
052    /**
053     * Create a Min instance.
054     */
055    public Min() {
056        n = 0;
057        value = Double.NaN;
058    }
059
060    /**
061     * Copy constructor, creates a new {@code Min} identical
062     * to the {@code original}.
063     *
064     * @param original the {@code Min} instance to copy
065     * @throws NullArgumentException if original is null
066     */
067    public Min(Min original) throws NullArgumentException {
068        copy(original, this);
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public void increment(final double d) {
076        if (d < value || Double.isNaN(value)) {
077            value = d;
078        }
079        n++;
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public void clear() {
087        value = Double.NaN;
088        n = 0;
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public double getResult() {
096        return value;
097    }
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
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     * <ul>
115     * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
116     * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
117     * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
118     * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
119     * </ul>
120     *
121     * @param values the input array
122     * @param begin index of the first array element to include
123     * @param length the number of elements to include
124     * @return the minimum of the values or Double.NaN if length = 0
125     * @throws MathIllegalArgumentException if the array is null or the array index
126     *  parameters are not valid
127     */
128    @Override
129    public double evaluate(final double[] values,final int begin, final int length)
130        throws MathIllegalArgumentException {
131
132        double min = Double.NaN;
133        if (MathArrays.verifyValues(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.n = source.n;
168        dest.value = source.value;
169    }
170}