package edu.stanford.rt.datatype; import java.util.*; import edu.stanford.rt.credential.*; /** * @author Ninghui Li, Sandra Qiu
* A SetValueSet object contains a set of DataValue objects, which are instances of IntegerValue, DecimalValue, StringValue, EnumValue, or PrincipalValue. All members have the same type. */ public class SetValueSet extends ValueSet { private HashSet values; /** * Constructor for an empty SetValueSet object. */ public SetValueSet(SimpleType type) { super(type); this.values = new HashSet(); } /** * Constructor for SetValueSet. */ public SetValueSet(SimpleType type, HashSet values) throws DomainSpecException { super(type); this.values = new HashSet(); Iterator it = values.iterator(); while (it.hasNext()) { Object o = it.next(); if (o == null) continue; if (!(o instanceof IntegerValue) && !(o instanceof DecimalValue) && !(o instanceof StringValue) && !(o instanceof EnumValue) && !(o instanceof PrincipalValue)) { throw new DomainSpecException("Illegal argument type"); } this.values.add((DataValue) o); } } /** Add a data value to this set. Exception is thrown if the given value is not an instance of IntegerValue, DecimalValue, StringValue, EnumValue, or PrincipalValue. */ public synchronized void addValue(DataValue value) throws DomainSpecException { if (value != null) { if (!(value instanceof IntegerValue) || !(value instanceof DecimalValue) || !(value instanceof StringValue) || !(value instanceof EnumValue) || !(value instanceof PrincipalValue)) { throw new DomainSpecException("Illegal argument type"); } this.values.add(value); } } /** Returns an unmodifiable view of the values.*/ public Set getValues() { return Collections.unmodifiableSet(values); } /* (non-Javadoc) * @see edu.stanford.rt.datatype.ValueSet#toString(String) */ public String toString(String indent) { String thisIndent = indent + " "; StringBuffer sb = new StringBuffer(); sb.append(thisIndent).append("SetValueSet: \n"); sb.append(thisIndent + " ").append( getType().toString()).append( "\n"); Iterator it = values.iterator(); while (it.hasNext()) { DataValue value = (DataValue) it.next(); sb.append(thisIndent + " ").append( value.toString()).append( "\n"); } return sb.toString(); } }