View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: TabularData.java
13   * Created: 2008-06-17
14   * Author: entrop, kpietak
15   * $Id: TabularData.java 2308 2009-01-12 22:33:45Z kpietak $
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   * Contains list of rows. Rows don't need to have the same count of columns.
30   * 
31   * @author AGH CAST Team
32   */
33  public class TabularData extends AbstractRawDataMonitorable {
34  
35  	/**
36  	 * List containing table rows.
37  	 */
38  	protected LinkedList<DataRow> rows;
39  
40  	/**
41  	 * Default constructor.
42  	 */
43  	public TabularData() {
44  		rows = new LinkedList<DataRow>();
45  	}
46  
47  	/**
48  	 * Add a specified row to the end of table.
49  	 * 
50  	 * @param newRow
51  	 *            row to add
52  	 */
53  	public void addRow(DataRow newRow) {
54  		newRow.setRowNumber(rows.size());
55  		rows.add(newRow);
56  	}
57  
58  	/**
59  	 * Restores specified rows as first row in table. Notifies attached monitors if row is successfully restored.
60  	 * 
61  	 * @param row
62  	 *            row to restore
63  	 * @throws IllegalArgumentException
64  	 *             when specified row has unspecified row number
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. " + //$NON-NLS-1$
70  			                "Row which was supposed to be added: " //$NON-NLS-1$
71  			                + row.toString());
72  		} else {
73  			rows.addFirst(row);
74  			notifyRestored(row);
75  		}
76  	}
77  
78  	/**
79  	 * Returns distinct values from all rows specified columns.
80  	 * 
81  	 * @param columnIndex
82  	 *            column index
83  	 * @param numberOfRowsToIgnore
84  	 *            number of rows to ignore
85  	 * @return set of distinct values
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 	 * Adds list of rows on the end of data table.
113 	 * 
114 	 * @param newRows
115 	 *            rows to add
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 	 * Removes the specified row from table. Notifies attached monitors.
129 	 * 
130 	 * @param toDelete
131 	 *            row to delete
132 	 * @return true if row was successfully removed
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 	 * Returns column count.
145 	 * 
146 	 * @return column count which is maximum size of all rows.
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 	 * Returns table rows count.
161 	 * 
162 	 * @return table rows count
163 	 */
164 	public int size() {
165 		return rows.size();
166 	}
167 
168 	/**
169 	 * Gets row in table by row number.
170 	 * 
171 	 * @param row
172 	 *            row number
173 	 * @return row
174 	 */
175 	public DataRow get(int row) {
176 		return rows.get(row);
177 	}
178 
179 	/**
180 	 * Checks if table is empty.
181 	 * 
182 	 * @return true if table is empty, false otherwise.
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 }