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
19 import java.math.BigInteger;
20 import java.util.function.DoubleSupplier;
21 import java.util.function.IntSupplier;
22 import java.util.function.LongSupplier;
23
24 /**
25 * Represents the result of a statistic computed over a set of values.
26 *
27 * <p>Base interface implemented by all statistics.
28 *
29 * @since 1.1
30 */
31 @FunctionalInterface
32 public interface StatisticResult extends DoubleSupplier, IntSupplier, LongSupplier {
33 /**
34 * {@inheritDoc}
35 *
36 * <p>The default implementation uses the closest representable {@code int} value of
37 * the {@link #getAsDouble()} {@code result}. In the event of ties the result is
38 * rounded towards positive infinity. This will raise an {@link ArithmeticException}
39 * if the closest integer result is not within the range {@code [-2^31, 2^31)}.
40 *
41 * @throws ArithmeticException if the {@code result} overflows an {@code int} or is not
42 * finite
43 */
44 @Override
45 default int getAsInt() {
46 return IntMath.toIntExact(getAsDouble());
47 }
48
49 /**
50 * {@inheritDoc}
51 *
52 * <p>The default implementation uses the closest representable {@code long} value of
53 * the {@link #getAsDouble()} {@code result}. In the event of ties the result is
54 * rounded towards positive infinity. This will raise an {@link ArithmeticException}
55 * if the closest integer result is not within the range {@code [-2^63, 2^63)}.
56 *
57 * @throws ArithmeticException if the {@code result} overflows a {@code long} or is not
58 * finite
59 */
60 @Override
61 default long getAsLong() {
62 return IntMath.toLongExact(getAsDouble());
63 }
64
65 /**
66 * Gets a result as a {@link BigInteger}.
67 *
68 * <p>The default implementation uses the closest representable {@code BigInteger}
69 * value of the {@link #getAsDouble()} {@code result}. In the event of ties the result
70 * is rounded towards positive infinity. This will raise an
71 * {@link ArithmeticException} if the {@code result} is not finite.
72 *
73 * @return a result
74 * @throws ArithmeticException if the {@code result} is not finite
75 */
76 default BigInteger getAsBigInteger() {
77 return IntMath.toBigIntegerExact(getAsDouble());
78 }
79 }