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

name.valery1707.validator.russian.inn.InnInfo Maven / Gradle / Ivy

package name.valery1707.validator.russian.inn;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

@SuppressWarnings("WeakerAccess")
public class InnInfo {
	/**
	 * Код субъекта Российской Федерации согласно ст. 65 Конституции
	 */
	private final byte subject;

	/**
	 * Номер местной налоговой инспекции
	 */
	private final byte localTax;

	/**
	 * Номер налоговой записи налогоплательщика/
	 */
	private final int id;

	/**
	 * Юридическое Лицо
	 */
	private final boolean juridical;

	private String crc;

	public InnInfo(int subject, int localTax, int id, boolean juridical) {
		this((byte) subject, (byte) localTax, id, juridical);
	}

	public InnInfo(byte subject, byte localTax, int id, boolean juridical) {
		if (subject < 0 || subject > 99) {
			throw new IllegalArgumentException("Subject must be between 1 and 99");
		}
		if (localTax < 0 || localTax > 99) {
			throw new IllegalArgumentException("Local tax must be between 1 and 99");
		}
		if (id < 0 || ((juridical && id > 99999) || id > 999999)) {
			throw new IllegalArgumentException("Local tax must be between 1 and 99");
		}
		this.subject = subject;
		this.localTax = localTax;
		this.id = id;
		this.juridical = juridical;
	}

	@Nonnull
	public static InnInfo parse(@Nullable String value) {
		if (InnValidator.isValid(value).nonValid()) {
			throw new IllegalArgumentException("Can not parse INN from string: " + value);
		}
		boolean juridical = value.length() == 10;
		byte subject = Byte.valueOf(value.substring(0, 2));
		byte localTax = Byte.valueOf(value.substring(2, 4));
		int id = Integer.valueOf(value.substring(4, value.length() - (juridical ? 1 : 2)));
		return new InnInfo(subject, localTax, id, juridical);
	}

	public String format() {
		String s = String.format(
				isJuridical()
						? "%02d%02d%05d"
						: "%02d%02d%06d"
				, getSubject(), getLocalTax(), getId());
		if (crc == null) {
			crc = String.valueOf(InnValidator.calcCrc(s));
			if (isPhysical()) {
				crc += String.valueOf(InnValidator.calcCrc(s + crc));
			}
		}
		return s + crc;
	}

	public byte getSubject() {
		return subject;
	}

	public byte getLocalTax() {
		return localTax;
	}

	public int getId() {
		return id;
	}

	public boolean isJuridical() {
		return juridical;
	}

	public boolean isPhysical() {
		return !isJuridical();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof InnInfo)) {
			return false;
		}

		InnInfo that = (InnInfo) o;
		return
				this.getSubject() == that.getSubject() &&
				this.getLocalTax() == that.getLocalTax() &&
				this.getId() == that.getId() &&
				this.isJuridical() == that.isJuridical()
				;
	}

	@Override
	public int hashCode() {
		int result = (int) getSubject();
		result = 31 * result + (int) getLocalTax();
		result = 31 * result + getId();
		result = 31 * result + (isJuridical() ? 1 : 0);
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy