package edu.stanford.rt.datatype;
import java.util.*;
import edu.stanford.rt.credential.*;
import edu.stanford.rt.util.*;
/**
* @author Ninghui Li, Sandra Qiu
*
* Implementation of the RecordType
element in
* DomainSpecification
.
*/
public class RecordType extends DataType
{
/** Maps String (field name) to DataType (field type).*/
private OrderedMap fieldDeclarations; //required element.
/** the number of fields declared for this RecordType declaration.*/
private int size;
/**
* constructor for RecordType.
*/
public RecordType(String name, OrderedMap fieldDeclarations)
throws DomainSpecException
{
super(name);
int size = fieldDeclarations.size();
this.fieldDeclarations =
new OrderedMap(size, String.class, DataType.class);
this.fieldDeclarations.putAll(fieldDeclarations);
this.fieldDeclarations.setUneditable();
}
/**
* Method getFieldDeclarations.
* @return OrderedMap
*/
public OrderedMap getFieldDeclarations()
{
if (fieldDeclarations.isEditable())
throw new IllegalStateException("Field declarations are not finalized.");
return fieldDeclarations;
}
/**
* Method getFieldType.
* returns DataType for the field with given fieldName.
* @param fieldName
* @return DataType
* @throws DomainSpecException
*/
public DataType getFieldType(String fieldName)
throws DomainSpecException
{
return (DataType) fieldDeclarations.get(fieldName);
}
/* (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("RecordType: ").append(
getName()).append(
"\n");
sb.append(thisIndent+" ").append("Fields: \n");
Iterator it = fieldDeclarations.keyIterator();
while (it.hasNext())
{
String key = (String) it.next();
sb.append(thisIndent + " ").append(key);
DataType value = null;
try
{
value = getFieldType(key);
sb.append(" ");
sb.append(value.toString()).append("\n");
}
catch (DomainSpecException e)
{
e.printStackTrace();
}
}
return sb.toString();
}
}