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 private Field[] _fieldArray;
13 * Constructor for an empty field list
17 _fieldArray = new Field[0];
21 * Constructor for a given number of empty fields
24 public FieldList(int inNumFields)
26 if (inNumFields < 0) inNumFields = 0;
27 _fieldArray = new Field[inNumFields];
31 * Constructor giving array of Field objects
34 public FieldList(Field[] inFieldArray)
36 if (inFieldArray == null || inFieldArray.length == 0)
38 _fieldArray = new Field[0];
42 _fieldArray = new Field[inFieldArray.length];
43 System.arraycopy(inFieldArray, 0, _fieldArray, 0, inFieldArray.length);
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
52 public int getFieldIndex(Field inField)
54 if (inField == null) return -1;
55 for (int f=0; f<_fieldArray.length; f++)
57 if (_fieldArray[f] != null && _fieldArray[f].equals(inField))
65 * Check whether the FieldList contains the given
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 * Convert to String for debug
141 public String toString()
143 StringBuffer buffer = new StringBuffer();
145 for (int i=0; i<_fieldArray.length; i++)
147 buffer.append(_fieldArray[i].getName()).append(',');
150 return buffer.toString();