]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/FieldList.java
Version 4, January 2008
[GpsPrune.git] / tim / prune / data / FieldList.java
1 package tim.prune.data;
2
3 /**
4  * Class to hold an ordered list of fields
5  * to match the value list in a data point
6  */
7 public class FieldList
8 {
9         private Field[] _fieldArray;
10
11
12         /**
13          * Constructor for an empty field list
14          */
15         public FieldList()
16         {
17                 _fieldArray = new Field[0];
18         }
19
20         /**
21          * Constructor for a given number of empty fields
22          * @param inNumFields
23          */
24         public FieldList(int inNumFields)
25         {
26                 if (inNumFields < 0) inNumFields = 0;
27                 _fieldArray = new Field[inNumFields];
28         }
29
30         /**
31          * Constructor giving array of Field objects
32          * @param inFieldArray array of Field objects
33          */
34         public FieldList(Field[] inFieldArray)
35         {
36                 if (inFieldArray == null || inFieldArray.length == 0)
37                 {
38                         _fieldArray = new Field[0];
39                 }
40                 else
41                 {
42                         _fieldArray = new Field[inFieldArray.length];
43                         System.arraycopy(inFieldArray, 0, _fieldArray, 0, inFieldArray.length);
44                 }
45         }
46
47         /**
48          * Get the index of the given field
49          * @param inField field to look for
50          * @return index number of the field starting at zero
51          */
52         public int getFieldIndex(Field inField)
53         {
54                 if (inField == null) return -1;
55                 for (int f=0; f<_fieldArray.length; f++)
56                 {
57                         if (_fieldArray[f] != null && _fieldArray[f].equals(inField))
58                                 return f;
59                 }
60                 return -1;
61         }
62
63
64         /**
65          * Check whether the FieldList contains the given
66          * Field object
67          * @param inField Field to check
68          * @return true if the FieldList contains the given field
69          */
70         public boolean contains(Field inField)
71         {
72                 return (getFieldIndex(inField) >= 0);
73         }
74
75
76         /**
77          * @return number of fields in list
78          */
79         public int getNumFields()
80         {
81                 if (_fieldArray == null) return 0;
82                 return _fieldArray.length;
83         }
84
85
86         /**
87          * Get the specified Field object
88          * @param inIndex index to retrieve
89          * @return Field object
90          */
91         public Field getField(int inIndex)
92         {
93                 if (_fieldArray == null || inIndex < 0 || inIndex >= _fieldArray.length)
94                 {
95                         return null;
96                 }
97                 return _fieldArray[inIndex];
98         }
99
100
101         /**
102          * Merge this list with a second list, giving a superset
103          * @param inOtherList other FieldList object to merge
104          * @return Merged FieldList object
105          */
106         public FieldList merge(FieldList inOtherList)
107         {
108                 // count number of fields
109                 int totalFields = _fieldArray.length;
110                 for (int f=0; f<inOtherList._fieldArray.length; f++)
111                 {
112                         if (inOtherList._fieldArray[f] != null && !contains(inOtherList._fieldArray[f]))
113                         {
114                                 totalFields++;
115                         }
116                 }
117                 FieldList list = new FieldList(totalFields);
118                 // copy these fields into array
119                 System.arraycopy(_fieldArray, 0, list._fieldArray, 0, _fieldArray.length);
120                 // copy additional fields from other array if any
121                 if (totalFields > _fieldArray.length)
122                 {
123                         int fieldCounter = _fieldArray.length;
124                         for (int f=0; f<inOtherList._fieldArray.length; f++)
125                         {
126                                 if (inOtherList._fieldArray[f] != null && !contains(inOtherList._fieldArray[f]))
127                                 {
128                                         list._fieldArray[fieldCounter] = inOtherList._fieldArray[f];
129                                         fieldCounter++;
130                                 }
131                         }
132                 }
133                 // return the merged list
134                 return list;
135         }
136
137
138         /**
139          * Extend the field list to include the specified field
140          * @param inField Field to add
141          * @return new index of added Field
142          */
143         public int extendList(Field inField)
144         {
145                 // See if field is already in list
146                 int currIndex = getFieldIndex(inField);
147                 if (currIndex >= 0) return currIndex;
148                 // Need to extend - increase array size
149                 int oldNumFields = _fieldArray.length;
150                 Field[] fields = new Field[oldNumFields + 1];
151                 System.arraycopy(_fieldArray, 0, fields, 0, oldNumFields);
152                 _fieldArray = fields;
153                 // Add new field and return index
154                 _fieldArray[oldNumFields] = inField;
155                 return oldNumFields;
156         }
157
158
159         /**
160          * Convert to String for debug
161          */
162         public String toString()
163         {
164                 StringBuffer buffer = new StringBuffer();
165                 buffer.append("(");
166                 for (int i=0; i<_fieldArray.length; i++)
167                 {
168                         buffer.append(_fieldArray[i].getName()).append(',');
169                 }
170                 buffer.append(")");
171                 return buffer.toString();
172         }
173 }