1 | package edu.stanford.rt.datatype; |
---|
2 | |
---|
3 | import java.util.*; |
---|
4 | |
---|
5 | import edu.stanford.rt.credential.*; |
---|
6 | |
---|
7 | /** |
---|
8 | * @author Ninghui Li, Sandra Qiu<br> |
---|
9 | * |
---|
10 | A SetValueSet object contains a set of DataValue objects, which are |
---|
11 | instances of IntegerValue, DecimalValue, StringValue, EnumValue, |
---|
12 | or PrincipalValue. All members have the same type. |
---|
13 | */ |
---|
14 | public class SetValueSet extends ValueSet |
---|
15 | { |
---|
16 | private HashSet values; |
---|
17 | |
---|
18 | /** |
---|
19 | * Constructor for an empty SetValueSet object. |
---|
20 | */ |
---|
21 | public SetValueSet(SimpleType type) |
---|
22 | { |
---|
23 | super(type); |
---|
24 | this.values = new HashSet(); |
---|
25 | |
---|
26 | } |
---|
27 | |
---|
28 | /** |
---|
29 | * Constructor for SetValueSet. |
---|
30 | */ |
---|
31 | public SetValueSet(SimpleType type, HashSet values) |
---|
32 | throws DomainSpecException |
---|
33 | { |
---|
34 | super(type); |
---|
35 | this.values = new HashSet(); |
---|
36 | |
---|
37 | Iterator it = values.iterator(); |
---|
38 | while (it.hasNext()) |
---|
39 | { |
---|
40 | Object o = it.next(); |
---|
41 | if (o == null) |
---|
42 | continue; |
---|
43 | |
---|
44 | if (!(o instanceof IntegerValue) |
---|
45 | && !(o instanceof DecimalValue) |
---|
46 | && !(o instanceof StringValue) |
---|
47 | && !(o instanceof EnumValue) |
---|
48 | && !(o instanceof PrincipalValue)) |
---|
49 | { |
---|
50 | throw new DomainSpecException("Illegal argument type"); |
---|
51 | } |
---|
52 | |
---|
53 | this.values.add((DataValue) o); |
---|
54 | } |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | Add a data value to this set. |
---|
60 | Exception is thrown if the given value is not an instance of |
---|
61 | IntegerValue, DecimalValue, StringValue, EnumValue, or PrincipalValue. |
---|
62 | */ |
---|
63 | public synchronized void addValue(DataValue value) |
---|
64 | throws DomainSpecException |
---|
65 | { |
---|
66 | if (value != null) |
---|
67 | { |
---|
68 | if (!(value instanceof IntegerValue) |
---|
69 | || !(value instanceof DecimalValue) |
---|
70 | || !(value instanceof StringValue) |
---|
71 | || !(value instanceof EnumValue) |
---|
72 | || !(value instanceof PrincipalValue)) |
---|
73 | { |
---|
74 | throw new DomainSpecException("Illegal argument type"); |
---|
75 | } |
---|
76 | |
---|
77 | this.values.add(value); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | /** Returns an unmodifiable view of the values.*/ |
---|
82 | public Set getValues() |
---|
83 | { |
---|
84 | return Collections.unmodifiableSet(values); |
---|
85 | } |
---|
86 | |
---|
87 | /* (non-Javadoc) |
---|
88 | * @see edu.stanford.rt.datatype.ValueSet#toString(String) |
---|
89 | */ |
---|
90 | public String toString(String indent) |
---|
91 | { |
---|
92 | String thisIndent = indent + " "; |
---|
93 | StringBuffer sb = new StringBuffer(); |
---|
94 | sb.append(thisIndent).append("SetValueSet: \n"); |
---|
95 | sb.append(thisIndent + " ").append( |
---|
96 | getType().toString()).append( |
---|
97 | "\n"); |
---|
98 | Iterator it = values.iterator(); |
---|
99 | while (it.hasNext()) |
---|
100 | { |
---|
101 | DataValue value = (DataValue) it.next(); |
---|
102 | sb.append(thisIndent + " ").append( |
---|
103 | value.toString()).append( |
---|
104 | "\n"); |
---|
105 | } |
---|
106 | |
---|
107 | return sb.toString(); |
---|
108 | } |
---|
109 | |
---|
110 | } |
---|