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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22 package org.hipparchus.stat.descriptive;
23
24 import org.hipparchus.util.MathUtils;
25 import org.hipparchus.util.Precision;
26
27 /**
28 * Abstract base class for implementations of the
29 * {@link StorelessUnivariateStatistic} interface.
30 * <p>
31 * Provides default {@code hashCode()} and {@code equals(Object)}
32 * implementations.
33 */
34 public abstract class AbstractStorelessUnivariateStatistic
35 implements StorelessUnivariateStatistic {
36
37 /** Empty constructor.
38 * <p>
39 * This constructor is not strictly necessary, but it prevents spurious
40 * javadoc warnings with JDK 18 and later.
41 * </p>
42 * @since 3.0
43 */
44 public AbstractStorelessUnivariateStatistic() { // NOPMD - unnecessary constructor added intentionally to make javadoc happy
45 // nothing to do
46 }
47
48 /** {@inheritDoc} */
49 @Override
50 public abstract StorelessUnivariateStatistic copy();
51
52 /** {@inheritDoc} */
53 @Override
54 public abstract void clear();
55
56 /** {@inheritDoc} */
57 @Override
58 public abstract double getResult();
59
60 /** {@inheritDoc} */
61 @Override
62 public abstract void increment(double d);
63
64 /**
65 * Returns true iff <code>object</code> is the same type of
66 * {@link StorelessUnivariateStatistic} (the object's class equals this
67 * instance) returning the same values as this for <code>getResult()</code>
68 * and <code>getN()</code>.
69 *
70 * @param object object to test equality against.
71 * @return true if object returns the same value as this
72 */
73 @Override
74 public boolean equals(Object object) {
75 if (object == this ) {
76 return true;
77 }
78 if (object == null || object.getClass() != this.getClass()) {
79 return false;
80 }
81 StorelessUnivariateStatistic other = (StorelessUnivariateStatistic) object;
82 return Precision.equalsIncludingNaN(other.getResult(), getResult()) &&
83 Precision.equalsIncludingNaN(other.getN(), getN());
84 }
85
86 /**
87 * Returns hash code based on getResult() and getN().
88 *
89 * @return hash code
90 */
91 @Override
92 public int hashCode() {
93 return 31 * (31 + MathUtils.hash(getResult())) + MathUtils.hash(getN());
94 }
95
96 @Override
97 public String toString() {
98 return String.format("%s: result=%f, N=%d",
99 getClass().getSimpleName(),
100 getResult(),
101 getN());
102 }
103 }