package net.morilib.nina;

public class VersionNumber {

	private int[] numbers;

	/**
	 * 
	 * @param s
	 */
	public VersionNumber(String s) {
		int[] l = new int[5], a;
		int k, m, p = 0;

		for(k = m = 0; k < s.length(); k++) {
			if(s.charAt(k) == '.') {
				l[p++] = Integer.parseInt(s.substring(m, k));
				m = k + 1;
				if(p >= l.length) {
					a = new int[l.length * 2];
					System.arraycopy(l, 0, a, 0, l.length);
					l = a;
				}
			}
		}
		l[p++] = Integer.parseInt(s.substring(m, k));
		numbers = new int[p];
		System.arraycopy(l, 0, numbers, 0, p);
	}

	/**
	 * 
	 * @return
	 */
	public int getDepth() {
		return numbers.length;
	}

	/**
	 * 
	 * @param a
	 * @return
	 */
	public boolean isUpperThan(VersionNumber a) {
		int k;

		for(k = 0; k < numbers.length; k++) {
			if(k >= a.numbers.length) {
				return true;  // 1.2.1 > 1.2
			} else if(numbers[k] > a.numbers[k]) {
				return true;  // 1.2 > 1.1
			} else if(numbers[k] < a.numbers[k]) {
				return false;  // 1.2 < 1.3
			}
		}
		return k >= a.numbers.length;  // 1.2 == 1.2, 1.2 < 1.2.1
	}

	/**
	 * 
	 * @param a
	 * @return
	 */
	public boolean isLowerThan(VersionNumber a) {
		int k;

		for(k = 0; k < numbers.length; k++) {
			if(k >= a.numbers.length) {
				return false;  // 1.2.1 > 1.2
			} else if(numbers[k] > a.numbers[k]) {
				return false;  // 1.2 > 1.1
			} else if(numbers[k] < a.numbers[k]) {
				return true;  // 1.2 < 1.3
			}
		}
		return k <= a.numbers.length;  // 1.2 == 1.2, 1.2 < 1.2.1
	}

	/**
	 * 
	 * @param a
	 * @return
	 */
	public boolean isIncluded(VersionNumber a) {
		int k;

		for(k = 0; k < numbers.length; k++) {
			if(k >= a.numbers.length) {
				return false;  // 1.2.1 > 1.2
			} else if(numbers[k] > a.numbers[k]) {
				return false;  // 1.2 > 1.1
			} else if(numbers[k] < a.numbers[k]) {
				return false;  // 1.2 < 1.3
			}
		}
		return k <= a.numbers.length;  // 1.2 == 1.2, 1.2 < 1.2.1
	}

}
