Skip to content

Commit

Permalink
fori-loops performance and readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
akmsw committed Jun 28, 2023
1 parent dee5d20 commit 29c01d9
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 66 deletions.
10 changes: 6 additions & 4 deletions src/main/java/armameeldoparti/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ private static void populatePlayersSets() {
for (Position position : Position.values()) {
List<Player> playersSet = new ArrayList<>();

for (int i = 0; i < CommonFields.getPlayersAmountMap()
.get(position) * 2; i++) {
int totalPlayersInPosition = CommonFields.getPlayersAmountMap()
.get(position) * 2;

for (int i = 0; i < totalPlayersInPosition; i++) {
playersSet.add(new Player("", position));
}

Expand Down Expand Up @@ -215,8 +217,8 @@ private static void setGraphicalProperties() {
* @param font Font to use.
*/
private static void setProgramFont(Font font) {
final Enumeration<Object> keys = UIManager.getDefaults()
.keys();
Enumeration<Object> keys = UIManager.getDefaults()
.keys();

while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Expand Down
45 changes: 25 additions & 20 deletions src/main/java/armameeldoparti/controllers/AnchoragesController.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ public void updateCheckBoxesText() {
Map<Position, List<JCheckBox>> checkBoxesMap = ((AnchoragesView) getView()).getCheckBoxesMap();

for (Position position : Position.values()) {
for (int i = 0; i < CommonFields.getPlayersSets()
.get(position)
.size(); i++) {
int positionSetSize = CommonFields.getPlayersSets()
.get(position)
.size();

for (int checkBoxIndex = 0; checkBoxIndex < positionSetSize; checkBoxIndex++) {
checkBoxesMap.get(position)
.get(i)
.get(checkBoxIndex)
.setText(CommonFields.getPlayersSets()
.get(position)
.get(i)
.get(checkBoxIndex)
.getName());
}
}
Expand Down Expand Up @@ -174,8 +176,8 @@ public void deleteLastAnchorageButtonEvent() {
public void deleteAnchorageButtonEvent(Component parentComponent) {
String[] optionsDelete = new String[anchoragesAmount];

for (int i = 0; i < anchoragesAmount; i++) {
optionsDelete[i] = Integer.toString(i + 1);
for (int anchorageNumber = 0; anchorageNumber < anchoragesAmount; anchorageNumber++) {
optionsDelete[anchorageNumber] = Integer.toString(anchorageNumber + 1);
}

int anchorageToDelete = JOptionPane.showOptionDialog(
Expand Down Expand Up @@ -251,31 +253,32 @@ private void updateTextArea() {
.setText("");

var wrapper = new Object() {
private int anchorageNum;
private int anchorageNumber;
private int counter = 1;
};

for (wrapper.anchorageNum = 1;
wrapper.anchorageNum <= anchoragesAmount;
wrapper.anchorageNum++) {
for (wrapper.anchorageNumber = 1;
wrapper.anchorageNumber <= anchoragesAmount;
wrapper.anchorageNumber++) {
((AnchoragesView) getView()).getTextArea()
.append(" ----- ANCLAJE #" + wrapper.anchorageNum
+ " -----" + System.lineSeparator());
.append(" ----- ANCLAJE #" + wrapper.anchorageNumber + " -----"
+ System.lineSeparator());

CommonFields.getPlayersSets()
.forEach((key, value) -> value.stream()
.filter(p -> p.getAnchorageNumber()
== wrapper.anchorageNum)
== wrapper.anchorageNumber)
.forEach(p -> {
((AnchoragesView) getView()).getTextArea()
.append(" " + wrapper.counter
+ ". " + p.getName()
((AnchoragesView) getView())
.getTextArea()
.append(" " + wrapper.counter + ". "
+ p.getName()
+ System.lineSeparator());

wrapper.counter++;
}));

if (wrapper.anchorageNum != anchoragesAmount) {
if (wrapper.anchorageNumber != anchoragesAmount) {
((AnchoragesView) getView()).getTextArea()
.append(System.lineSeparator());
}
Expand Down Expand Up @@ -347,8 +350,10 @@ private void deleteAnchorage(int anchorageToDelete) {
changeAnchorage(anchorageToDelete, 0);

if (anchorageToDelete != anchoragesAmount) {
for (int i = anchorageToDelete + 1; i <= anchoragesAmount; i++) {
changeAnchorage(i, i - 1);
for (int anchorageNumber = anchorageToDelete + 1;
anchorageNumber <= anchoragesAmount;
anchorageNumber++) {
changeAnchorage(anchorageNumber, anchorageNumber - 1);
}
}

Expand Down
68 changes: 42 additions & 26 deletions src/main/java/armameeldoparti/controllers/ResultsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ public void updateTable() {
});

if (CommonFields.getDistribution() == Constants.MIX_BY_SKILLS) {
for (int i = 0; i < 2; i++) {
for (int teamIndex = 0; teamIndex < 2; teamIndex++) {
((ResultsView) getView()).getTable()
.setValueAt(teams.get(i)
.setValueAt(teams.get(teamIndex)
.getTeamPlayers()
.values()
.stream()
Expand All @@ -189,7 +189,7 @@ public void updateTable() {
.reduce(0, Math::addExact),
((ResultsView) getView()).getTable()
.getRowCount() - 1,
i + 1);
teamIndex + 1);
}
}
}
Expand Down Expand Up @@ -224,23 +224,28 @@ public List<Team> bySkillsMix() {
public void setTableCellsSize() {
JTable table = ((ResultsView) getView()).getTable();

for (int column = 0; column < table.getColumnCount(); column++) {
int columnCount = table.getColumnCount();
int rowCount = table.getRowCount();

for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
table.getColumnModel()
.getColumn(column)
.getColumn(columnIndex)
.setPreferredWidth(FIXED_CELL_WIDTH);
}

for (int i = 0; i < table.getRowCount(); i++) {
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
int rowHeight = table.getRowHeight();

for (int j = 0; j < table.getColumnCount(); j++) {
Component component = table.prepareRenderer(table.getCellRenderer(i, j), i, j);
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
Component component = table.prepareRenderer(table.getCellRenderer(rowIndex, columnIndex),
rowIndex,
columnIndex);

rowHeight = Math.max(rowHeight, component.getPreferredSize()
.height);
}

table.setRowHeight(i, rowHeight);
table.setRowHeight(rowIndex, rowHeight);
}
}

Expand Down Expand Up @@ -340,40 +345,51 @@ public Component getTableCellRendererComponent(JTable myTable, Object value,
* Fills the table cells whose texts do not change.
*/
private void fillTableFields() {
for (int i = 0; i < 2; i++) {
for (int teamIndex = 0; teamIndex < 2; teamIndex++) {
((ResultsView) getView()).getTable()
.setValueAt("EQUIPO #" + (i + 1), 0, i + 1);
.setValueAt("EQUIPO #" + (teamIndex + 1), 0, teamIndex + 1);
}

for (int i = 1; i < ((ResultsView) getView()).getTable()
.getRowCount() - 1; i++) {
if (i == 1) {
int rowCount = ((ResultsView) getView()).getTable()
.getRowCount() - 1;

for (int rowIndex = 1; rowIndex < rowCount; rowIndex++) {
if (rowIndex == 1) {
((ResultsView) getView()).getTable()
.setValueAt(CommonFields.getPositionsMap()
.get(Position.CENTRAL_DEFENDER), i, 0);
} else if (i < 4) {
.get(Position.CENTRAL_DEFENDER),
rowIndex,
0);
} else if (rowIndex < 4) {
((ResultsView) getView()).getTable()
.setValueAt(CommonFields.getPositionsMap()
.get(Position.LATERAL_DEFENDER), i, 0);
} else if (i < 6) {
.get(Position.LATERAL_DEFENDER),
rowIndex,
0);
} else if (rowIndex < 6) {
((ResultsView) getView()).getTable()
.setValueAt(CommonFields.getPositionsMap()
.get(Position.MIDFIELDER), i, 0);
} else if (i < 7) {
.get(Position.MIDFIELDER),
rowIndex,
0);
} else if (rowIndex < 7) {
((ResultsView) getView()).getTable()
.setValueAt(CommonFields.getPositionsMap()
.get(Position.FORWARD), i, 0);
.get(Position.FORWARD),
rowIndex,
0);
}
}

if (CommonFields.getDistribution() == Constants.MIX_BY_SKILLS) {
for (int i = 0; i < 2; i++) {
for (int teamIndex = 0; teamIndex < 2; teamIndex++) {
((ResultsView) getView()).getTable()
.setValueAt(i == 0 ? CommonFields.getPositionsMap()
.get(Position.GOALKEEPER)
: "PUNTAJE DEL EQUIPO",
.setValueAt(teamIndex == 0 ? CommonFields.getPositionsMap()
.get(Position.GOALKEEPER)
: "PUNTAJE DEL EQUIPO",
((ResultsView) getView()).getTable()
.getRowCount() + i - 2, 0);
.getRowCount() + teamIndex - 2,
0);
}
} else {
((ResultsView) getView()).getTable()
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/armameeldoparti/utils/mixers/BySkillsMixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ public List<Team> withoutAnchorages(@NonNull List<Team> teams) {
teams.sort(Comparator.comparingInt(Team::getTeamSkill));

if (currentSet.size() == 2) {
for (int i = 0; i < 2; i++) {
teams.get(i)
for (int teamIndex = 0; teamIndex < 2; teamIndex++) {
teams.get(teamIndex)
.getTeamPlayers()
.get(position)
.add(currentSet.get(i));
.add(currentSet.get(teamIndex));
}
} else {
distributeSubsets(teams, currentSet, position);
Expand Down Expand Up @@ -204,18 +204,18 @@ private void distributeSubsets(@NonNull List<Team> teams,
.reduce(0, Math::addExact)));

var wrapper = new Object() {
int index;
int teamIndex;
};

for (wrapper.index = 0; wrapper.index < 2; wrapper.index++) {
playersSubsets.get(wrapper.index)
.forEach(p -> p.setTeamNumber(teams.get(1 - wrapper.index)
for (wrapper.teamIndex = 0; wrapper.teamIndex < 2; wrapper.teamIndex++) {
playersSubsets.get(wrapper.teamIndex)
.forEach(p -> p.setTeamNumber(teams.get(1 - wrapper.teamIndex)
.getTeamNumber()));

teams.get(wrapper.index)
teams.get(wrapper.teamIndex)
.getTeamPlayers()
.get(position)
.addAll(playersSubsets.get(1 - wrapper.index));
.addAll(playersSubsets.get(1 - wrapper.teamIndex));
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/armameeldoparti/utils/mixers/RandomMixer.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public List<Team> withoutAnchorages(@NonNull List<Team> teams) {
List<Player> playersSet = CommonFields.getPlayersSets()
.get(position);

for (int i = 0; i < playersSet.size() / 2; i++) {
int halfPlayersInSet = playersSet.size() / 2;

for (int i = 0; i < halfPlayersInSet; i++) {
chosenPlayer = playersSet.get(
playersSet.indexOf(
getRandomUnassignedPlayer(playersSet.stream()
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/armameeldoparti/views/NamesInputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ private void addAnchoragesCheckBox() {
*/
private void addTextFields() {
for (Position position : Position.values()) {
for (int i = 0; i < CommonFields.getPlayersAmountMap()
.get(position) * 2; i++) {
int totalPlayersInPosition = CommonFields.getPlayersAmountMap()
.get(position) * 2;

for (int i = 0; i < totalPlayersInPosition; i++) {
JTextField tf = new JTextField();

tf.addActionListener(e -> {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/armameeldoparti/views/SkillPointsInputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,24 @@ private void addSpinners() {
List<Player> currentSet = CommonFields.getPlayersSets()
.get(position);

for (int j = 0; j < currentSet.size(); j++) {
int currentSetSize = currentSet.size();

for (int playerIndex = 0; playerIndex < currentSetSize; playerIndex++) {
JSpinner spinner = new JSpinner(new SpinnerNumberModel(Constants.SKILL_INI,
Constants.SKILL_MIN,
Constants.SKILL_MAX,
Constants.SKILL_STEP));

JLabel nameLabel = new JLabel(currentSet.get(j)
JLabel nameLabel = new JLabel(currentSet.get(playerIndex)
.getName());

spinnersMap.put(currentSet.get(j), spinner);
spinnersMap.put(currentSet.get(playerIndex), spinner);

labelsMap.put(spinner, nameLabel);

getMasterPanel().add(nameLabel, "pushx");
getMasterPanel().add(spinnersMap.get(currentSet.get(j)), j % 2 != 0 ? "wrap" : null);
getMasterPanel().add(spinnersMap.get(currentSet.get(playerIndex)),
playerIndex % 2 != 0 ? "wrap" : null);
}

spinnersMap.values()
Expand Down

0 comments on commit 29c01d9

Please sign in to comment.