/
Helpers.java
174 lines (155 loc) · 4.95 KB
/
Helpers.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.internal.utils;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Objects;
/**
* This class has major inspiration from <a href="https://commons.apache.org/proper/commons-lang/" target="_blank">Lang 3</a>
*
* <p>Specifically StringUtils.java and ExceptionUtils.java
*/
public final class Helpers
{
// ## StringUtils ##
public static boolean isEmpty(final CharSequence seq)
{
return seq == null || seq.length() == 0;
}
public static boolean containsWhitespace(final CharSequence seq)
{
if (isEmpty(seq))
return false;
for (int i = 0; i < seq.length(); i++)
{
if (Character.isWhitespace(seq.charAt(i)))
return true;
}
return false;
}
public static boolean isBlank(final CharSequence seq)
{
if (isEmpty(seq))
return true;
for (int i = 0; i < seq.length(); i++)
{
if (!Character.isWhitespace(seq.charAt(i)))
return false;
}
return true;
}
public static int countMatches(final CharSequence seq, final char c)
{
if (isEmpty(seq))
return 0;
int count = 0;
for (int i = 0; i < seq.length(); i++)
{
if (seq.charAt(i) == c)
count++;
}
return count;
}
public static String truncate(final String input, final int maxWidth)
{
if (input == null)
return null;
Checks.notNegative(maxWidth, "maxWidth");
if (input.length() <= maxWidth)
return input;
if (maxWidth == 0)
return "";
return input.substring(0, maxWidth);
}
public static String rightPad(final String input, final int size)
{
int pads = size - input.length();
if (pads <= 0)
return input;
StringBuilder out = new StringBuilder(input);
for (int i = pads; i > 0; i--)
out.append(' ');
return out.toString();
}
public static String leftPad(final String input, final int size)
{
int pads = size - input.length();
if (pads <= 0)
return input;
StringBuilder out = new StringBuilder();
for (int i = pads; i > 0; i--)
out.append(' ');
return out.append(input).toString();
}
public static boolean isNumeric(final String input)
{
if (isEmpty(input))
return false;
for (char c : input.toCharArray())
{
if (!Character.isDigit(c))
return false;
}
return true;
}
// ## CollectionUtils ##
public static boolean deepEquals(Collection<?> first, Collection<?> second)
{
if (first == second)
return true;
if (first == null || second == null || first.size() != second.size())
return false;
for (Iterator<?> itFirst = first.iterator(), itSecond = second.iterator(); itFirst.hasNext(); )
{
Object elementFirst = itFirst.next();
Object elementSecond = itSecond.next();
if (!Objects.equals(elementFirst, elementSecond))
return false;
}
return true;
}
public static boolean deepEqualsUnordered(Collection<?> first, Collection<?> second)
{
if (first == second) return true;
if (first == null || second == null) return false;
return first.size() == second.size() && second.containsAll(first);
}
public static <E extends Enum<E>> EnumSet<E> copyEnumSet(Class<E> clazz, Collection<E> col)
{
return col.isEmpty() ? EnumSet.noneOf(clazz) : EnumSet.copyOf(col);
}
// ## ExceptionUtils ##
public static <T extends Throwable> T appendCause(T throwable, Throwable cause)
{
Throwable t = throwable;
while (t.getCause() != null)
t = t.getCause();
t.initCause(cause);
return throwable;
}
public static boolean hasCause(Throwable throwable, Class<? extends Throwable> cause)
{
Throwable cursor = throwable;
while (cursor != null)
{
if (cause.isInstance(cursor))
return true;
cursor = cursor.getCause();
}
return false;
}
}