11package me .hsgamer .bettergui .util ;
22
3- import me .hsgamer .hscore .common .Validate ;
4-
5- import java .math .BigDecimal ;
63import java .util .*;
4+ import java .util .regex .Matcher ;
5+ import java .util .regex .Pattern ;
76import java .util .stream .Collectors ;
87import java .util .stream .IntStream ;
98import java .util .stream .Stream ;
@@ -15,6 +14,7 @@ public class SlotUtil {
1514 private static final String POS_X = "position-x" ;
1615 private static final String POS_Y = "position-y" ;
1716 private static final String POS_SLOT = "slot" ;
17+ private static final Pattern RANGE_PATTERN = Pattern .compile ("([0-9]+)-([0-9]+)" );
1818
1919 private SlotUtil () {
2020 // EMPTY
@@ -58,22 +58,21 @@ public static List<Integer> getSlots(Map<String, Object> map) {
5858 * @return the stream of slots
5959 */
6060 public static Stream <Integer > generateSlots (String input ) {
61- if (Validate .isValidInteger (input )) {
62- return Stream .of (Integer .parseInt (input ));
63- } else {
64- String [] split = input .split ("-" , 2 );
65- Optional <BigDecimal > optional1 = Validate .getNumber (split [0 ].trim ());
66- Optional <BigDecimal > optional2 = Validate .getNumber (split [1 ].trim ());
67- if (optional1 .isPresent () && optional2 .isPresent ()) {
68- int s1 = optional1 .get ().intValue ();
69- int s2 = optional2 .get ().intValue ();
70- if (s1 <= s2 ) {
71- return IntStream .rangeClosed (s1 , s2 ).boxed ();
72- } else {
73- return IntStream .rangeClosed (s2 , s1 ).boxed ().sorted (Collections .reverseOrder ());
74- }
61+ Matcher matcher = RANGE_PATTERN .matcher (input );
62+ if (matcher .matches ()) {
63+ int s1 = Integer .parseInt (matcher .group (1 ));
64+ int s2 = Integer .parseInt (matcher .group (2 ));
65+ if (s1 <= s2 ) {
66+ return IntStream .rangeClosed (s1 , s2 ).boxed ();
67+ } else {
68+ return IntStream .rangeClosed (s2 , s1 ).boxed ().sorted (Collections .reverseOrder ());
7569 }
7670 }
77- return Stream .empty ();
71+
72+ try {
73+ return Stream .of (Integer .parseInt (input ));
74+ } catch (Exception ignored ) {
75+ return Stream .empty ();
76+ }
7877 }
7978}
0 commit comments