/** * @see java.text.DecimalFormat */ public String getPattern(){ return getPattern(-1); }
public int maxScale(){ return maxScale(-1); }
public int minScale(){ return minScale(-1); }
static public DecimalFormat getFormat(Parameter<?, ?> parameter){ if(parameter.hasCargo(ValuesCargo.class)){ ValuesCargo valuesCargo = parameter.getCargo(ValuesCargo.class); try { Map<String, BigDecimal> values = parseValues(valuesCargo.loadStringMap()); ScaleFrequencyMap map = ScaleFrequencyMap.sample(values.values()); int minCount = Math.max(3, values.size() / 10); return new DecimalFormat(map.getPattern(minCount), new DecimalFormatSymbols(Locale.US)); } catch(Exception e){ logger.log(Level.WARNING, "Parameter \'" + parameter.getId() + "\' does not specify decimal format", e); } } return null; }
static public ScaleFrequencyMap sample(Collection<?> values){ ScaleFrequencyMap result = new ScaleFrequencyMap(); for(Object value : values){ if(value == null){ continue; } result.add(BigDecimalFormat.toBigDecimal(value)); } return result; } }
/** * @see java.text.DecimalFormat */ public String getPattern(int minCount){ int min = minScale(minCount); int max = maxScale(minCount); StringBuffer sb = new StringBuffer(); sb.append('0'); int diff = (max - min); if(min > 0 || diff > 0){ sb.append('.'); max = Math.min(min + 4, max); for(int i = 1; true; i++){ if(i <= min){ sb.append('0'); } else if(i <= max){ sb.append('#'); } else { break; } } } return sb.toString(); }
public int maxScale(int minCount){ Set<Integer> scales = getScales(minCount); if(scales.isEmpty()){ return 0; } Integer scale = Collections.max(scales); return scale.intValue(); }
public int minScale(int minCount){ Set<Integer> scales = getScales(minCount); if(scales.isEmpty()){ return 0; } Integer scale = Collections.min(scales); return scale.intValue(); }