forked from spotify/semantic-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPercentile.java
130 lines (114 loc) · 3.91 KB
/
Percentile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2016 Spotify AB.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.spotify.metrics.ffwd;
import com.google.common.base.Strings;
public class Percentile {
private final int whole;
private final int decimal;
private final String percentileString;
private final double quantile;
/**
* Construct a percentile based on the whole part and the decimal part of a number. e.g.,
* whole=99, decimal=8 => p99.8
*
* @param whole a value in range [0..100) that represents a percentile, e.g., 75 means p75
* @param decimal a value in range [0..9] that represents the decimal digit of the percentile
*/
public Percentile(int whole, int decimal) {
if (whole < 0 || whole >= 100) {
throw new IllegalArgumentException("whole " + whole + " is not in [0..100)");
}
if (decimal < 0 || decimal > 9) {
throw new IllegalArgumentException("decimal " + decimal + " is not in [0..9]");
}
this.whole = whole;
this.decimal = decimal;
percentileString = toPercentileString(this.whole, this.decimal);
quantile = toQuantile(this.whole, this.decimal);
}
/**
* @param whole a value in range [0..100) that represents a percentile, e.g., 75 means p75
*/
public Percentile(int whole) {
this(whole, 0);
}
/**
* @param quantile a value in range [0..1) that represents a percentile, e.g., 0.75 means p75.
* Note that it will be rounded to have 3 decimal digits, e.g., 0.7558 -> 0.756
*/
public Percentile(double quantile) {
// Here is what is happening:
// p = quantile * 100;
// int w = (int) p;
// int d = (int)Math.round((p - w) * 10);
// this(w, d);
this((int) (quantile * 100),
(int) Math.round(((quantile * 100) - (int) (quantile * 100)) * 10));
}
private String toPercentileString(int whole, int decimal) {
String pString = "p" + whole;
if (decimal != 0) {
return pString + "." + decimal;
}
return pString;
}
private double toQuantile(int whole, int decimal) {
return Double.parseDouble("0." + Strings.padStart("" + whole, 2, '0') + decimal);
}
public double getQuantile() {
return quantile;
}
public String getPercentileString() {
return percentileString;
}
@Override
public String toString() {
return percentileString;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + decimal;
result = prime * result + whole;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Percentile other = (Percentile) obj;
if (decimal != other.decimal) {
return false;
}
if (whole != other.whole) {
return false;
}
return true;
}
}