This repository was archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataUtilities.java
More file actions
129 lines (105 loc) · 4.12 KB
/
DataUtilities.java
File metadata and controls
129 lines (105 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package de.timmi6790.basemod.utilities;
import lombok.experimental.UtilityClass;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;
@UtilityClass
public class DataUtilities {
private final Pattern BOOLEAN_PATTERN = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
public boolean isInt(final Object value) {
try {
Integer.parseInt(String.valueOf(value));
return true;
} catch (final NumberFormatException ignore) {
return false;
}
}
public boolean isDouble(final Object value) {
try {
Double.parseDouble(String.valueOf(value));
return true;
} catch (final NumberFormatException ignore) {
return false;
}
}
public boolean isFloat(final Object value) {
try {
Float.parseFloat(String.valueOf(value));
return true;
} catch (final NumberFormatException ignore) {
return false;
}
}
public boolean isBoolean(final Object value) {
return BOOLEAN_PATTERN.matcher(String.valueOf(value)).matches();
}
public boolean hasEmptyArg(final String[] args, final int position) {
return args.length == position + 1 && args[position].length() == 0;
}
public List<String> getStartWithIgnoreCase(final Collection<String> options, final String start) {
return getStartWithIgnoreCase(options.toArray(new String[0]), start);
}
public List<String> getStartWithIgnoreCase(final String[] options, final String start) {
if (start.length() == 0 || (start.length() == 1 && start.charAt(0) == ' ')) {
return new ArrayList<>(Arrays.asList(options));
}
final List<String> tabCompleteOptions = new ArrayList<>();
final String startLower = start.toLowerCase();
for (final String option : options) {
if (option.toLowerCase().startsWith(startLower) && !option.equalsIgnoreCase(start)) {
tabCompleteOptions.add(option);
}
}
return tabCompleteOptions;
}
public List<String> addToListIfStartWith(final List<String> list, final String condition, final String toCheck) {
if (toCheck.length() == 0 || (toCheck.length() == 1 && toCheck.charAt(0) == ' ')
|| condition.toLowerCase().startsWith(toCheck.toLowerCase())) {
list.add(condition);
}
return list;
}
public void shuffleArray(final int[] array) {
for (int index = array.length - 1; index > 0; index--) {
final int switchIndex = ThreadLocalRandom.current().nextInt(index + 1);
final int temp = array[switchIndex];
array[switchIndex] = array[index];
array[index] = temp;
}
}
public List<String> combine(final String prefix, final Collection<String> values) {
final List<String> combinedValues = new ArrayList<>();
for (final String value : values) {
combinedValues.add(prefix + value);
}
return combinedValues;
}
public boolean containsIgnoreCase(final Collection<String> values, final String search) {
for (final String value : values) {
if (value.equalsIgnoreCase(search)) {
return true;
}
}
return false;
}
public void removeIgnoreCase(final Collection<String> values, final String search) {
values.removeIf(value -> value.equalsIgnoreCase(search));
}
public List<String> collectionToLowerCase(final Collection<String> values) {
final List<String> newValues = new ArrayList<>();
for (final String value : values) {
newValues.add(value.toLowerCase());
}
return newValues;
}
public List<String> collectionToUpperCase(final Collection<String> values) {
final List<String> newValues = new ArrayList<>();
for (final String value : values) {
newValues.add(value.toUpperCase());
}
return newValues;
}
}