Skip to content

Commit 5198fe5

Browse files
Improve performance
instead of checking rows, cols, executing like 5 getter methods, now Slot includes Slot#isSlot. if it is less than 0 then isSlot returns false, and if it goes above 53 the constructor would default to -1 (meaning it's not a valid slot). This improves performance when checking for any remaining slots.
1 parent c6e279c commit 5198fe5

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/main/java/me/flame/menus/menu/iterator/MenuIterator.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
import java.util.Iterator;
1010
import java.util.NoSuchElementException;
11+
import java.util.Objects;
12+
import java.util.Optional;
1113

1214
/**
1315
*
14-
* @author Mqzen
16+
* @author Mqzen, FlameyosFlow (Mostly Mqzen)
1517
* @date 28/8/2023
1618
*
1719
* A special class made to iterate over complex menus.
@@ -38,8 +40,7 @@ public MenuIterator(int startingRow, int startingCol,
3840
public @Nullable Slot nextSlot(boolean emptyOnly) {
3941
if (!emptyOnly) {
4042
Slot newPos = direction.shift(currentPosition, menu.getRows());
41-
if (newPos.getRow() >= menu.getRows() || newPos.getRow() < 1
42-
|| newPos.getColumn() > 9 || newPos.getColumn() < 1) {
43+
if (!currentPosition.isSlot()) {
4344
return null;
4445
}
4546
currentPosition = newPos;
@@ -49,8 +50,7 @@ public MenuIterator(int startingRow, int startingCol,
4950
while (menu.hasItem(currentPosition)) {
5051
Slot shiftedPos = direction.shift(currentPosition, menu.getRows());
5152

52-
if (shiftedPos.getRow() >= menu.getRows() || shiftedPos.getRow() < 1
53-
|| shiftedPos.getColumn() > 9 || shiftedPos.getColumn() < 1) {
53+
if (!currentPosition.isSlot()) {
5454
return null;
5555
}
5656

@@ -63,8 +63,7 @@ public MenuIterator(int startingRow, int startingCol,
6363

6464
public @Nullable Slot nextSlot() {
6565
Slot newPos = direction.shift(currentPosition, menu.getRows());
66-
if (newPos.getRow() >= menu.getRows() || newPos.getRow() < 1
67-
|| newPos.getColumn() > 9 || newPos.getColumn() < 1) {
66+
if (!newPos.isSlot()) {
6867
return null;
6968
}
7069
currentPosition = newPos;
@@ -73,18 +72,25 @@ public MenuIterator(int startingRow, int startingCol,
7372

7473
@Override
7574
public boolean hasNext() {
76-
return currentPosition.getRow() >= menu.getRows() || currentPosition.getRow() < 1
77-
|| currentPosition.getColumn() > 9 || currentPosition.getColumn() < 1;
75+
return currentPosition.isSlot();
7876
}
7977

8078
@Override
81-
public MenuItem next() {
82-
Slot slot = nextSlot(false);
79+
public @Nullable MenuItem next() {
80+
Slot slot = nextSlot();
8381
if (slot == null)
8482
throw new NoSuchElementException("Used MenuIterator#next() but no more items to iterate over");
8583
return menu.getItem(slot);
8684
}
8785

86+
public @NotNull MenuItem nextNotNull() {
87+
return Objects.requireNonNull(next());
88+
}
89+
90+
public Optional<MenuItem> nextOptional() {
91+
return Optional.ofNullable(next());
92+
}
93+
8894
public enum IterationDirection {
8995
HORIZONTAL() {
9096
@Override
@@ -93,12 +99,8 @@ Slot shift(Slot oldPos, int maxRows) {
9399
int oldCol = oldPos.getColumn();
94100
int oldRow = oldPos.getRow();
95101

96-
if (oldCol == 9 && oldRow < maxRows) {
97-
oldRow++;
98-
oldCol = 1;
99-
} else {
100-
oldCol++;
101-
}
102+
oldCol = (oldCol == 9 && oldRow < maxRows) ? 1 : oldCol + 1;
103+
if (oldCol == 1) oldRow++;
102104

103105
return new Slot(oldRow, oldCol);
104106
}
@@ -142,13 +144,12 @@ Slot shift(Slot oldPos, int maxRows) {
142144
}
143145
return new Slot(row, oldPos.getColumn());
144146
}
145-
146147
},
147148

148149
RIGHT_ONLY {
149150
@Override
150151
Slot shift(Slot oldPos, int maxRows) {
151-
int col = oldPos.getColumn()+1;
152+
int col = oldPos.getColumn() + 1;
152153
if (col > 9) {
153154
col = oldPos.getColumn();
154155
}
@@ -157,10 +158,10 @@ Slot shift(Slot oldPos, int maxRows) {
157158
},
158159

159160

160-
LEFT_ONLY{
161+
LEFT_ONLY {
161162
@Override
162163
Slot shift(Slot oldPos, int maxRows) {
163-
int col = oldPos.getColumn()-1;
164+
int col = oldPos.getColumn() - 1;
164165
if (col < 1) {
165166
col = oldPos.getColumn();
166167
}

0 commit comments

Comments
 (0)