1 package tim.prune.data;
4 * Class to hold an ordered list of fields
5 * to match the value list in a data point
9 /** Array of Field objects making the list */
10 private Field[] _fieldArray;
14 * Constructor for an empty field list
18 _fieldArray = new Field[0];
22 * Constructor for a given number of empty fields
25 public FieldList(int inNumFields)
27 if (inNumFields < 0) inNumFields = 0;
28 _fieldArray = new Field[inNumFields];
32 * Constructor giving array of Field objects
33 * @param inFieldArray array of Field objects
35 public FieldList(Field[] inFieldArray)
37 if (inFieldArray == null || inFieldArray.length == 0)
39 _fieldArray = new Field[0];
43 _fieldArray = new Field[inFieldArray.length];
44 System.arraycopy(inFieldArray, 0, _fieldArray, 0, inFieldArray.length);
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
53 public int getFieldIndex(Field inField)
55 if (inField == null) return -1;
56 for (int f=0; f<_fieldArray.length; f++)
58 if (_fieldArray[f] != null && _fieldArray[f].equals(inField))
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
70 public boolean contains(Field inField)
72 return (getFieldIndex(inField) >= 0);
77 * @return number of fields in list
79 public int getNumFields()
81 if (_fieldArray == null) return 0;
82 return _fieldArray.length;
87 * Get the specified Field object
88 * @param inIndex index to retrieve
89 * @return Field object
91 public Field getField(int inIndex)
93 if (_fieldArray == null || inIndex < 0 || inIndex >= _fieldArray.length)
97 return _fieldArray[inIndex];
102 * Merge this list with a second list, giving a superset
103 * @param inOtherList other FieldList object to merge
104 * @return Merged FieldList object
106 public FieldList merge(FieldList inOtherList)
108 // count number of fields
109 int totalFields = _fieldArray.length;
110 for (int f=0; f<inOtherList._fieldArray.length; f++)
112 if (inOtherList._fieldArray[f] != null && !contains(inOtherList._fieldArray[f]))
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)
123 int fieldCounter = _fieldArray.length;
124 for (int f=0; f<inOtherList._fieldArray.length; f++)
126 if (inOtherList._fieldArray[f] != null && !contains(inOtherList._fieldArray[f]))
128 list._fieldArray[fieldCounter] = inOtherList._fieldArray[f];
133 // return the merged list
139 * Extend the field list to include the specified field
140 * @param inField Field to add
141 * @return new index of added Field
143 public int extendList(Field inField)
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;
160 * Convert to String for debug
162 public String toString()
164 StringBuffer buffer = new StringBuffer();
166 for (int i=0; i<_fieldArray.length; i++)
168 buffer.append(_fieldArray[i].getName()).append(',');
171 return buffer.toString();