All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.evolver.utils.arg.IntArg Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
package fi.evolver.utils.arg;

import java.io.IOException;
import java.io.InputStream;

public class IntArg extends Arg {
	private final Integer minValue;
	private final Integer maxValue;

	private static final String VALUE = "Value";

	public IntArg(String name, Integer minValue, Integer maxValue, Integer defaultValue) {
		super(Integer.class, name, false, defaultValue);
		this.minValue = minValue;
		this.maxValue = maxValue;
	}

	public IntArg(String name, Integer minValue, Integer maxValue) {
		super(Integer.class, name, true, null);
		this.minValue = minValue;
		this.maxValue = maxValue;
	}

	public IntArg(String name, Integer defaultValue) {
		this(name, null, null, defaultValue);
	}

	public IntArg(String name) {
		this(name, null, null);
	}


	public Integer getMinValue() {
		return minValue;
	}

	public Integer getMaxValue() {
		return maxValue;
	}


	@Override
	protected Integer convert(InputStream in) throws IOException {
		String val = readUtf8Value(in).trim();
		if (!val.matches("-?\\d+"))
			throw new IllegalArgumentException(VALUE + " " + val + " is not a valid integer");
		return Integer.parseInt(val);
	}


	@Override
	protected void validate(Integer value) {
		if (minValue != null && value < minValue)
			throw new IllegalArgumentException(VALUE + " " + value + " is too small, must be at least " + minValue);
		if (maxValue != null && value > maxValue)
			throw new IllegalArgumentException(VALUE + " " + value + " is too big, may be at most " + maxValue);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy