AggregatableStatistic.java

  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. package org.hipparchus.stat.descriptive;

  18. import org.hipparchus.exception.NullArgumentException;
  19. import org.hipparchus.util.MathUtils;

  20. /**
  21.  * An interface for statistics that can aggregate results.
  22.  *
  23.  * @param <T> the type of statistic
  24.  */
  25. public interface AggregatableStatistic<T> {

  26.     /**
  27.      * Aggregates the provided instance into this instance.
  28.      * <p>
  29.      * This method can be used to combine statistics computed over partitions or
  30.      * subsamples - i.e., the value of this instance after this operation should
  31.      * be the same as if a single statistic would have been applied over the
  32.      * combined dataset.
  33.      *
  34.      * @param other the instance to aggregate into this instance
  35.      * @throws NullArgumentException if the input is null
  36.      */
  37.     void aggregate(T other) throws NullArgumentException;

  38.     /**
  39.      * Aggregates the results from the provided instances into this instance.
  40.      * <p>
  41.      * This method can be used to combine statistics computed over partitions or
  42.      * subsamples - i.e., the value of this instance after this operation should
  43.      * be the same as if a single statistic would have been applied over the
  44.      * combined dataset.
  45.      *
  46.      * @param others the other instances to aggregate into this instance
  47.      * @throws NullArgumentException if either others or any instance is null
  48.      */
  49.     @SuppressWarnings("unchecked")
  50.     default void aggregate(T... others) {
  51.         MathUtils.checkNotNull(others);
  52.         for (T other : others) {
  53.             aggregate(other);
  54.         }
  55.     }

  56.     /**
  57.      * Aggregates the results from the provided instances into this instance.
  58.      * <p>
  59.      * This method can be used to combine statistics computed over partitions or
  60.      * subsamples - i.e., the value of this instance after this operation should
  61.      * be the same as if a single statistic would have been applied over the
  62.      * combined dataset.
  63.      *
  64.      * @param others the other instances to aggregate into this instance
  65.      * @throws NullArgumentException if either others or any instance is null
  66.      */
  67.     default void aggregate(Iterable<T> others) {
  68.         MathUtils.checkNotNull(others);
  69.         for (T other : others) {
  70.             aggregate(other);
  71.         }
  72.     }

  73. }