Product.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. /**
  19.  * Returns the product of the available values.
  20.  *
  21.  * <ul>
  22.  *   <li>The result is one if no values are observed.
  23.  *   <li>The result is {@code NaN} if any of the values is {@code NaN}.
  24.  * </ul>
  25.  *
  26.  * <p>This class is designed to work with (though does not require)
  27.  * {@linkplain java.util.stream streams}.
  28.  *
  29.  * <p><strong>This instance is not thread safe.</strong>
  30.  * If multiple threads access an instance of this class concurrently,
  31.  * and at least one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
  32.  * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
  33.  *
  34.  * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
  35.  * and {@link StatisticAccumulator#combine(StatisticResult) combine}
  36.  * as {@code accumulator} and {@code combiner} functions of
  37.  * {@link java.util.stream.Collector Collector} on a parallel stream,
  38.  * because the parallel instance of {@link java.util.stream.Stream#collect Stream.collect()}
  39.  * provides the necessary partitioning, isolation, and merging of results for
  40.  * safe and efficient parallel execution.
  41.  *
  42.  * @since 1.1
  43.  */
  44. public final class Product implements DoubleStatistic, StatisticAccumulator<Product> {

  45.     /** Product of all values. */
  46.     private double productValue = 1;

  47.     /**
  48.      * Create an instance.
  49.      */
  50.     private Product() {
  51.         // No-op
  52.     }

  53.     /**
  54.      * Creates an instance.
  55.      *
  56.      * <p>The initial result is one.
  57.      *
  58.      * @return {@code Product} instance.
  59.      */
  60.     public static Product create() {
  61.         return new Product();
  62.     }

  63.     /**
  64.      * Returns an instance populated using the input {@code values}.
  65.      *
  66.      * <p>The result is {@code NaN} if any of the values is {@code NaN}
  67.      * or the product at any point is a {@code NaN}.
  68.      *
  69.      * <p>When the input is an empty array, the result is one.
  70.      *
  71.      * @param values Values.
  72.      * @return {@code Product} instance.
  73.      */
  74.     public static Product of(double... values) {
  75.         return Statistics.add(new Product(), values);
  76.     }

  77.     /**
  78.      * Returns an instance populated using the input {@code values}.
  79.      *
  80.      * <p>When the input is an empty array, the result is one.
  81.      *
  82.      * @param values Values.
  83.      * @return {@code Product} instance.
  84.      */
  85.     public static Product of(int... values) {
  86.         return Statistics.add(new Product(), values);
  87.     }

  88.     /**
  89.      * Returns an instance populated using the input {@code values}.
  90.      *
  91.      * <p>When the input is an empty array, the result is one.
  92.      *
  93.      * @param values Values.
  94.      * @return {@code Product} instance.
  95.      */
  96.     public static Product of(long... values) {
  97.         return Statistics.add(new Product(), values);
  98.     }

  99.     /**
  100.      * Updates the state of the statistic to reflect the addition of {@code value}.
  101.      *
  102.      * @param value Value.
  103.      */
  104.     @Override
  105.     public void accept(double value) {
  106.         this.productValue *= value;
  107.     }

  108.     /**
  109.      * Gets the product of all input values.
  110.      *
  111.      * <p>When no values have been added, the result is one.
  112.      *
  113.      * @return product of all values.
  114.      */
  115.     @Override
  116.     public double getAsDouble() {
  117.         return productValue;
  118.     }

  119.     @Override
  120.     public Product combine(Product other) {
  121.         productValue *= other.productValue;
  122.         return this;
  123.     }
  124. }