1 package com.stateofflow.eclipse.metrics.export.html.histogram;
2
3 import com.stateofflow.eclipse.metrics.util.CounterMap;
4
5 class Category implements Comparable<Category> {
6 private final int x;
7 private final int preferredUpperBound;
8
9 public Category(final int x, final int preferredUpperBound) {
10 this.preferredUpperBound = preferredUpperBound;
11 this.x = Math.min(x, getMaxX());
12 }
13
14 public int compareTo(final Category that) {
15 return x - (that).x;
16 }
17
18 @Override
19 public boolean equals(final Object obj) {
20 if (obj == this) {
21 return true;
22 }
23
24 if (obj == null || !obj.getClass().equals(getClass())) {
25 return false;
26 }
27
28 final Category that = (Category) obj;
29 return x == that.x;
30 }
31
32 public int getInRangeValue(final CounterMap<Category> categoryToYValueMap) {
33 return getRangeValue(true, categoryToYValueMap);
34 }
35
36 private int getMaxX() {
37 return preferredUpperBound * 2;
38 }
39
40 public int getOutOfRangeValue(final CounterMap<Category> categoryToYValueMap) {
41 return getRangeValue(false, categoryToYValueMap);
42 }
43
44 private int getRangeValue(final boolean inRange, final CounterMap<Category> categoryToYValueMap) {
45 return isInRange() == inRange ? categoryToYValueMap.get(this) : 0;
46 }
47
48 @Override
49 public int hashCode() {
50 return x;
51 }
52
53 private boolean isInRange() {
54 return x <= preferredUpperBound;
55 }
56
57 public Category predecessor() {
58 return new Category(x - 1, preferredUpperBound);
59 }
60
61 @Override
62 public String toString() {
63 return x < getMaxX() ? Integer.toString(x) : ">=" + getMaxX();
64 }
65 }
66