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.statistics.descriptive;
018
019import java.math.BigInteger;
020
021/**
022 * Returns the minimum of the available values. Uses {@link Math#min(long, long) Math.min} as an
023 * underlying function to compute the {@code minimum}.
024 *
025 * <ul>
026 *   <li>The result is {@link Long#MAX_VALUE} if no values are added.
027 * </ul>
028 *
029 * <p>This class is designed to work with (though does not require)
030 * {@linkplain java.util.stream streams}.
031 *
032 * <p><strong>This implementation is not thread safe.</strong>
033 * If multiple threads access an instance of this class concurrently,
034 * and at least one of the threads invokes the {@link java.util.function.LongConsumer#accept(long) accept} or
035 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
036 *
037 * <p>However, it is safe to use {@link java.util.function.LongConsumer#accept(long) accept}
038 * and {@link StatisticAccumulator#combine(StatisticResult) combine}
039 * as {@code accumulator} and {@code combiner} functions of
040 * {@link java.util.stream.Collector Collector} on a parallel stream,
041 * because the parallel implementation of {@link java.util.stream.Stream#collect Stream.collect()}
042 * provides the necessary partitioning, isolation, and merging of results for
043 * safe and efficient parallel execution.
044 *
045 * @since 1.1
046 * @see Math#min(long, long)
047 */
048public final class LongMin implements LongStatistic, StatisticAccumulator<LongMin> {
049
050    /** Current minimum. */
051    private long minimum = Long.MAX_VALUE;
052
053    /**
054     * Create an instance.
055     */
056    private LongMin() {
057        // No-op
058    }
059
060    /**
061     * Creates an instance.
062     *
063     * <p>The initial result is {@link Long#MAX_VALUE}.
064     *
065     * @return {@code Min} instance.
066     */
067    public static LongMin create() {
068        return new LongMin();
069    }
070
071    /**
072     * Returns an instance populated using the input {@code values}.
073     *
074     * <p>When the input is an empty array, the result is
075     * {@link Long#MAX_VALUE}.
076     *
077     * @param values Values.
078     * @return {@code Min} instance.
079     */
080    public static LongMin of(long... values) {
081        return Statistics.add(new LongMin(), values);
082    }
083
084    /**
085     * Updates the state of the statistic to reflect the addition of {@code value}.
086     *
087     * @param value Value.
088     */
089    @Override
090    public void accept(long value) {
091        minimum = Math.min(minimum, value);
092    }
093
094    /**
095     * Gets the minimum of all input values.
096     *
097     * <p>When no values have been added, the result is
098     * {@link Long#MAX_VALUE}.
099     *
100     * @return minimum of all values.
101     */
102    @Override
103    public long getAsLong() {
104        return minimum;
105    }
106
107    /**
108     * Gets the minimum of all input values.
109     *
110     * <p>This method will throw an {@link ArithmeticException} if the {@code long}
111     * minimum overflows an {@code int}; or no values have been added.
112     *
113     * @return minimum of all values.
114     * @see Math#toIntExact(long)
115     */
116    @Override
117    public int getAsInt() {
118        return Math.toIntExact(minimum);
119    }
120
121    @Override
122    public double getAsDouble() {
123        return minimum;
124    }
125
126    @Override
127    public BigInteger getAsBigInteger() {
128        return BigInteger.valueOf(minimum);
129    }
130
131    @Override
132    public LongMin combine(LongMin other) {
133        accept(other.getAsLong());
134        return this;
135    }
136}