private int positionByTwoDivision(int[] array, int begin, int end, int value) {
int mid = (begin + end) >> 1;
int midValue = array[mid];
int halfMidValue = midValue >> 1;
//判断是否可以命中mid
if (value > halfMidValue && value <= midValue) {
return mid;
}
//没法命中,则根据大小来定
if (value <= halfMidValue) {
if (mid - 1 < 0) {//没路可走的边界条件
return 0;
}
return positionByTwoDivision(array, begin, mid - 1, value);
} else {
return positionByTwoDivision(array, mid + 1, end, value);
}
}
public int positionInValueArray(int val) {
int length = SCALE.length;
//如果大于最大值|小于等于最小值
if (val >= SCALE[length - 1]) {
return length - 1;
} else if (val <= SCALE[0]) {
return 0;
}
//采用2分法来计算
return positionByTwoDivision(SCALE, 0, length - 1, val);
}
public void accumulate(Object value) {
//转换为long值,int值够用了
Long longValue = (Long) value;
int intValue = longValue.intValue();
//找到下标
int index = positionInValueArray(intValue);
countContainer[index]++;
}
//确保在[1,MAX]范围内,
//自然顺序
private int adjust(int input, int max) {
if (input <= 1) {
return 1;
} else if (input >= max) {
return max;
} else {
return input;
}
}
private static final ThreadLocal<StringBuilder> STR_BUILDER_ThreadLocal = new ThreadLocal<StringBuilder>() {
public StringBuilder initialValue() {
return new StringBuilder();
}
};
private static final String SEPARATOR = ":";
public String getValue() {
//total
int total = 0;
int length = countContainer.length;
for (int index = 0; index < length; index++) {
total += countContainer[index];
}
//如果total为0的异常情况
//注意是自然序---[1,total]
int percent_9999_pos = adjust((int) (total * PERCENT_9999), total);
boolean found_9999 = false;
int percent_9999_value = Integer.MAX_VALUE;
//999
int percent_999_pos = adjust((int) (total * PERCENT_999), total);
boolean found_999 = false;
int percent_999_value = Integer.MAX_VALUE;
//99
int percent_99_pos = adjust((int) (total * PERCENT_99), total);
boolean found_99 = false;
int percent_99_value = Integer.MAX_VALUE;
//95
int percent_95_pos = adjust((int) (total * PERCENT_95), total);
boolean found_95 = false;
int percent_95_value = Integer.MAX_VALUE;
//90
int percent_90_pos = adjust((int) (total * PERCENT_90), total);
boolean found_90 = false;
int percent_90_value = Integer.MAX_VALUE;
//75
int percent_75_pos = adjust((int) (total * PERCENT_75), total);
boolean found_75 = false;
int percent_75_value = Integer.MAX_VALUE;
//50
int percent_50_pos = adjust((int) (total * PERCENT_50), total);
boolean found_50 = false;
int percent_50_value = Integer.MAX_VALUE;