Skip to content

Commit

Permalink
Merge pull request #76 from mrm1st3r/fix/remove-reflection-usage
Browse files Browse the repository at this point in the history
Remove reflection usages
  • Loading branch information
miachm authored Aug 7, 2023
2 parents badf1db + bdd724c commit d36c400
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions src/com/github/miachm/sods/Sheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -133,28 +134,19 @@ private <T extends TableField> void deleteFields(List<T> fields, int index, int
}
}

public <T extends TableField> T createInstanceOfT(Class<T> aClass) {
try {
return aClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}


private <T extends TableField> T getFieldForEditing(List<T> fields, Class<T> aClass, int index)
private <T extends TableField> T getFieldForEditing(List<T> fields, Supplier<T> fieldSupplier, int index)
{
List<T> list = getFieldForEditingRange(fields, aClass, index, 1);
List<T> list = getFieldForEditingRange(fields, fieldSupplier, index, 1);
return list.get(0);
}

private <T extends TableField> List<T> getFieldForEditingRange(List<T> fields, Class<T> aClass, int index, int howmany)
private <T extends TableField> List<T> getFieldForEditingRange(List<T> fields, Supplier<T> fieldSupplier, int index, int howmany)
{
Pair<Integer,Integer> 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++;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -453,8 +445,8 @@ private int getIndex(List<? extends TableField> 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;
}

Expand All @@ -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);
}

Expand All @@ -491,7 +483,7 @@ private void toggleRows(int row, int howmany, boolean hidden)
checkRowRange(row);
checkRowRange(row + howmany - 1);

List<Row> list = getFieldForEditingRange(rows, Row.class, row, howmany);
List<Row> list = getFieldForEditingRange(rows, Row::new, row, howmany);
for (Row item : list)
item.row_style.setHidden(hidden);
}
Expand All @@ -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);
}

Expand All @@ -529,7 +521,7 @@ private void toggleColumns(int column, int howmany, boolean hidden)
checkColumnRange(column);
checkColumnRange(column + howmany - 1);

List<Column> list = getFieldForEditingRange(columns, Column.class, column, howmany);
List<Column> list = getFieldForEditingRange(columns, Column::new, column, howmany);
for (Column item : list)
item.column_style.setHidden(hidden);
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -678,7 +670,7 @@ public void setColumnWidths(int column, int numColumns, Double width)
checkColumnRange(column);
checkColumnRange(column + numColumns - 1);

List<Column> list = getFieldForEditingRange(columns, Column.class, column, numColumns);
List<Column> list = getFieldForEditingRange(columns, Column::new, column, numColumns);
for (Column item : list)
item.column_style.setWidth(width);
}
Expand All @@ -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);
}

Expand All @@ -718,7 +710,7 @@ public void setRowHeights(int row, int numRows, Double height)
return;
checkRowRange(row);
checkRowRange(row + numRows - 1);
List<Row> list = getFieldForEditingRange(rows, Row.class, row, numRows);
List<Row> list = getFieldForEditingRange(rows, Row::new, row, numRows);
for (Row item : list)
item.row_style.setHeight(height);
}
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -1006,4 +998,4 @@ public String toString() {
",\nishidden=" + isHidden +
'}';
}
}
}

0 comments on commit d36c400

Please sign in to comment.