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

fi.evolver.utils.arg.LongArg 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 LongArg extends Arg {
	private final Long minValue;
	private final Long maxValue;

	private static final String VALUE = "Value";

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

	public LongArg(String name, Long defaultValue) {
		this(name, null, null, defaultValue);
	}

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

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


	public Long getMinValue() {
		return minValue;
	}

	public Long getMaxValue() {
		return maxValue;
	}


	@Override
	protected Long 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 Long.parseLong(val);
	}


	@Override
	protected void validate(Long 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