package edu.stanford.rt.datatype; import java.util.*; import edu.stanford.rt.credential.*; /** * @author Ninghui Li, Sandra Qiu
* * Represents EnumType element in DomainSpecification. * Each EnumType declaration object knows its legal values * and the ordering of the values if the values are ordered. */ public class EnumType extends OrderedType { /** the number of values this type has*/ private int size; /** Defines the ordering of values.*/ private DataType valueType; /** are values case-sensitive?*/ private boolean ignoreCase; /** are values ordered in any way?*/ private boolean ordered; /** the list of values in their natural order, i.e. the order that being added to the list*/ private ArrayList values; /** We also store the values in a hash set to speed up membership tests.*/ private HashSet valuesCopy; /** * Constructor for EnumType. */ public EnumType( String name, DataType valueType, OrderedMap enumValues, boolean ignoreCase, boolean ordered, int size) throws IllegalArgumentException { super(name); this.valueType = valueType; if (size != 0) this.size = size; else this.size = enumValues.size(); if (this.size <= 0) { throw new IllegalArgumentException("EnumType constructor error: empty values."); } if (!enumValues.getKeyClass().equals(String.class)) { throw new IllegalArgumentException("EnumType constructor error: non-string value."); } this.values = new ArrayList(enumValues.keyList()); this.valuesCopy = new HashSet(enumValues.keyList()); this.ignoreCase = ignoreCase; this.ordered = ordered; } /** * Method setValueType. * @param type */ public void setValueType(DataType type) { valueType = type; } /** * Method getValueType. * @return DataType */ public DataType getValueType() { return valueType; } /** * Method getValues.

* returns an unmodifiable view of the legal values. * @return List */ public synchronized List getValues() { return Collections.unmodifiableList(values); } /** * Method contains.

* checks whether the given value is part of the legal values. * @param value * @return boolean */ public boolean contains(String value) { return valuesCopy.contains(value); } /** * Method getSize.

* returns the actual number of legal values defined * for this type. * @return int */ public int getSize() { return size; } /** * Method isIgnoreCase.

* @return boolean */ public boolean isIgnoreCase() { return ignoreCase; } /** * Method isOrdered. * @return boolean */ public boolean isOrdered() { return ordered; } /* (non-Javadoc) * @see edu.stanford.rt.datatype.SimpleType#isValidValue(DataValue) */ public boolean isValidValue(DataValue v) { if (!(v instanceof EnumValue)) return false; String val = ((EnumValue) v).getValue(); if (!valuesCopy.contains(val)) return false; return true; } /* (non-Javadoc) * @see edu.stanford.rt.datatype.OrderedType#compares(DataValue, DataValue) */ public int compares(DataValue value1, DataValue value2) { if (!(value1 instanceof EnumValue) || !(value2 instanceof EnumValue)) throw new IllegalArgumentException("Wrong argument type"); String val1 = ((EnumValue) value1).getValue(); String val2 = ((EnumValue) value2).getValue(); return val1.compareTo(val2); } /* (non-Javadoc) * @see edu.stanford.rt.datatype.DataType#toString(String) */ public String toString(String indent) { String thisIndent = indent + " "; StringBuffer sb = new StringBuffer(); sb.append(thisIndent).append("EnumType: ").append( getName()).append( "\n"); sb.append(thisIndent+" ").append(" {"); Iterator it = values.iterator(); while (it.hasNext()) { sb.append("\"").append((String) it.next()).append("\" "); } sb.append("} \n"); sb .append(thisIndent + " ") .append("valueType: ") .append( (valueType == null) ? "null" : valueType.toString(thisIndent + " ")) .append("\n"); return sb.toString(); } }