View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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    *      https://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  
18  package org.hipparchus.stat.descriptive;
19  
20  import org.hipparchus.exception.NullArgumentException;
21  import org.hipparchus.util.MathUtils;
22  
23  /**
24   * An interface for statistics that can aggregate results.
25   *
26   * @param <T> the type of statistic
27   */
28  public interface AggregatableStatistic<T> {
29  
30      /**
31       * Aggregates the provided instance into this instance.
32       * <p>
33       * This method can be used to combine statistics computed over partitions or
34       * subsamples - i.e., the value of this instance after this operation should
35       * be the same as if a single statistic would have been applied over the
36       * combined dataset.
37       *
38       * @param other the instance to aggregate into this instance
39       * @throws NullArgumentException if the input is null
40       */
41      void aggregate(T other) throws NullArgumentException;
42  
43      /**
44       * Aggregates the results from the provided instances into this instance.
45       * <p>
46       * This method can be used to combine statistics computed over partitions or
47       * subsamples - i.e., the value of this instance after this operation should
48       * be the same as if a single statistic would have been applied over the
49       * combined dataset.
50       *
51       * @param others the other instances to aggregate into this instance
52       * @throws NullArgumentException if either others or any instance is null
53       */
54      @SuppressWarnings("unchecked")
55      default void aggregate(T... others) {
56          MathUtils.checkNotNull(others);
57          for (T other : others) {
58              aggregate(other);
59          }
60      }
61  
62      /**
63       * Aggregates the results from the provided instances into this instance.
64       * <p>
65       * This method can be used to combine statistics computed over partitions or
66       * subsamples - i.e., the value of this instance after this operation should
67       * be the same as if a single statistic would have been applied over the
68       * combined dataset.
69       *
70       * @param others the other instances to aggregate into this instance
71       * @throws NullArgumentException if either others or any instance is null
72       */
73      default void aggregate(Iterable<T> others) {
74          MathUtils.checkNotNull(others);
75          for (T other : others) {
76              aggregate(other);
77          }
78      }
79  
80  }