[8780cbec] | 1 | package edu.stanford.rt.datatype; |
---|
| 2 | |
---|
| 3 | import java.util.*; |
---|
| 4 | |
---|
| 5 | import edu.stanford.rt.credential.*; |
---|
| 6 | import edu.stanford.rt.util.*; |
---|
| 7 | |
---|
| 8 | /** |
---|
| 9 | * @author Ninghui Li, Sandra Qiu <br> |
---|
| 10 | * |
---|
| 11 | * Implementation of the <code>RecordType</code> element in |
---|
| 12 | * <code>DomainSpecification</code>. |
---|
| 13 | */ |
---|
| 14 | public class RecordType extends DataType |
---|
| 15 | { |
---|
| 16 | /** Maps String (field name) to DataType (field type).*/ |
---|
| 17 | private OrderedMap fieldDeclarations; //required element. |
---|
| 18 | |
---|
| 19 | /** the number of fields declared for this RecordType declaration.*/ |
---|
| 20 | private int size; |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * constructor for RecordType. |
---|
| 24 | */ |
---|
| 25 | public RecordType(String name, OrderedMap fieldDeclarations) |
---|
| 26 | throws DomainSpecException |
---|
| 27 | { |
---|
| 28 | super(name); |
---|
| 29 | int size = fieldDeclarations.size(); |
---|
| 30 | this.fieldDeclarations = |
---|
| 31 | new OrderedMap(size, String.class, DataType.class); |
---|
| 32 | this.fieldDeclarations.putAll(fieldDeclarations); |
---|
| 33 | this.fieldDeclarations.setUneditable(); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * Method getFieldDeclarations. |
---|
| 38 | * @return OrderedMap |
---|
| 39 | */ |
---|
| 40 | public OrderedMap getFieldDeclarations() |
---|
| 41 | { |
---|
| 42 | if (fieldDeclarations.isEditable()) |
---|
| 43 | throw new IllegalStateException("Field declarations are not finalized."); |
---|
| 44 | return fieldDeclarations; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Method getFieldType. |
---|
| 49 | * returns DataType for the field with given fieldName. |
---|
| 50 | * @param fieldName |
---|
| 51 | * @return DataType |
---|
| 52 | * @throws DomainSpecException |
---|
| 53 | */ |
---|
| 54 | public DataType getFieldType(String fieldName) |
---|
| 55 | throws DomainSpecException |
---|
| 56 | { |
---|
| 57 | return (DataType) fieldDeclarations.get(fieldName); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /* (non-Javadoc) |
---|
| 61 | * @see edu.stanford.rt.datatype.DataType#toString(String) |
---|
| 62 | */ |
---|
| 63 | public String toString(String indent) |
---|
| 64 | { |
---|
| 65 | String thisIndent = indent + " "; |
---|
| 66 | StringBuffer sb = new StringBuffer(); |
---|
| 67 | sb.append(thisIndent).append("RecordType: ").append( |
---|
| 68 | getName()).append( |
---|
| 69 | "\n"); |
---|
| 70 | sb.append(thisIndent+" ").append("Fields: \n"); |
---|
| 71 | Iterator it = fieldDeclarations.keyIterator(); |
---|
| 72 | while (it.hasNext()) |
---|
| 73 | { |
---|
| 74 | String key = (String) it.next(); |
---|
| 75 | sb.append(thisIndent + " ").append(key); |
---|
| 76 | DataType value = null; |
---|
| 77 | try |
---|
| 78 | { |
---|
| 79 | value = getFieldType(key); |
---|
| 80 | sb.append(" "); |
---|
| 81 | sb.append(value.toString()).append("\n"); |
---|
| 82 | } |
---|
| 83 | catch (DomainSpecException e) |
---|
| 84 | { |
---|
| 85 | e.printStackTrace(); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | } |
---|
| 89 | return sb.toString(); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | } |
---|