From bdd724c0d4d964eb96650e489761e2ef9456da8e Mon Sep 17 00:00:00 2001 From: Lukas Taake Date: Thu, 3 Aug 2023 19:26:24 +0200 Subject: [PATCH] Remove reflection usages --- src/com/github/miachm/sods/Sheet.java | 48 +++++++++++---------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/src/com/github/miachm/sods/Sheet.java b/src/com/github/miachm/sods/Sheet.java index 894f899..23452f1 100644 --- a/src/com/github/miachm/sods/Sheet.java +++ b/src/com/github/miachm/sods/Sheet.java @@ -4,6 +4,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; +import java.util.function.Supplier; /** * Represents a sheet in a Spreadsheet. @@ -133,28 +134,19 @@ private void deleteFields(List fields, int index, int } } - public T createInstanceOfT(Class aClass) { - try { - return aClass.getDeclaredConstructor().newInstance(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - - private T getFieldForEditing(List fields, Class aClass, int index) + private T getFieldForEditing(List fields, Supplier fieldSupplier, int index) { - List list = getFieldForEditingRange(fields, aClass, index, 1); + List list = getFieldForEditingRange(fields, fieldSupplier, index, 1); return list.get(0); } - private List getFieldForEditingRange(List fields, Class aClass, int index, int howmany) + private List getFieldForEditingRange(List fields, Supplier fieldSupplier, int index, int howmany) { Pair pair = getIndexDelete(fields, index); if (pair.second > 0) { if (pair.first == fields.size()) { - fields.add(createInstanceOfT(aClass)); + fields.add(fieldSupplier.get()); T item = fields.get(pair.first); item.num_repeated = pair.second; pair.first++; @@ -195,7 +187,7 @@ else if (item.num_repeated < howmany){ } } if (howmany > 0) { - T item = createInstanceOfT(aClass); + T item = fieldSupplier.get(); item.num_repeated = howmany; fields.add(item); list.add(item); @@ -453,8 +445,8 @@ private int getIndex(List fields, int index) } Cell getCell(int row,int column){ - Row item = getFieldForEditing(rows, Row.class, row); - Cell cell = getFieldForEditing(item.cells, Cell.class, column); + Row item = getFieldForEditing(rows, Row::new, row); + Cell cell = getFieldForEditing(item.cells, Cell::new, column); return cell; } @@ -466,7 +458,7 @@ Cell getCell(int row,int column){ public void hideRow(int row) { checkRowRange(row); - Row item = getFieldForEditing(rows, Row.class, row); + Row item = getFieldForEditing(rows, Row::new, row); item.row_style.setHidden(true); } @@ -491,7 +483,7 @@ private void toggleRows(int row, int howmany, boolean hidden) checkRowRange(row); checkRowRange(row + howmany - 1); - List list = getFieldForEditingRange(rows, Row.class, row, howmany); + List list = getFieldForEditingRange(rows, Row::new, row, howmany); for (Row item : list) item.row_style.setHidden(hidden); } @@ -504,7 +496,7 @@ private void toggleRows(int row, int howmany, boolean hidden) public void hideColumn(int column) { checkColumnRange(column); - Column item = getFieldForEditing(columns, Column.class, column); + Column item = getFieldForEditing(columns, Column::new, column); item.column_style.setHidden(true); } @@ -529,7 +521,7 @@ private void toggleColumns(int column, int howmany, boolean hidden) checkColumnRange(column); checkColumnRange(column + howmany - 1); - List list = getFieldForEditingRange(columns, Column.class, column, howmany); + List list = getFieldForEditingRange(columns, Column::new, column, howmany); for (Column item : list) item.column_style.setHidden(hidden); } @@ -656,7 +648,7 @@ public void setColumnWidth(int column, Double width) if (width < 0.0) throw new IllegalArgumentException("Width can't be negative!"); } - Column item = getFieldForEditing(columns, Column.class, column); + Column item = getFieldForEditing(columns, Column::new, column); item.column_style.setWidth(width); } @@ -678,7 +670,7 @@ public void setColumnWidths(int column, int numColumns, Double width) checkColumnRange(column); checkColumnRange(column + numColumns - 1); - List list = getFieldForEditingRange(columns, Column.class, column, numColumns); + List list = getFieldForEditingRange(columns, Column::new, column, numColumns); for (Column item : list) item.column_style.setWidth(width); } @@ -698,7 +690,7 @@ public void setRowHeight(int row, Double height) if (height < 0.0) throw new IllegalArgumentException("Height can't be negative!"); } - Row item = getFieldForEditing(rows, Row.class, row); + Row item = getFieldForEditing(rows, Row::new, row); item.row_style.setHeight(height); } @@ -718,7 +710,7 @@ public void setRowHeights(int row, int numRows, Double height) return; checkRowRange(row); checkRowRange(row + numRows - 1); - List list = getFieldForEditingRange(rows, Row.class, row, numRows); + List list = getFieldForEditingRange(rows, Row::new, row, numRows); for (Row item : list) item.row_style.setHeight(height); } @@ -733,7 +725,7 @@ public void setRowHeights(int row, int numRows, Double height) public void showRow(int row) { checkRowRange(row); - Row item = getFieldForEditing(rows, Row.class, row); + Row item = getFieldForEditing(rows, Row::new, row); item.row_style.setHidden(false); } @@ -746,7 +738,7 @@ public void showRow(int row) public void showColumn(int column) { checkColumnRange(column); - Column item = getFieldForEditing(columns, Column.class, column); + Column item = getFieldForEditing(columns, Column::new, column); item.column_style.setHidden(false); } @@ -848,7 +840,7 @@ public Style getDefaultColumnCellStyle(int column) { */ public void setDefaultColumnCellStyle(int column, Style defaultColumnCellStyle) { checkColumnRange(column); - Column item = getFieldForEditing(columns, Column.class, column); + Column item = getFieldForEditing(columns, Column::new, column); item.column_style.setDefaultCellStyle(defaultColumnCellStyle); } @@ -1006,4 +998,4 @@ public String toString() { ",\nishidden=" + isHidden + '}'; } -} \ No newline at end of file +}