Skip to content

Commit 3ae910c

Browse files
committed
getSlots to IntStream
1 parent 2ae5085 commit 3ae910c

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

src/main/java/me/hsgamer/bettergui/util/SlotUtil.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import me.hsgamer.hscore.common.Validate;
44

55
import java.math.BigDecimal;
6-
import java.util.*;
7-
import java.util.stream.Collectors;
6+
import java.util.Arrays;
7+
import java.util.Map;
8+
import java.util.Optional;
89
import java.util.stream.IntStream;
9-
import java.util.stream.Stream;
1010

1111
/**
1212
* The utility class for generating slots
@@ -27,23 +27,21 @@ private SlotUtil() {
2727
*
2828
* @return the slots
2929
*/
30-
public static List<Integer> getSlots(Map<String, Object> map) {
31-
List<Integer> slots = new ArrayList<>();
30+
public static IntStream getSlots(Map<String, Object> map) {
31+
IntStream slots = IntStream.empty();
3232

3333
if (map.containsKey(POS_X) || map.containsKey(POS_Y)) {
34-
int x = 1;
35-
int y = 1;
36-
if (map.containsKey(POS_X)) {
37-
x = Integer.parseInt(String.valueOf(map.get(POS_X)));
34+
Optional<Integer> x = Validate.getNumber(String.valueOf(map.get(POS_X))).map(BigDecimal::intValue);
35+
Optional<Integer> y = Validate.getNumber(String.valueOf(map.get(POS_Y))).map(BigDecimal::intValue);
36+
if (x.isPresent() && y.isPresent()) {
37+
slots = IntStream.of((y.get() - 1) * 9 + x.get() - 1);
3838
}
39-
if (map.containsKey(POS_Y)) {
40-
y = Integer.parseInt(String.valueOf(map.get(POS_Y)));
41-
}
42-
slots.add((y - 1) * 9 + x - 1);
4339
}
40+
4441
if (map.containsKey(POS_SLOT)) {
45-
slots.addAll(generateSlots(String.valueOf(map.get(POS_SLOT))).collect(Collectors.toList()));
42+
slots = IntStream.concat(slots, generateSlots(String.valueOf(map.get(POS_SLOT))));
4643
}
44+
4745
return slots;
4846
}
4947

@@ -54,24 +52,24 @@ public static List<Integer> getSlots(Map<String, Object> map) {
5452
*
5553
* @return the stream of slots
5654
*/
57-
public static Stream<Integer> generateSlots(String input) {
55+
public static IntStream generateSlots(String input) {
5856
return Arrays.stream(input.split(","))
5957
.map(String::trim)
60-
.flatMap(rawSlot -> {
58+
.flatMapToInt(rawSlot -> {
6159
String[] rangeSplit = rawSlot.split("-", 2);
6260
if (rangeSplit.length == 2) {
6361
Optional<Integer> start = Validate.getNumber(rangeSplit[0]).map(BigDecimal::intValue);
6462
Optional<Integer> end = Validate.getNumber(rangeSplit[1]).map(BigDecimal::intValue);
6563
if (start.isPresent() && end.isPresent()) {
66-
return IntStream.rangeClosed(start.get(), end.get()).boxed();
64+
return IntStream.rangeClosed(start.get(), end.get());
6765
} else {
68-
return Stream.empty();
66+
return IntStream.empty();
6967
}
7068
} else {
7169
return Validate.getNumber(rawSlot)
7270
.map(BigDecimal::intValue)
73-
.map(Stream::of)
74-
.orElseGet(Stream::empty);
71+
.map(IntStream::of)
72+
.orElseGet(IntStream::empty);
7573
}
7674
});
7775
}

0 commit comments

Comments
 (0)