package timeflow.data.db; import java.util.*; // methods are public for testing purposes. public class Schema implements Iterable { private Map schema = new HashMap(); private List fieldList = new ArrayList(); // so we preserve field order. public Iterator iterator() { return fieldList.iterator(); } public Field getField(String key) { return schema.get(key); } public List getKeys() { return new ArrayList(schema.keySet()); } public List getFields(Class type) { List a = new ArrayList(); for (Field s : fieldList) { if (type == null || s.getType() == type) { a.add(s); } } return a; } public List getFields() { return getFields(null); } // not sure this actually works! removing things while iterating? to-do: test! public void delete(Field field) { if (schema.get(field.getName()) == null) { throw new IllegalArgumentException("No field exists: " + field); } Set keys = new HashSet(schema.keySet()); for (String s : keys) { Field f = schema.get(s); if (f == field) { schema.remove(s); } } fieldList.remove(field); } public void addAlias(Field field, String name) { if (field == null) { schema.remove(name); return; } if (!schema.values().contains(field)) { throw new IllegalArgumentException("Field does not exist in schema: " + field); } schema.put(name, field); } public Field add(String name, Class type) { return add(new Field(name, type)); } public Field add(Field field) { if (schema.get(field.getName()) != null) { throw new IllegalArgumentException("Schema already has field named '" + field.getName() + "', type=" + field.getType()); } schema.put(field.getName(), field); fieldList.add(field); return field; } public void setNewFieldOrder(List newOrder) { // first, we go through and check that this really is a new ordering! if (newOrder.size() != fieldList.size()) { throw new IllegalArgumentException("Field lists have different sizes"); } for (Field f : newOrder) { if (!fieldList.contains(f)) { throw new IllegalArgumentException("New field list has unexpected field: " + f); } } fieldList = newOrder; } public void print() { System.out.println(schema); } public void renameField(Field field, String name) { Field old = schema.get(name); if (old != null && old != field) { throw new IllegalArgumentException("Can't rename a field to a name that already exists: " + name); } schema.remove(field); field.setName(name); schema.put(name, field); } }