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;
020import java.util.function.DoubleSupplier;
021import java.util.function.IntSupplier;
022import java.util.function.LongSupplier;
023
024/**
025 * Represents the result of a statistic computed over a set of values.
026 *
027 * <p>Base interface implemented by all statistics.
028 *
029 * @since 1.1
030 */
031@FunctionalInterface
032public interface StatisticResult extends DoubleSupplier, IntSupplier, LongSupplier {
033    /**
034     * {@inheritDoc}
035     *
036     * <p>The default implementation uses the closest representable {@code int} value of
037     * the {@link #getAsDouble()} {@code result}. In the event of ties the result is
038     * rounded towards positive infinity. This will raise an {@link ArithmeticException}
039     * if the closest integer result is not within the range {@code [-2^31, 2^31)}.
040     *
041     * @throws ArithmeticException if the {@code result} overflows an {@code int} or is not
042     * finite
043     */
044    @Override
045    default int getAsInt() {
046        return IntMath.toIntExact(getAsDouble());
047    }
048
049    /**
050     * {@inheritDoc}
051     *
052     * <p>The default implementation uses the closest representable {@code long} value of
053     * the {@link #getAsDouble()} {@code result}. In the event of ties the result is
054     * rounded towards positive infinity. This will raise an {@link ArithmeticException}
055     * if the closest integer result is not within the range {@code [-2^63, 2^63)}.
056     *
057     * @throws ArithmeticException if the {@code result} overflows a {@code long} or is not
058     * finite
059     */
060    @Override
061    default long getAsLong() {
062        return IntMath.toLongExact(getAsDouble());
063    }
064
065    /**
066     * Gets a result as a {@link BigInteger}.
067     *
068     * <p>The default implementation uses the closest representable {@code BigInteger}
069     * value of the {@link #getAsDouble()} {@code result}. In the event of ties the result
070     * is rounded towards positive infinity. This will raise an
071     * {@link ArithmeticException} if the {@code result} is not finite.
072     *
073     * @return a result
074     * @throws ArithmeticException if the {@code result} is not finite
075     */
076    default BigInteger getAsBigInteger() {
077        return IntMath.toBigIntegerExact(getAsDouble());
078    }
079}