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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
package tim.prune.data;
/**
* Class to hold an ordered list of fields
* to match the value list in a data point
*/
public class FieldList
{
/** Array of Field objects making the list */
private Field[] _fieldArray;
/**
* Constructor for an empty field list
*/
public FieldList()
{
_fieldArray = new Field[0];
}
/**
* Constructor for a given number of empty fields
* @param inNumFields
*/
public FieldList(int inNumFields)
{
if (inNumFields < 0) inNumFields = 0;
_fieldArray = new Field[inNumFields];
}
/**
* Constructor giving array of Field objects
* @param inFieldArray array of Field objects
*/
public FieldList(Field[] inFieldArray)
{
if (inFieldArray == null || inFieldArray.length == 0)
{
_fieldArray = new Field[0];
}
else
{
_fieldArray = new Field[inFieldArray.length];
System.arraycopy(inFieldArray, 0, _fieldArray, 0, inFieldArray.length);
}
}
/**
* Get the index of the given field
* @param inField field to look for
* @return index number of the field starting at zero
*/
public int getFieldIndex(Field inField)
{
if (inField == null) return -1;
for (int f=0; f<_fieldArray.length; f++)
{
if (_fieldArray[f] != null && _fieldArray[f].equals(inField))
return f;
}
return -1;
}
/**
* Check whether the FieldList contains the given Field object
* @param inField Field to check
* @return true if the FieldList contains the given field
*/
public boolean contains(Field inField)
{
return (getFieldIndex(inField) >= 0);
}
/**
* @return number of fields in list
*/
public int getNumFields()
{
if (_fieldArray == null) return 0;
return _fieldArray.length;
}
/**
* Get the specified Field object
* @param inIndex index to retrieve
* @return Field object
*/
public Field getField(int inIndex)
{
if (_fieldArray == null || inIndex < 0 || inIndex >= _fieldArray.length)
{
return null;
}
return _fieldArray[inIndex];
}
/**
* Merge this list with a second list, giving a superset
* @param inOtherList other FieldList object to merge
* @return Merged FieldList object
*/
public FieldList merge(FieldList inOtherList)
{
// count number of fields
int totalFields = _fieldArray.length;
for (int f=0; f<inOtherList._fieldArray.length; f++)
{
if (inOtherList._fieldArray[f] != null && !contains(inOtherList._fieldArray[f]))
{
totalFields++;
}
}
FieldList list = new FieldList(totalFields);
// copy these fields into array
System.arraycopy(_fieldArray, 0, list._fieldArray, 0, _fieldArray.length);
// copy additional fields from other array if any
if (totalFields > _fieldArray.length)
{
int fieldCounter = _fieldArray.length;
for (int f=0; f<inOtherList._fieldArray.length; f++)
{
if (inOtherList._fieldArray[f] != null && !contains(inOtherList._fieldArray[f]))
{
list._fieldArray[fieldCounter] = inOtherList._fieldArray[f];
fieldCounter++;
}
}
}
// return the merged list
return list;
}
/**
* Extend the field list to include the specified field
* @param inField Field to add
* @return new index of added Field
*/
public int extendList(Field inField)
{
// See if field is already in list
int currIndex = getFieldIndex(inField);
if (currIndex >= 0) return currIndex;
// Need to extend - increase array size
int oldNumFields = _fieldArray.length;
Field[] fields = new Field[oldNumFields + 1];
System.arraycopy(_fieldArray, 0, fields, 0, oldNumFields);
_fieldArray = fields;
// Add new field and return index
_fieldArray[oldNumFields] = inField;
return oldNumFields;
}
/**
* Convert to String for debug
*/
public String toString()
{
StringBuffer buffer = new StringBuffer();
buffer.append('(');
for (int i=0; i<_fieldArray.length; i++)
{
buffer.append(_fieldArray[i].getName()).append(',');
}
buffer.append(')');
return buffer.toString();
}
}
|