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 | * Represents <code>EnumType</code> element in DomainSpecification. |
---|
11 | * Each <code>EnumType</code> declaration object knows its legal values |
---|
12 | * and the ordering of the values if the values are ordered. |
---|
13 | */ |
---|
14 | public class EnumType extends OrderedType |
---|
15 | { |
---|
16 | /** the number of values this type has*/ |
---|
17 | private int size; |
---|
18 | |
---|
19 | /** Defines the ordering of values.*/ |
---|
20 | private DataType valueType; |
---|
21 | |
---|
22 | /** are values case-sensitive?*/ |
---|
23 | private boolean ignoreCase; |
---|
24 | |
---|
25 | /** are values ordered in any way?*/ |
---|
26 | private boolean ordered; |
---|
27 | |
---|
28 | /** the list of values in their natural order, i.e. the order that |
---|
29 | being added to the list*/ |
---|
30 | private ArrayList values; |
---|
31 | |
---|
32 | /** We also store the values in a hash set to speed up |
---|
33 | membership tests.*/ |
---|
34 | private HashSet valuesCopy; |
---|
35 | |
---|
36 | /** |
---|
37 | * Constructor for EnumType. |
---|
38 | */ |
---|
39 | public EnumType( |
---|
40 | String name, |
---|
41 | DataType valueType, |
---|
42 | OrderedMap enumValues, |
---|
43 | boolean ignoreCase, |
---|
44 | boolean ordered, |
---|
45 | int size) |
---|
46 | throws IllegalArgumentException |
---|
47 | { |
---|
48 | super(name); |
---|
49 | this.valueType = valueType; |
---|
50 | |
---|
51 | if (size != 0) |
---|
52 | this.size = size; |
---|
53 | else |
---|
54 | this.size = enumValues.size(); |
---|
55 | if (this.size <= 0) |
---|
56 | { |
---|
57 | throw new IllegalArgumentException("EnumType constructor error: empty values."); |
---|
58 | } |
---|
59 | |
---|
60 | if (!enumValues.getKeyClass().equals(String.class)) |
---|
61 | { |
---|
62 | throw new IllegalArgumentException("EnumType constructor error: non-string value."); |
---|
63 | } |
---|
64 | |
---|
65 | this.values = new ArrayList(enumValues.keyList()); |
---|
66 | this.valuesCopy = new HashSet(enumValues.keyList()); |
---|
67 | this.ignoreCase = ignoreCase; |
---|
68 | this.ordered = ordered; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Method setValueType. |
---|
73 | * @param type |
---|
74 | */ |
---|
75 | public void setValueType(DataType type) |
---|
76 | { |
---|
77 | valueType = type; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Method getValueType. |
---|
82 | * @return DataType |
---|
83 | */ |
---|
84 | public DataType getValueType() |
---|
85 | { |
---|
86 | return valueType; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Method getValues.<p> |
---|
91 | * returns an unmodifiable view of the legal values. |
---|
92 | * @return List |
---|
93 | */ |
---|
94 | public synchronized List getValues() |
---|
95 | { |
---|
96 | return Collections.unmodifiableList(values); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Method contains.<p> |
---|
101 | * checks whether the given value is part of the legal values. |
---|
102 | * @param value |
---|
103 | * @return boolean |
---|
104 | */ |
---|
105 | public boolean contains(String value) |
---|
106 | { |
---|
107 | return valuesCopy.contains(value); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Method getSize.<p> |
---|
112 | * returns the actual number of legal values defined |
---|
113 | * for this type. |
---|
114 | * @return int |
---|
115 | */ |
---|
116 | public int getSize() |
---|
117 | { |
---|
118 | return size; |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Method isIgnoreCase.<p> |
---|
123 | * @return boolean |
---|
124 | */ |
---|
125 | public boolean isIgnoreCase() |
---|
126 | { |
---|
127 | return ignoreCase; |
---|
128 | } |
---|
129 | /** |
---|
130 | * Method isOrdered. |
---|
131 | * @return boolean |
---|
132 | */ |
---|
133 | public boolean isOrdered() |
---|
134 | { |
---|
135 | return ordered; |
---|
136 | } |
---|
137 | |
---|
138 | /* (non-Javadoc) |
---|
139 | * @see edu.stanford.rt.datatype.SimpleType#isValidValue(DataValue) |
---|
140 | */ |
---|
141 | public boolean isValidValue(DataValue v) |
---|
142 | { |
---|
143 | if (!(v instanceof EnumValue)) |
---|
144 | return false; |
---|
145 | String val = ((EnumValue) v).getValue(); |
---|
146 | if (!valuesCopy.contains(val)) |
---|
147 | return false; |
---|
148 | return true; |
---|
149 | } |
---|
150 | |
---|
151 | /* (non-Javadoc) |
---|
152 | * @see edu.stanford.rt.datatype.OrderedType#compares(DataValue, DataValue) |
---|
153 | */ |
---|
154 | public int compares(DataValue value1, DataValue value2) |
---|
155 | { |
---|
156 | if (!(value1 instanceof EnumValue) |
---|
157 | || !(value2 instanceof EnumValue)) |
---|
158 | throw new IllegalArgumentException("Wrong argument type"); |
---|
159 | |
---|
160 | String val1 = ((EnumValue) value1).getValue(); |
---|
161 | String val2 = ((EnumValue) value2).getValue(); |
---|
162 | |
---|
163 | return val1.compareTo(val2); |
---|
164 | } |
---|
165 | |
---|
166 | /* (non-Javadoc) |
---|
167 | * @see edu.stanford.rt.datatype.DataType#toString(String) |
---|
168 | */ |
---|
169 | public String toString(String indent) |
---|
170 | { |
---|
171 | String thisIndent = indent + " "; |
---|
172 | StringBuffer sb = new StringBuffer(); |
---|
173 | sb.append(thisIndent).append("EnumType: ").append( |
---|
174 | getName()).append( |
---|
175 | "\n"); |
---|
176 | sb.append(thisIndent+" ").append(" {"); |
---|
177 | Iterator it = values.iterator(); |
---|
178 | while (it.hasNext()) |
---|
179 | { |
---|
180 | sb.append("\"").append((String) it.next()).append("\" "); |
---|
181 | } |
---|
182 | sb.append("} \n"); |
---|
183 | |
---|
184 | sb |
---|
185 | .append(thisIndent + " ") |
---|
186 | .append("valueType: ") |
---|
187 | .append( |
---|
188 | (valueType == null) |
---|
189 | ? "null" |
---|
190 | : valueType.toString(thisIndent + " ")) |
---|
191 | .append("\n"); |
---|
192 | |
---|
193 | return sb.toString(); |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | } |
---|