/* $Id: SelectionListMembershipChecker.java 125 2011-03-09 09:49:51Z ohura $ */
package smart_gs.spread_selection;

import java.util.ArrayList;
import java.util.Vector;

public class SelectionListMembershipChecker {
	private String [] stringInterval_list;
	private int size;
	private ArrayList<Vector<Integer>> interval_list;

//	This constructor assumes the given selectionList is NOT null or empty string "".
//	Check the conditions before calling it.
	public SelectionListMembershipChecker (String selectionList) {
		stringInterval_list = selectionList.split(",");
		size = stringInterval_list.length;
		interval_list = new ArrayList();

		for (int i = size - 1; i>=0;i--) {
			String[] tmp = stringInterval_list[i].split("-");
			Vector<Integer> pair = new Vector();
			if (tmp.length == 1){
				pair.addElement(Integer.parseInt(tmp[0].trim()));
				pair.addElement(Integer.parseInt(tmp[0].trim()));
				interval_list.add(pair);
			} else {
				pair.addElement(Integer.parseInt(tmp[0].trim()));
				pair.addElement(Integer.parseInt(tmp[1].trim()));
				interval_list.add(pair);
			}
		}
	}
	
	public boolean isIn (int index){
		boolean result = false;
		for (int i = size - 1; i>=0;i--){
			Vector<Integer> pair = interval_list.get(i);
			if ( pair.get(0) <= index && index <= pair.get(1)) {
				result = true;
				break;
			}
		}
		return result;
	}
}