View Javadoc
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;
23  
24  import java.util.Comparator;
25  
26  /**
27   * Maintains a frequency distribution of Long values.
28   * <p>
29   * Accepts byte, short, int, long primitive or Integer and Long values.
30   * <p>
31   * Integer values (byte, short, int, long, Integer, Long) are not
32   * distinguished by type, i.e. {@code addValue(Long.valueOf(2)),
33   * addValue(2), addValue(2L)} all have the same effect (similarly
34   * for arguments to {@code getCount()} etc.).
35   * <p>
36   * NOTE: byte and short values will be implicitly converted to int values
37   * by the compiler, thus there are no explicit overloaded methods for these
38   * primitive types.
39   * <p>
40   * The values are ordered using the default (natural order), unless a
41   * {@code Comparator} is supplied in the constructor.
42   */
43  public class LongFrequency extends Frequency<Long> {
44  
45      /** Serializable version identifier */
46      private static final long serialVersionUID = 20160322L;
47  
48      /**
49       * Default constructor.
50       */
51      public LongFrequency() {
52       // This constructor is intentionally empty. Nothing special is needed here.
53      }
54  
55      /**
56       * Constructor allowing values Comparator to be specified.
57       *
58       * @param comparator Comparator used to order values
59       */
60      public LongFrequency(Comparator<? super Long> comparator) {
61          super(comparator);
62      }
63  
64      /**
65       * Adds 1 to the frequency count for v.
66       *
67       * @param v the value to add.
68       */
69      public void addValue(int v) {
70          incrementValue(Long.valueOf(v), 1);
71      }
72  
73      /**
74       * Increments the frequency count for v.
75       *
76       * @param v the value to add.
77       * @param increment the amount by which the value should be incremented
78       */
79      public void incrementValue(int v, long increment) {
80          incrementValue(Long.valueOf(v), increment);
81      }
82  
83      //-------------------------------------------------------------------------
84  
85      /**
86       * Returns the number of values equal to v.
87       *
88       * @param v the value to lookup.
89       * @return the frequency of v.
90       */
91      public long getCount(int v) {
92          return getCount(Long.valueOf(v));
93      }
94  
95      /**
96       * Returns the percentage of values that are equal to v
97       * (as a proportion between 0 and 1).
98       * <p>
99       * Returns {@code Double.NaN} if no values have been added.
100      *
101      * @param v the value to lookup
102      * @return the proportion of values equal to v
103      */
104     public double getPct(int v) {
105         return getPct(Long.valueOf(v));
106     }
107 
108     //-----------------------------------------------------------------------------------------
109 
110     /**
111      * Returns the cumulative frequency of values less than or equal to v.
112      *
113      * @param v the value to lookup.
114      * @return the proportion of values equal to v
115      */
116     public long getCumFreq(int v) {
117         return getCumFreq(Long.valueOf(v));
118     }
119 
120     //----------------------------------------------------------------------------------------------
121 
122     /**
123      * Returns the cumulative percentage of values less than or equal to v
124      * (as a proportion between 0 and 1).
125      * <p>
126      * Returns {@code Double.NaN} if no values have been added.
127      *
128      * @param v the value to lookup
129      * @return the proportion of values less than or equal to v
130      */
131     public double getCumPct(int v) {
132         return getCumPct(Long.valueOf(v));
133     }
134 
135 }