File: FieldList.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (164 lines) | stat: -rw-r--r-- 3,238 bytes parent folder | download | duplicates (4)
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
package tim.prune.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Class to hold an ordered list of fields
 * to match the value list in a data point
 */
public class FieldList
{
	/** List of Field objects */
	private final ArrayList<Field> _fields;


	/**
	 * Constructor for an empty field list
	 */
	public FieldList() {
		_fields = new ArrayList<>();
	}

	/**
	 * Constructor giving multiple Field objects
	 * @param inFields Field objects
	 */
	public FieldList(Field ... inFields)
	{
		this();
		Collections.addAll(_fields, inFields);
	}

	/**
	 * 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;
		int index = 0;
		for (Field field : _fields)
		{
			if (field != null && field.equals(inField)) {
				return index;
			}
			index++;
		}
		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() {
		return _fields.size();
	}


	/**
	 * Get the specified Field object
	 * @param inIndex index to retrieve
	 * @return Field object
	 */
	public Field getField(int inIndex)
	{
		try {
			return _fields.get(inIndex);
		}
		catch (IndexOutOfBoundsException e) {
			return null;
		}
	}


	/**
	 * 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)
	{
		FieldList superset = new FieldList();
		superset._fields.addAll(_fields);
		if (inOtherList != null)
		{
			for (Field field : inOtherList._fields)
			{
				if (!contains(field)) {
					superset._fields.add(field);
				}
			}
		}
		return superset;
	}


	/**
	 * Extend the field list to include the specified field
	 * @param inField Field to add
	 * @return new index of added Field
	 */
	public int addField(Field inField)
	{
		// See if field is already in list
		int currIndex = getFieldIndex(inField);
		if (currIndex >= 0) {
			return currIndex;
		}
		// Add new field and return index
		_fields.add(inField);
		return getNumFields() - 1;
	}

	/**
	 * Extend the field list to include the specified fields
	 * @param inFields Field objects to add
	 */
	public void addFields(Field ... inFields)
	{
		for (Field field : inFields) {
			addField(field);
		}
	}

	/**
	 * Convert to String for debug
	 */
	public String toString()
	{
		StringBuilder result = new StringBuilder();
		result.append('(');
		for (Field field : _fields) {
			result.append(field.getName()).append(',');
		}
		result.append(')');
		return result.toString();
	}

	/** @return list of fields specific to the given file type */
	public List<Field> getFields(FileType inFileType)
	{
		ArrayList<Field> fields = new ArrayList<Field>();
		for (Field field : _fields)
		{
			if (field.isSpecificToFileType(inFileType)) {
				fields.add(field);
			}
		}
		return fields;
	}
}