/
Helpers.java
181 lines (159 loc) · 5.11 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
175
176
177
178
179
180
181
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* 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.core.utils;
import org.json.JSONObject;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
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 boolean equalsIgnoreCase(final String seq0, final String seq1)
{
return Objects.equals(seq0, seq1) || (seq0 != null && seq0.equalsIgnoreCase(seq1));
}
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 (input.isEmpty())
return false;
for (char c : input.toCharArray())
{
if (!Character.isDigit(c))
return false;
}
return true;
}
// ## ExceptionUtils ##
//Copied from ogr.apache.commons:commons-lang3:3.5 ExceptionsUtils.java
public static String getStackTrace(final Throwable throwable) {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
throwable.printStackTrace(pw);
return sw.getBuffer().toString();
}
// ## CollectionUtils ##
public static boolean deepEquals(Collection<?> first, Collection<?> second)
{
if (first != null)
{
if (second == null)
return false;
if (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;
}
}
else if (second != null)
{
return false;
}
return true;
}
// ## JSONObject ##
public static boolean optBoolean(JSONObject object, String key)
{
return !object.isNull(key) && object.getBoolean(key);
}
public static int optInt(JSONObject object, String key, int defaultValue)
{
return object.isNull(key) ? defaultValue : object.getInt(key);
}
public static long optLong(JSONObject object, String key, long defaultValue)
{
return object.isNull(key) ? defaultValue : object.getLong(key);
}
}