AbstractStorelessUnivariateStatistic.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.  *      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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive;

  22. import org.hipparchus.util.MathUtils;
  23. import org.hipparchus.util.Precision;

  24. /**
  25.  * Abstract base class for implementations of the
  26.  * {@link StorelessUnivariateStatistic} interface.
  27.  * <p>
  28.  * Provides default {@code hashCode()} and {@code equals(Object)}
  29.  * implementations.
  30.  */
  31. public abstract class AbstractStorelessUnivariateStatistic
  32.     implements StorelessUnivariateStatistic {

  33.     /** Empty constructor.
  34.      * <p>
  35.      * This constructor is not strictly necessary, but it prevents spurious
  36.      * javadoc warnings with JDK 18 and later.
  37.      * </p>
  38.      * @since 3.0
  39.      */
  40.     public AbstractStorelessUnivariateStatistic() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
  41.         // nothing to do
  42.     }

  43.     /** {@inheritDoc} */
  44.     @Override
  45.     public abstract StorelessUnivariateStatistic copy();

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public abstract void clear();

  49.     /** {@inheritDoc} */
  50.     @Override
  51.     public abstract double getResult();

  52.     /** {@inheritDoc} */
  53.     @Override
  54.     public abstract void increment(double d);

  55.     /**
  56.      * Returns true iff <code>object</code> is the same type of
  57.      * {@link StorelessUnivariateStatistic} (the object's class equals this
  58.      * instance) returning the same values as this for <code>getResult()</code>
  59.      * and <code>getN()</code>.
  60.      *
  61.      * @param object object to test equality against.
  62.      * @return true if object returns the same value as this
  63.      */
  64.     @Override
  65.     public boolean equals(Object object) {
  66.         if (object == this ) {
  67.             return true;
  68.         }
  69.         if (object == null || object.getClass() != this.getClass()) {
  70.             return false;
  71.         }
  72.         StorelessUnivariateStatistic other = (StorelessUnivariateStatistic) object;
  73.         return Precision.equalsIncludingNaN(other.getResult(), getResult()) &&
  74.                Precision.equalsIncludingNaN(other.getN(),      getN());
  75.     }

  76.     /**
  77.      * Returns hash code based on getResult() and getN().
  78.      *
  79.      * @return hash code
  80.      */
  81.     @Override
  82.     public int hashCode() {
  83.         return 31 * (31 + MathUtils.hash(getResult())) + MathUtils.hash(getN());
  84.     }

  85.     @Override
  86.     public String toString() {
  87.         return String.format("%s: result=%f, N=%d",
  88.                              getClass().getSimpleName(),
  89.                              getResult(),
  90.                              getN());
  91.     }
  92. }