]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/FieldList.java
Version 19.2, December 2018
[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         /** Array of Field objects making the list */
10         private Field[] _fieldArray;
11
12
13         /**
14          * Constructor for an empty field list
15          */
16         public FieldList()
17         {
18                 _fieldArray = new Field[0];
19         }
20
21         /**
22          * Constructor for a given number of empty fields
23          * @param inNumFields
24          */
25         public FieldList(int inNumFields)
26         {
27                 if (inNumFields < 0) inNumFields = 0;
28                 _fieldArray = new Field[inNumFields];
29         }
30
31         /**
32          * Constructor giving array of Field objects
33          * @param inFieldArray array of Field objects
34          */
35         public FieldList(Field[] inFieldArray)
36         {
37                 if (inFieldArray == null || inFieldArray.length == 0)
38                 {
39                         _fieldArray = new Field[0];
40                 }
41                 else
42                 {
43                         _fieldArray = new Field[inFieldArray.length];
44                         System.arraycopy(inFieldArray, 0, _fieldArray, 0, inFieldArray.length);
45                 }
46         }
47
48         /**
49          * Get the index of the given field
50          * @param inField field to look for
51          * @return index number of the field starting at zero
52          */
53         public int getFieldIndex(Field inField)
54         {
55                 if (inField == null) return -1;
56                 for (int f=0; f<_fieldArray.length; f++)
57                 {
58                         if (_fieldArray[f] != null && _fieldArray[f].equals(inField))
59                                 return f;
60                 }
61                 return -1;
62         }
63
64
65         /**
66          * Check whether the FieldList contains the given 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 }