LongFrequency.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;

  22. import java.util.Comparator;

  23. /**
  24.  * Maintains a frequency distribution of Long values.
  25.  * <p>
  26.  * Accepts byte, short, int, long primitive or Integer and Long values.
  27.  * <p>
  28.  * Integer values (byte, short, int, long, Integer, Long) are not
  29.  * distinguished by type, i.e. {@code addValue(Long.valueOf(2)),
  30.  * addValue(2), addValue(2L)} all have the same effect (similarly
  31.  * for arguments to {@code getCount()} etc.).
  32.  * <p>
  33.  * NOTE: byte and short values will be implicitly converted to int values
  34.  * by the compiler, thus there are no explicit overloaded methods for these
  35.  * primitive types.
  36.  * <p>
  37.  * The values are ordered using the default (natural order), unless a
  38.  * {@code Comparator} is supplied in the constructor.
  39.  */
  40. public class LongFrequency extends Frequency<Long> {

  41.     /** Serializable version identifier */
  42.     private static final long serialVersionUID = 20160322L;

  43.     /**
  44.      * Default constructor.
  45.      */
  46.     public LongFrequency() {
  47.      // This constructor is intentionally empty. Nothing special is needed here.
  48.     }

  49.     /**
  50.      * Constructor allowing values Comparator to be specified.
  51.      *
  52.      * @param comparator Comparator used to order values
  53.      */
  54.     public LongFrequency(Comparator<? super Long> comparator) {
  55.         super(comparator);
  56.     }

  57.     /**
  58.      * Adds 1 to the frequency count for v.
  59.      *
  60.      * @param v the value to add.
  61.      */
  62.     public void addValue(int v) {
  63.         incrementValue(Long.valueOf(v), 1);
  64.     }

  65.     /**
  66.      * Increments the frequency count for v.
  67.      *
  68.      * @param v the value to add.
  69.      * @param increment the amount by which the value should be incremented
  70.      */
  71.     public void incrementValue(int v, long increment) {
  72.         incrementValue(Long.valueOf(v), increment);
  73.     }

  74.     //-------------------------------------------------------------------------

  75.     /**
  76.      * Returns the number of values equal to v.
  77.      *
  78.      * @param v the value to lookup.
  79.      * @return the frequency of v.
  80.      */
  81.     public long getCount(int v) {
  82.         return getCount(Long.valueOf(v));
  83.     }

  84.     /**
  85.      * Returns the percentage of values that are equal to v
  86.      * (as a proportion between 0 and 1).
  87.      * <p>
  88.      * Returns {@code Double.NaN} if no values have been added.
  89.      *
  90.      * @param v the value to lookup
  91.      * @return the proportion of values equal to v
  92.      */
  93.     public double getPct(int v) {
  94.         return getPct(Long.valueOf(v));
  95.     }

  96.     //-----------------------------------------------------------------------------------------

  97.     /**
  98.      * Returns the cumulative frequency of values less than or equal to v.
  99.      *
  100.      * @param v the value to lookup.
  101.      * @return the proportion of values equal to v
  102.      */
  103.     public long getCumFreq(int v) {
  104.         return getCumFreq(Long.valueOf(v));
  105.     }

  106.     //----------------------------------------------------------------------------------------------

  107.     /**
  108.      * Returns the cumulative percentage of values less than or equal to v
  109.      * (as a proportion between 0 and 1).
  110.      * <p>
  111.      * Returns {@code Double.NaN} if no values have been added.
  112.      *
  113.      * @param v the value to lookup
  114.      * @return the proportion of values less than or equal to v
  115.      */
  116.     public double getCumPct(int v) {
  117.         return getCumPct(Long.valueOf(v));
  118.     }

  119. }