StatisticResult.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.statistics.descriptive;

  18. import java.math.BigInteger;
  19. import java.util.function.DoubleSupplier;
  20. import java.util.function.IntSupplier;
  21. import java.util.function.LongSupplier;

  22. /**
  23.  * Represents the result of a statistic computed over a set of values.
  24.  *
  25.  * <p>Base interface implemented by all statistics.
  26.  *
  27.  * @since 1.1
  28.  */
  29. @FunctionalInterface
  30. public interface StatisticResult extends DoubleSupplier, IntSupplier, LongSupplier {
  31.     /**
  32.      * {@inheritDoc}
  33.      *
  34.      * <p>The default implementation uses the closest representable {@code int} value of
  35.      * the {@link #getAsDouble()} {@code result}. In the event of ties the result is
  36.      * rounded towards positive infinity. This will raise an {@link ArithmeticException}
  37.      * if the closest integer result is not within the range {@code [-2^31, 2^31)}.
  38.      *
  39.      * @throws ArithmeticException if the {@code result} overflows an {@code int} or is not
  40.      * finite
  41.      */
  42.     @Override
  43.     default int getAsInt() {
  44.         return IntMath.toIntExact(getAsDouble());
  45.     }

  46.     /**
  47.      * {@inheritDoc}
  48.      *
  49.      * <p>The default implementation uses the closest representable {@code long} value of
  50.      * the {@link #getAsDouble()} {@code result}. In the event of ties the result is
  51.      * rounded towards positive infinity. This will raise an {@link ArithmeticException}
  52.      * if the closest integer result is not within the range {@code [-2^63, 2^63)}.
  53.      *
  54.      * @throws ArithmeticException if the {@code result} overflows a {@code long} or is not
  55.      * finite
  56.      */
  57.     @Override
  58.     default long getAsLong() {
  59.         return IntMath.toLongExact(getAsDouble());
  60.     }

  61.     /**
  62.      * Gets a result as a {@link BigInteger}.
  63.      *
  64.      * <p>The default implementation uses the closest representable {@code BigInteger}
  65.      * value of the {@link #getAsDouble()} {@code result}. In the event of ties the result
  66.      * is rounded towards positive infinity. This will raise an
  67.      * {@link ArithmeticException} if the {@code result} is not finite.
  68.      *
  69.      * @return a result
  70.      * @throws ArithmeticException if the {@code result} is not finite
  71.      */
  72.     default BigInteger getAsBigInteger() {
  73.         return IntMath.toBigIntegerExact(getAsDouble());
  74.     }
  75. }