Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package timeflow.data.db;
 
import java.util.*;
 
// methods are public for testing purposes.
public class Schema implements Iterable<Field>
{
 
        private Map<String, Field> schema = new HashMap<String, Field>();
        private List<Field> fieldList = new ArrayList<Field>(); // so we preserve field order.
 
        public Iterator<Field> iterator()
        {
                return fieldList.iterator();
        }
 
        public Field getField(String key)
        {
                return schema.get(key);
        }
 
        public List<String> getKeys()
        {
                return new ArrayList(schema.keySet());
        }
 
        public List<Field> getFields(Class type)
        {
                List<Field> a = new ArrayList<Field>();
                for (Field s : fieldList)
                {
                        if (type == null || s.getType() == type)
                        {
                                a.add(s);
                        }
                }
                return a;
        }
 
        public List<Field> 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<String> keys = new HashSet<String>(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<Field> 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);
        }
}