class Settings {
private static final int VALUE = 0;
private static final int MAX_VALUE = 1;
private static final int MIN_VALUE = 2;
private final Map<String, int[]> settings;
public Settings() {
settings = new HashMap<>();
}
public void insert(String parameterName, int value, int maxValue, int minValue) {
if (settings.containsKey(parameterName)) {
throw new IllegalArgumentException("Parameter name already exists");
}
checkValueRange(value, minValue, maxValue);
settings.put(parameterName, new int[] { value, maxValue, minValue });
}
public void setValue(String parameterName, int value) {
checkSettingExistence(parameterName);
int[] values = settings.get(parameterName);
checkValueRange(value, values[MIN_VALUE], values[MAX_VALUE]);
values[VALUE] = value;
}
public int getValue(String parameterName) {
checkSettingExistence(parameterName);
int[] values = settings.get(parameterName);
return values[VALUE];
}
public void printAllSettingsSorted() {
Collection<String> sortedParamterNames = new TreeSet<>(settings.keySet());
for (String parameterName : sortedParamterNames) {
int[] values = settings.get(parameterName);
System.out.format("%s = %d [%d, %d]%n", parameterName, values[VALUE], values[MIN_VALUE], values[MAX_VALUE]);
}
}
private void checkValueRange(int value, int minValue, int maxValue) {
if (value < minValue || maxValue < value) {
throw new IllegalArgumentException("Value exceeds limits");
}
}
private void checkSettingExistence(String parameterName) {
if (!settings.containsKey(parameterName)) {
throw new NoSuchElementException("No setting found with parameter name: " + parameterName);
}
}
}