1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.rawdata;
19
20 import java.util.HashSet;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Set;
24
25 import pl.edu.agh.cast.rawdata.logging.AbstractRawDataMonitorable;
26 import pl.edu.agh.cast.rawdata.logging.IRawDataObserver;
27
28
29
30
31
32
33 public class TabularData extends AbstractRawDataMonitorable {
34
35
36
37
38 protected LinkedList<DataRow> rows;
39
40
41
42
43 public TabularData() {
44 rows = new LinkedList<DataRow>();
45 }
46
47
48
49
50
51
52
53 public void addRow(DataRow newRow) {
54 newRow.setRowNumber(rows.size());
55 rows.add(newRow);
56 }
57
58
59
60
61
62
63
64
65
66 public void restoreAsFirst(DataRow row) {
67 if (row.getRowNumber() == DataRow.UNSPECIFIED_ROW_NUMBER) {
68 throw new IllegalArgumentException(
69 "Cannot insert row with unspecified row number. Use addRow method instead. " +
70 "Row which was supposed to be added: "
71 + row.toString());
72 } else {
73 rows.addFirst(row);
74 notifyRestored(row);
75 }
76 }
77
78
79
80
81
82
83
84
85
86
87 public Set<String> getAllRowValues(int columnIndex, int numberOfRowsToIgnore) {
88 Set<String> possibleValues = new HashSet<String>();
89
90 int i = 0;
91 for (DataRow row : getAllRows()) {
92 i++;
93 if (i <= numberOfRowsToIgnore) {
94 continue;
95 }
96
97 String columnValue = null;
98
99 if (columnIndex < row.size()) {
100 columnValue = row.get(columnIndex).trim();
101 }
102
103 if (columnValue != null) {
104 possibleValues.add(columnValue);
105 }
106 }
107
108 return possibleValues;
109 }
110
111
112
113
114
115
116
117 public void addRows(List<DataRow> newRows) {
118 for (DataRow newRow : newRows) {
119 addRow(newRow);
120 }
121 }
122
123 public List<DataRow> getAllRows() {
124 return rows;
125 }
126
127
128
129
130
131
132
133
134 public boolean removeRow(DataRow toDelete) {
135 boolean res = rows.remove(toDelete);
136 if (res) {
137 notifyRemoved(toDelete);
138 }
139 return res;
140
141 }
142
143
144
145
146
147
148 public int getColumnsCount() {
149 int maxRowSize = 0;
150
151 for (DataRow row : rows) {
152 if (maxRowSize < row.size()) {
153 maxRowSize = row.size();
154 }
155 }
156 return maxRowSize;
157 }
158
159
160
161
162
163
164 public int size() {
165 return rows.size();
166 }
167
168
169
170
171
172
173
174
175 public DataRow get(int row) {
176 return rows.get(row);
177 }
178
179
180
181
182
183
184 public boolean isEmpty() {
185 return rows.isEmpty();
186 }
187
188 private void notifyRemoved(DataRow row) {
189 for (IRawDataObserver observer : getObservers()) {
190 observer.notifyRemoved(row);
191 }
192 }
193
194 private void notifyRestored(DataRow row) {
195 for (IRawDataObserver observer : getObservers()) {
196 observer.notifyRestored(row);
197 }
198 }
199 }