package nz.strydom.gross.domain;

import java.util.*;

public enum MeasureUnit {
	NONE("", "", "", UnitType.OTHER, 0)
	, KILOGRAM("Kilogram", "kg", "kg", UnitType.WEIGHT, 1000)
	, GRAM("Gram", "g", "g", UnitType.WEIGHT, 1, "gram", "grams")
	, LITRE("Litre", "l", "l", UnitType.VOLUME, 1000)
	, COUNT("Count", "", "", UnitType.COUNT, 1)
	, CUP("Cups", " cup", " cups", UnitType.VOLUME, 250)
	, TEASPOON("Teaspoon", " teaspoon", " teaspoons", UnitType.VOLUME, 5, "tspn")
	, TABLESPOON("Tablespoon", " tablespoon", " tablespoons", UnitType.VOLUME, 15, "tblsp")
	;
	
	private String description;
	public String getDescription() { return this.description; }
	
	private String display;
	public String getDisplay() { return this.display; }
	
	private UnitType unitType;
	public UnitType getUnitType() { return this.unitType; }
	
	private double amountOfBase;
	public double getAmountOfBase() { return this.amountOfBase; }
	
	private String displayPlural;
	public String getDisplayPlural() { return this.displayPlural; }
	
	private Set<String> matches = new HashSet<>();
	public Set<String> getMatches() { return this.matches; }

	private MeasureUnit(String description, String display, String displayPlural, UnitType unitType, double amountOfBase, String... matches) {
		this.description = description;
		this.display = display;
		this.displayPlural = displayPlural;
		this.unitType = unitType;
		this.amountOfBase = amountOfBase;
		this.matches.addAll(Arrays.asList(matches));
		this.matches.add(display.trim());
		this.matches.add(displayPlural.trim());
	}
	
	public boolean canConvertToOtherUnit(MeasureUnit otherUnit) {
		if (otherUnit == null || otherUnit.getUnitType() == UnitType.OTHER) {
			return false;
		}
		
		return this.getUnitType() == otherUnit.getUnitType();
	}
	
	public static MeasureUnit parse(String string) {
		String input = string.trim();
		
		ArrayList<MeasureUnit> potential = new ArrayList<>();
		
		for (MeasureUnit unit : MeasureUnit.values()) {
			for (String item : unit.getMatches()) {
				if (item.equalsIgnoreCase(input)) {
					potential.add(unit);
					break;
				}
			}
		}
		
		if (potential.size() <= 0) {
			return null;
		} else if (potential.size() > 1) {
			throw new RuntimeException("Multiple measurements matched. [" + potential.toString() + "]");
		} else {
			return potential.get(0);
		}
	}
	
}

@@ -290,19 +290,38 @@
 ME, 1000
+, %22litre%22, %22litres%22
 )%0D%0A
-
 %09, COUNT
@@ -410,16 +410,22 @@
 UME, 250
+, %22cp%22
 )%0D%0A%09, TE
@@ -496,16 +496,22 @@
 , %22tspn%22
+, %22ts%22
 )%0D%0A%09, TA

Add a few more unit matches