1. Project Clover database Sun May 19 2019 23:30:46 BRT
  2. Package net.technearts

File ExcelFile.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
50% of files have more coverage

Code metrics

20
93
25
2
223
194
46
0.49
3.72
12.5
1.84

Classes

Class Line # Actions
ExcelFile 26 83 0% 37 30
0.747899274.8%
ExcelFile.ExcelSheet 182 10 0% 9 6
0.6842105468.4%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    package net.technearts;
2   
3    import java.io.File;
4    import java.io.FileInputStream;
5    import java.io.FileNotFoundException;
6    import java.io.FileOutputStream;
7    import java.io.IOException;
8    import java.nio.file.Path;
9    import java.text.DecimalFormat;
10    import java.util.HashMap;
11    import java.util.Iterator;
12    import java.util.Map;
13    import java.util.SortedSet;
14    import java.util.TreeSet;
15    import java.util.function.Consumer;
16    import java.util.function.Predicate;
17    import java.util.stream.Collectors;
18   
19    import org.apache.poi.ss.usermodel.Cell;
20    import org.apache.poi.ss.usermodel.Row;
21    import org.apache.poi.ss.usermodel.Sheet;
22    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
23   
24    import com.google.common.collect.Streams;
25   
 
26    public class ExcelFile implements AutoCloseable {
27    private static final DecimalFormat DECIMAL = new DecimalFormat();
 
28  1 toggle static {
29  1 DECIMAL.setMaximumFractionDigits(340);
30  1 DECIMAL.setMaximumIntegerDigits(309);
31  1 DECIMAL.setMinimumFractionDigits(0);
32  1 DECIMAL.setMinimumIntegerDigits(0);
33  1 DECIMAL.setGroupingUsed(false);
34    }
35    private XSSFWorkbook workbook;
36    private Map<String, Sheet> sheets;
37   
 
38  0 toggle public ExcelFile(Path path) throws FileNotFoundException, IOException {
39  0 this(path.toFile());
40    }
41   
 
42  7 toggle public ExcelFile(File file) throws FileNotFoundException, IOException {
43  7 this.workbook = new XSSFWorkbook(new FileInputStream(file));
44  7 sheets = new HashMap<>();
45  7 Iterator<Sheet> i = this.workbook.sheetIterator();
46  28 while (i.hasNext()) {
47  21 Sheet sheet = i.next();
48  21 sheets.put(sheet.getSheetName(), sheet);
49    }
50    }
51   
 
52  151 toggle public <T> T read(String sheetName, int line, int column, Converter<T> function,
53    Consumer<T> action) {
54  151 T result = read(sheetName, line, column, function);
55  131 action.accept(result);
56  131 return result;
57    }
58   
 
59  157 toggle public <T> T read(String sheetName, int line, int column, Converter<T> function) {
60  157 String result = read(sheetName, line, column);
61  157 try {
62  157 return function.apply(result);
63    } catch (NullPointerException e) {
64  10 return null;
65    }
66    }
67   
 
68  0 toggle public String read(String sheetName, int line, int column, Consumer<String> action) {
69  0 String result = read(sheetName, line, column);
70  0 action.accept(result);
71  0 return result;
72    }
73   
 
74  157 toggle public String read(String sheetName, int line, int column) {
75  157 String result;
76  157 try {
77  157 Sheet sheet = sheets.get(sheetName);
78  157 Row row = sheet.getRow(line);
79  157 Cell cell = row.getCell(column);
80  157 result = getCellValue(cell);
81    } catch (IllegalStateException | NullPointerException e) {
82  0 result = null;
83    }
84  157 return result;
85    }
86   
 
87  12 toggle public <T> void write(String sheetName, int line, int column, T value) {
88  12 Sheet sheet = sheets.get(sheetName);
89  12 Row row = sheet.getRow(line);
90  12 if (row == null) {
91  4 row = sheet.createRow(line);
92    }
93  12 Cell cell = row.getCell(column);
94  12 if (cell == null) {
95  12 cell = row.createCell(column);
96    }
97  12 setCellValue(cell, value);
98    }
99   
 
100  25 toggle public SortedSet<Integer> getRows(String sheetName, Predicate<Row> predicate) {
101  25 Sheet sheet = sheets.get(sheetName);
102  25 return Streams.stream(sheet::iterator).filter(predicate).map(row -> row.getRowNum())
103    .collect(Collectors.toCollection(TreeSet::new));
104    }
105   
 
106  145 toggle public int getColumn(String sheetName, Predicate<Cell> predicate) {
107  145 Sheet sheet = sheets.get(sheetName);
108  145 Iterator<Row> rows = sheet.iterator();
109  145 Iterator<Cell> cells;
110  145 Row row;
111  145 Cell cell;
112  145 while (rows.hasNext()) {
113  145 row = rows.next();
114  145 cells = row.cellIterator();
115  520 while (cells.hasNext()) {
116  520 cell = cells.next();
117  520 if (predicate.test(cell)) {
118  145 return cell.getColumnIndex();
119    }
120    }
121    }
122  0 return -1;
123    }
124   
 
125  157 toggle private String getCellValue(Cell cell) {
126  157 if (cell == null) {
127  10 return null;
128    }
129  147 switch (cell.getCellTypeEnum()) {
130  96 case STRING:
131  96 return cell.getStringCellValue();
132  51 case NUMERIC:
133  51 return DECIMAL.format(cell.getNumericCellValue());
134  0 case BOOLEAN:
135  0 return Boolean.toString(cell.getBooleanCellValue());
136  0 case _NONE:
137  0 case BLANK:
138  0 case FORMULA:
139  0 case ERROR:
140  0 default:
141  0 return null;
142    }
143    }
144   
 
145  12 toggle private void setCellValue(Cell cell, Object value) {
146  12 if (value.getClass().isAssignableFrom(Double.class)) {
147  0 cell.setCellValue((double) value);
148  12 } else if (value.getClass().isAssignableFrom(Boolean.class)) {
149  0 cell.setCellValue((boolean) value);
150  12 } else if (value.getClass().isAssignableFrom(Integer.class)) {
151  6 cell.setCellValue((int) value);
152    } else {
153  6 cell.setCellValue(value.toString());
154    }
155    }
156   
 
157  26 toggle public ExcelSheet sheet(String sheetName) {
158  26 return new ExcelSheet(this, sheetName);
159    }
160   
 
161  0 toggle public void save(Path path) {
162  0 save(path.toFile());
163    }
164   
 
165  0 toggle public void save(File file) {
166  0 try {
167  0 workbook.write(new FileOutputStream(file));
168    } catch (IOException e) {
169  0 throw new IllegalArgumentException("Arquivo não existe", e);
170    }
171    }
172   
 
173  7 toggle @Override
174    public void close() {
175  7 try {
176  7 workbook.close();
177    } catch (IOException e) {
178  0 ;
179    }
180    }
181   
 
182    class ExcelSheet {
183    private final String sheet;
184    private final ExcelFile file;
185   
 
186  26 toggle private ExcelSheet(ExcelFile file, String sheet) {
187  26 this.sheet = sheet;
188  26 this.file = file;
189    }
190   
 
191  148 toggle public <T> T read(int line, int column, Converter<T> function, Consumer<T> action) {
192  148 return file.read(sheet, line, column, function, action);
193    }
194   
 
195  3 toggle public <T> T read(int line, int column, Converter<T> function) {
196  3 return file.read(sheet, line, column, function);
197    }
198   
 
199  0 toggle public String read(int line, int column, Consumer<String> action) {
200  0 return file.read(sheet, line, column, action);
201    }
202   
 
203  0 toggle public String read(int line, int column) {
204  0 return file.read(sheet, line, column);
205    }
206   
 
207  6 toggle public <T> void write(int line, int column, T value) {
208  6 file.write(sheet, line, column, value);
209    }
210   
 
211  25 toggle public SortedSet<Integer> getRows(Predicate<Row> predicate) {
212  25 return file.getRows(sheet, predicate);
213    }
214   
 
215  0 toggle public String toString() {
216  0 return sheet;
217    }
218   
 
219  145 toggle public int getColumn(Predicate<Cell> predicate) {
220  145 return file.getColumn(sheet, predicate);
221    }
222    }
223    }