| 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 |
|
|
| |
|
| 74.8% |
Uncovered Elements: 30 (119) |
Complexity: 37 |
Complexity Density: 0.45 |
|
| 26 |
|
public class ExcelFile implements AutoCloseable { |
| 27 |
|
private static final DecimalFormat DECIMAL = new DecimalFormat(); |
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 28 |
1 |
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 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 38 |
0 |
public ExcelFile(Path path) throws FileNotFoundException, IOException {... |
| 39 |
0 |
this(path.toFile()); |
| 40 |
|
} |
| 41 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 42 |
7 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 52 |
151 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 59 |
157 |
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 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 68 |
0 |
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 |
|
|
| |
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 74 |
157 |
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 |
|
|
| |
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 87 |
12 |
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% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 100 |
25 |
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 |
|
|
| |
|
| 84.2% |
Uncovered Elements: 3 (19) |
Complexity: 4 |
Complexity Density: 0.31 |
|
| 106 |
145 |
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 |
|
|
| |
|
| 52.9% |
Uncovered Elements: 8 (17) |
Complexity: 9 |
Complexity Density: 0.6 |
|
| 125 |
157 |
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 |
|
|
| |
|
| 69.2% |
Uncovered Elements: 4 (13) |
Complexity: 4 |
Complexity Density: 0.57 |
|
| 145 |
12 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 157 |
26 |
public ExcelSheet sheet(String sheetName) {... |
| 158 |
26 |
return new ExcelSheet(this, sheetName); |
| 159 |
|
} |
| 160 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 161 |
0 |
public void save(Path path) {... |
| 162 |
0 |
save(path.toFile()); |
| 163 |
|
} |
| 164 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 165 |
0 |
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 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 173 |
7 |
@Override... |
| 174 |
|
public void close() { |
| 175 |
7 |
try { |
| 176 |
7 |
workbook.close(); |
| 177 |
|
} catch (IOException e) { |
| 178 |
0 |
; |
| 179 |
|
} |
| 180 |
|
} |
| 181 |
|
|
| |
|
| 68.4% |
Uncovered Elements: 6 (19) |
Complexity: 9 |
Complexity Density: 0.9 |
|
| 182 |
|
class ExcelSheet { |
| 183 |
|
private final String sheet; |
| 184 |
|
private final ExcelFile file; |
| 185 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 186 |
26 |
private ExcelSheet(ExcelFile file, String sheet) {... |
| 187 |
26 |
this.sheet = sheet; |
| 188 |
26 |
this.file = file; |
| 189 |
|
} |
| 190 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 191 |
148 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 195 |
3 |
public <T> T read(int line, int column, Converter<T> function) {... |
| 196 |
3 |
return file.read(sheet, line, column, function); |
| 197 |
|
} |
| 198 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 199 |
0 |
public String read(int line, int column, Consumer<String> action) {... |
| 200 |
0 |
return file.read(sheet, line, column, action); |
| 201 |
|
} |
| 202 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 203 |
0 |
public String read(int line, int column) {... |
| 204 |
0 |
return file.read(sheet, line, column); |
| 205 |
|
} |
| 206 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 207 |
6 |
public <T> void write(int line, int column, T value) {... |
| 208 |
6 |
file.write(sheet, line, column, value); |
| 209 |
|
} |
| 210 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 211 |
25 |
public SortedSet<Integer> getRows(Predicate<Row> predicate) {... |
| 212 |
25 |
return file.getRows(sheet, predicate); |
| 213 |
|
} |
| 214 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 215 |
0 |
public String toString() {... |
| 216 |
0 |
return sheet; |
| 217 |
|
} |
| 218 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 219 |
145 |
public int getColumn(Predicate<Cell> predicate) {... |
| 220 |
145 |
return file.getColumn(sheet, predicate); |
| 221 |
|
} |
| 222 |
|
} |
| 223 |
|
} |