1 /*
2 * Copyright 2012 Olivier Godineau
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 * License for the specific language governing permissions and limitations under
12 * the License.
13 */
14 package olg.csv.base;
15
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19
20 /**
21 * Row in a spreadsheet. Impoverished model to be conformed to CSV.
22 *
23 * @see IReader
24 * @author Olivier Godineau
25 *
26 */
27 public class Row implements Iterable<Cell> {
28 /**
29 * The number of this row among a row list.
30 */
31 private final int num;
32 /**
33 * Cells of this row. May be only contain cells which are no empty. Depends
34 * on reader usage and its policy
35 */
36 private final List<Cell> cells;
37
38 /**
39 * number of cell (empties and no empty cells ).
40 */
41 private final int size;
42
43 /**
44 *
45 * @return true if row represents an empty row (all cells are empty).
46 */
47 public boolean isEmpty() {
48 boolean retour = true;
49 if (!(cells == null || cells.isEmpty())) {
50
51 for (Cell cell : cells) {
52 if (!cell.isEmpty()) {
53 retour = false;
54 break;
55 }
56 }
57
58 }
59 return retour;
60 }
61
62 /**
63 * Constructs a row with a num, cells and size.
64 *
65 * @param num
66 * line number.Lines number begin at 1
67 * @param cells
68 * the cells
69 * @param size
70 * the row size. Must be >0.
71 */
72 public Row(int num, List<Cell> cells, int size) {
73 super();
74 if (num < 1) {
75 throw new IllegalArgumentException(String.format(
76 "Row constructor argument num[%s] must be a positive integer", num));
77 }
78 if (size <= 0) {
79 throw new IllegalArgumentException(String.format(
80 "Row constructor argument size[%s] must be a not null positive integer", size));
81 }
82
83 this.num = num;
84 if (cells == null) {
85 this.cells = new ArrayList<Cell>(0);
86 } else {
87 this.cells = cells;
88 }
89
90 this.size = size;
91
92 // TODO valider la liste de cells
93
94 }
95
96 /**
97 * Return the number of the row among the target document lines.
98 *
99 * @return the row num.
100 */
101 public int getNum() {
102 return num;
103 }
104
105 /**
106 * The size of this Row corresponding to the number of cells (empty and no
107 * empty). can differ from getCells().size() depending on the chosen option
108 * at reading or at writing.
109 *
110 * @return the row size.
111 */
112 public int getSize() {
113 return size;
114 }
115
116 /**
117 * Cells iterator. Only iterate registered cells in this row.
118 *
119 * @return a cells iterator.
120 */
121 public Iterator<Cell> iterator() {
122 return cells.iterator();
123 }
124
125 /**
126 * Returns registered Cell which has the given num.
127 *
128 * @param num
129 * num of the cell. Must be < row size.
130 * @return <code>null</code> if no cell found with the given num
131 */
132 public Cell getCell(int num) {
133 Cell retour = null;
134 if (num >= size) {
135 throw new IllegalArgumentException(String.format("num argument[%s] must be lesser than row size[%s]", num,
136 size));
137 }
138
139 for (Cell cell : cells) {
140 if (cell.getNum() == num) {
141 retour = cell;
142 break;
143 }
144 }
145 return retour;
146 }
147
148 /**
149 * Return cell at the specific number from this row.
150 *
151 * @param num
152 * Must be in a sheet cell number format (as A, B, C, ...,
153 * AB,...) or a non negative integer
154 * @return <code>null</code> if no cell found with the given num.
155 * @see Cell#fromSheetCellNumber(String)
156 */
157 public Cell getCell(final String num) {
158 final int cellNumber = Cell.fromSheetCellNumber(num);
159
160 return getCell(cellNumber);
161
162 }
163
164 /**
165 * Returns a representation of this row like
166 * {num:num,size:size,cells:cells}.
167 *
168 * @return the string representation.
169 */
170 public String toString() {
171 return new StringBuilder().append("{num:").append(num).append(",size:").append(size).append(",cells:")
172 .append(cells.toString()).append("}").toString();
173 }
174
175 /**
176 * Create a copy with its size and cells.
177 *
178 * @param num
179 * the num of the row
180 * @return the row
181 */
182 public Row copy(int num) {
183 return new Row(num, cells, size);
184 }
185 }