Skip to content

Commit 5bbcc43

Browse files
Cleanup Helpers utility (discord-jda#1045)
1 parent 3add329 commit 5bbcc43

File tree

2 files changed

+130
-52
lines changed

2 files changed

+130
-52
lines changed

Diff for: src/main/java/net/dv8tion/jda/internal/utils/Helpers.java

+9-52
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package net.dv8tion.jda.internal.utils;
1818

19-
import net.dv8tion.jda.api.utils.data.DataObject;
20-
21-
import java.io.PrintWriter;
22-
import java.io.StringWriter;
2319
import java.util.Collection;
2420
import java.util.Iterator;
2521
import java.util.Objects;
@@ -63,11 +59,6 @@ public static boolean isBlank(final CharSequence seq)
6359
return true;
6460
}
6561

66-
public static boolean equalsIgnoreCase(final String seq0, final String seq1)
67-
{
68-
return Objects.equals(seq0, seq1) || (seq0 != null && seq0.equalsIgnoreCase(seq1));
69-
}
70-
7162
public static int countMatches(final CharSequence seq, final char c)
7263
{
7364
if (isEmpty(seq))
@@ -117,7 +108,7 @@ public static String leftPad(final String input, final int size)
117108

118109
public static boolean isNumeric(final String input)
119110
{
120-
if (input.isEmpty())
111+
if (isEmpty(input))
121112
return false;
122113
for (char c : input.toCharArray())
123114
{
@@ -127,37 +118,20 @@ public static boolean isNumeric(final String input)
127118
return true;
128119
}
129120

130-
// ## ExceptionUtils ##
131-
132-
//Copied from ogr.apache.commons:commons-lang3:3.5 ExceptionsUtils.java
133-
public static String getStackTrace(final Throwable throwable) {
134-
final StringWriter sw = new StringWriter();
135-
final PrintWriter pw = new PrintWriter(sw, true);
136-
throwable.printStackTrace(pw);
137-
return sw.getBuffer().toString();
138-
}
139-
140121
// ## CollectionUtils ##
141122

142123
public static boolean deepEquals(Collection<?> first, Collection<?> second)
143124
{
144-
if (first != null)
125+
if (first == second)
126+
return true;
127+
if (first == null || second == null || first.size() != second.size())
128+
return false;
129+
for (Iterator<?> itFirst = first.iterator(), itSecond = second.iterator(); itFirst.hasNext(); )
145130
{
146-
if (second == null)
131+
Object elementFirst = itFirst.next();
132+
Object elementSecond = itSecond.next();
133+
if (!Objects.equals(elementFirst, elementSecond))
147134
return false;
148-
if (first.size() != second.size())
149-
return false;
150-
for (Iterator<?> itFirst = first.iterator(), itSecond = second.iterator(); itFirst.hasNext(); )
151-
{
152-
Object elementFirst = itFirst.next();
153-
Object elementSecond = itSecond.next();
154-
if (!Objects.equals(elementFirst, elementSecond))
155-
return false;
156-
}
157-
}
158-
else if (second != null)
159-
{
160-
return false;
161135
}
162136
return true;
163137
}
@@ -168,21 +142,4 @@ public static boolean deepEqualsUnordered(Collection<?> first, Collection<?> sec
168142
if (first == null || second == null) return false;
169143
return first.size() == second.size() && second.containsAll(first);
170144
}
171-
172-
// ## JSONObject ##
173-
174-
public static boolean optBoolean(DataObject object, String key)
175-
{
176-
return !object.isNull(key) && object.getBoolean(key);
177-
}
178-
179-
public static int optInt(DataObject object, String key, int defaultValue)
180-
{
181-
return object.isNull(key) ? defaultValue : object.getInt(key);
182-
}
183-
184-
public static long optLong(DataObject object, String key, long defaultValue)
185-
{
186-
return object.isNull(key) ? defaultValue : object.getLong(key);
187-
}
188145
}

Diff for: src/test/java/HelpersTest.java

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2015-2019 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import net.dv8tion.jda.internal.utils.Helpers;
18+
import org.junit.jupiter.api.Assertions;
19+
import org.junit.jupiter.api.Test;
20+
21+
import java.util.Arrays;
22+
import java.util.List;
23+
24+
public class HelpersTest
25+
{
26+
@Test
27+
public void testIsEmpty()
28+
{
29+
Assertions.assertTrue(Helpers.isEmpty(null));
30+
Assertions.assertTrue(Helpers.isEmpty(""));
31+
Assertions.assertFalse(Helpers.isEmpty("null"));
32+
Assertions.assertFalse(Helpers.isEmpty("testing with spaces"));
33+
}
34+
35+
@Test
36+
public void testContainsWhitespace()
37+
{
38+
Assertions.assertTrue(Helpers.containsWhitespace(" "));
39+
Assertions.assertTrue(Helpers.containsWhitespace("testing with spaces"));
40+
Assertions.assertFalse(Helpers.containsWhitespace(null));
41+
Assertions.assertFalse(Helpers.containsWhitespace(""));
42+
Assertions.assertFalse(Helpers.containsWhitespace("null"));
43+
}
44+
45+
@Test
46+
public void testIsBlank()
47+
{
48+
Assertions.assertTrue(Helpers.isBlank(" "));
49+
Assertions.assertTrue(Helpers.isBlank(null));
50+
Assertions.assertTrue(Helpers.isBlank(""));
51+
Assertions.assertFalse(Helpers.isBlank("testing with spaces"));
52+
Assertions.assertFalse(Helpers.isBlank("null"));
53+
}
54+
55+
@Test
56+
public void testCountMatches()
57+
{
58+
Assertions.assertEquals(3, Helpers.countMatches("Hello World", 'l'));
59+
Assertions.assertEquals(1, Helpers.countMatches("Hello World", ' '));
60+
Assertions.assertEquals(0, Helpers.countMatches("Hello World", '_'));
61+
Assertions.assertEquals(0, Helpers.countMatches("", '!'));
62+
Assertions.assertEquals(0, Helpers.countMatches(null, '?'));
63+
}
64+
65+
@Test
66+
public void testTruncate()
67+
{
68+
Assertions.assertEquals("Hello", Helpers.truncate("Hello World", 5));
69+
Assertions.assertEquals("Hello", Helpers.truncate("Hello", 5));
70+
Assertions.assertEquals("Hello", Helpers.truncate("Hello", 10));
71+
Assertions.assertEquals("", Helpers.truncate("", 10));
72+
Assertions.assertEquals("", Helpers.truncate("Test", 0));
73+
Assertions.assertNull(Helpers.truncate(null, 10));
74+
}
75+
76+
@Test
77+
public void testRightPad()
78+
{
79+
Assertions.assertEquals("Hello ", Helpers.rightPad("Hello", 9));
80+
Assertions.assertEquals("Hello World", Helpers.rightPad("Hello World", 9));
81+
Assertions.assertEquals("Hello", Helpers.rightPad("Hello", 5));
82+
}
83+
84+
@Test
85+
public void testLeftPad()
86+
{
87+
Assertions.assertEquals(" Hello", Helpers.leftPad("Hello", 9));
88+
Assertions.assertEquals("Hello World", Helpers.leftPad("Hello World", 9));
89+
Assertions.assertEquals("Hello", Helpers.leftPad("Hello", 5));
90+
}
91+
92+
@Test
93+
public void testIsNumeric()
94+
{
95+
Assertions.assertTrue(Helpers.isNumeric("10"));
96+
Assertions.assertTrue(Helpers.isNumeric("1"));
97+
Assertions.assertTrue(Helpers.isNumeric("0"));
98+
Assertions.assertTrue(Helpers.isNumeric(String.valueOf(Long.MAX_VALUE)));
99+
Assertions.assertFalse(Helpers.isNumeric(null));
100+
Assertions.assertFalse(Helpers.isNumeric(""));
101+
Assertions.assertFalse(Helpers.isNumeric("Test"));
102+
Assertions.assertFalse(Helpers.isNumeric("1.0"));
103+
Assertions.assertFalse(Helpers.isNumeric("1e10"));
104+
}
105+
106+
@Test
107+
public void testDeepEquals()
108+
{
109+
List<String> a = Arrays.asList("A", "B", "C");
110+
List<String> b = Arrays.asList("B", "A", "C");
111+
List<String> c = Arrays.asList("A", "B");
112+
List<String> d = Arrays.asList("A", "B", "C");
113+
114+
Assertions.assertTrue(Helpers.deepEquals(a, a));
115+
Assertions.assertTrue(Helpers.deepEquals(a, d));
116+
Assertions.assertTrue(Helpers.deepEqualsUnordered(a, b));
117+
Assertions.assertFalse(Helpers.deepEquals(a, b));
118+
Assertions.assertFalse(Helpers.deepEquals(a, c));
119+
Assertions.assertFalse(Helpers.deepEqualsUnordered(b, c));
120+
}
121+
}

0 commit comments

Comments
 (0)