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

import java.util.ArrayList;
import java.util.Collections;

public class NormalizedSelectionListInString {
	
	public static String convert (ArrayList<Integer> selectionList) {
		Collections.sort(selectionList);
		StringBuilder stringToBuild;
		Integer size = selectionList.size();
		
		if (size == 0) {
			return "";
		} else if (size ==1){
			return (selectionList.get(0)).toString();
		} else {
			Integer start = selectionList.get(0);
			stringToBuild = new StringBuilder("");

			for (Integer i = 0; i < size; i++) {
				if (i == size - 1) {
					stringToBuild.append(start.toString());
					if (start < selectionList.get(i)) {
						stringToBuild.append("-");
						stringToBuild.append(selectionList.get(i));
					}
				} else if (selectionList.get(i + 1) > selectionList.get(i) + 1) {
					stringToBuild.append(start.toString());
					if (start < selectionList.get(i)) {
						stringToBuild.append("-");
						stringToBuild.append(selectionList.get(i));
					}
					;
					stringToBuild.append(",");
					start = selectionList.get(i + 1);
				}
			}
			return stringToBuild.toString();
		}
	}

}
