/** * Give category for specific value. If N is not included in any known category gives * "OutOfBounds(N)". * * @param n The value to get category for. * @return Matchine category for N. */ public String categoryFor(Number n) { for (NumberCategory category : categories) { String value = category.valueFor(n); if (value != null) return value; } return "OutOfBounds(" + n + ")"; }
/** * Check if a new category is valid in itself and relation to existing ones. * * <p>TODO: try closed categories vs open. Also if null is ever returned? * * @param newCat */ private void check(NumberCategory newCat) { if (newCat.min.doubleValue() > newCat.max.doubleValue()) { throw new IllegalArgumentException("Category min cannot be larger than max:" + newCat); } for (NumberCategory oldCat : categories) { if (newCat.valueFor(newCat.min) != null && oldCat.valueFor(newCat.min) != null) { throw new IllegalArgumentException("Overlapping categories: " + newCat + " and " + oldCat); } if (newCat.valueFor(newCat.max) != null && oldCat.valueFor(newCat.max) != null) { throw new IllegalArgumentException("Overlapping categories: " + newCat + " and " + oldCat); } } }