Skip to content

Commit 27879e0

Browse files
committed
Made CaseMapping an interface
1 parent a469fc5 commit 27879e0

File tree

3 files changed

+56
-66
lines changed

3 files changed

+56
-66
lines changed

src/main/java/org/kitteh/irc/client/library/feature/CaseMapping.java

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,63 +24,49 @@
2424
package org.kitteh.irc.client.library.feature;
2525

2626
import org.jspecify.annotations.NonNull;
27+
import org.jspecify.annotations.NullMarked;
2728
import org.jspecify.annotations.Nullable;
2829
import org.kitteh.irc.client.library.util.Sanity;
2930

3031
import java.util.HashMap;
32+
import java.util.Locale;
3133
import java.util.Map;
3234
import java.util.Optional;
3335

3436
/**
3537
* ISUPPORT CASEMAPPING.
3638
*/
37-
public enum CaseMapping {
39+
@NullMarked
40+
public interface CaseMapping {
41+
record Ranged(char upperbound) implements CaseMapping {
42+
@Override
43+
public String toLowerCase(String input) {
44+
Sanity.nullCheck(input, "Input");
45+
char[] arr = input.toCharArray();
46+
for (int i = 0; i < arr.length; i++) {
47+
char c = arr[i];
48+
if ((c >= 'A') && (c <= this.upperbound)) {
49+
arr[i] += (char) 32;
50+
}
51+
}
52+
return new String(arr);
53+
}
54+
}
55+
3856
/**
3957
* A-Z become a-z
4058
*/
41-
ASCII('Z'),
59+
CaseMapping.Ranged ASCII = new CaseMapping.Ranged('Z');
4260
/**
4361
* A-Z become a-z, []^ become {}~
4462
*/
45-
RFC1459('^'),
63+
CaseMapping.Ranged RFC1459 = new CaseMapping.Ranged('^');
4664
/**
4765
* A-Z become a-z, [] become {}
4866
*/
49-
STRICT_RFC1459(']');
50-
51-
private static final Map<String, CaseMapping> nameMap = new HashMap<>();
52-
53-
static {
54-
for (CaseMapping caseMapping : CaseMapping.values()) {
55-
CaseMapping.nameMap.put(caseMapping.name().replace('_', '-'), caseMapping);
56-
}
57-
}
58-
59-
/**
60-
* Gets a CaseMapping by name. Case insensitive.
61-
*
62-
* @param name the name of the CaseMapping to get
63-
* @return the matching CaseMapping if one exists
64-
*/
65-
public static @NonNull Optional<CaseMapping> getByName(@Nullable String name) {
66-
return (name == null) ? Optional.empty() : Optional.ofNullable(CaseMapping.nameMap.get(name.toUpperCase()));
67-
}
68-
69-
private final char upperbound;
70-
71-
CaseMapping(char upperbound) {
72-
this.upperbound = upperbound;
73-
}
67+
CaseMapping.Ranged STRICT_RFC1459 = new CaseMapping.Ranged(']');
7468

75-
/**
76-
* Gets if two given strings are equal, case insensitive, using this
77-
* case mapping.
78-
*
79-
* @param one one string
80-
* @param two two string, red string, blue string
81-
* @return true if equal ignoring case using this case mapping
82-
*/
83-
public boolean areEqualIgnoringCase(@NonNull String one, @NonNull String two) {
69+
default boolean areEqualIgnoringCase(String one,String two) {
8470
return this.toLowerCase(one).equals(this.toLowerCase(two));
8571
}
8672

@@ -91,15 +77,23 @@ public boolean areEqualIgnoringCase(@NonNull String one, @NonNull String two) {
9177
* @return lowercased string
9278
* @throws IllegalArgumentException if input is null
9379
*/
94-
public @NonNull String toLowerCase(@NonNull String input) {
95-
Sanity.nullCheck(input, "Input");
96-
char[] arr = input.toCharArray();
97-
for (int i = 0; i < arr.length; i++) {
98-
char c = arr[i];
99-
if ((c >= 'A') && (c <= this.upperbound)) {
100-
arr[i] += (char) 32;
101-
}
80+
String toLowerCase(String input);
81+
82+
/**
83+
* Gets a CaseMapping by name, case-insensitive.
84+
*
85+
* @param name the name of the CaseMapping to get
86+
* @return the matching CaseMapping if one exists
87+
*/
88+
static @NonNull Optional<CaseMapping> getByName(@Nullable String name) {
89+
if (name == null) {
90+
return Optional.empty();
10291
}
103-
return new String(arr);
92+
return Optional.ofNullable(switch (name.toUpperCase(Locale.ROOT)) {
93+
case "ASCII" -> ASCII;
94+
case "RFC1459" -> RFC1459;
95+
case "STRICT-RFC1459" -> STRICT_RFC1459;
96+
default -> null;
97+
});
10498
}
10599
}

src/test/java/org/kitteh/irc/client/library/CaseMappingTest.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,36 @@
55
import org.kitteh.irc.client.library.feature.CaseMapping;
66
import org.kitteh.irc.client.library.util.Pair;
77

8-
import java.util.EnumMap;
8+
import java.lang.reflect.Modifier;
9+
import java.util.Arrays;
10+
import java.util.HashMap;
911
import java.util.Map;
10-
import java.util.Optional;
1112

1213
/**
1314
* Tests case mappings.
1415
*/
1516
public class CaseMappingTest {
16-
/**
17-
* Verifies all casemapping enums match names.
18-
*/
19-
@Test
20-
public void verifyMatch() {
21-
for (CaseMapping caseMapping : CaseMapping.values()) {
22-
Optional<CaseMapping> acquired = CaseMapping.getByName(caseMapping.name().replace('_', '-'));
23-
Assertions.assertTrue(acquired.isPresent(), "Failed to acquire mapping for " + caseMapping.name());
24-
Assertions.assertEquals(caseMapping, acquired.get());
25-
}
26-
Assertions.assertEquals(Optional.empty(), CaseMapping.getByName(null));
27-
}
28-
2917
/**
3018
* Tests casemapping lowercasing.
3119
*/
3220
@Test
3321
public void lowerCase() {
34-
Map<CaseMapping, Pair<String, String>> test = new EnumMap<>(CaseMapping.class);
22+
Map<CaseMapping, Pair<String, String>> test = new HashMap<>();
3523
test.put(CaseMapping.ASCII, new Pair<>("abcdwxyzABCDWXYZ!@#$%^&*(){}[];':,.<>", "abcdwxyzabcdwxyz!@#$%^&*(){}[];':,.<>"));
3624
test.put(CaseMapping.RFC1459, new Pair<>("abcdwxyzABCDWXYZ!@#$%^&*(){}[];':,.<>", "abcdwxyzabcdwxyz!@#$%~&*(){}{};':,.<>"));
3725
test.put(CaseMapping.STRICT_RFC1459, new Pair<>("abcdwxyzABCDWXYZ!@#$%^&*(){}[];':,.<>", "abcdwxyzabcdwxyz!@#$%^&*(){}{};':,.<>"));
3826

39-
for (CaseMapping caseMapping : CaseMapping.values()) {
40-
Assertions.assertTrue(test.containsKey(caseMapping), "Missing CaseMapping " + caseMapping.name());
41-
}
27+
Arrays.stream(CaseMapping.class.getDeclaredFields())
28+
.filter(field -> Modifier.isPublic(field.getModifiers()) && CaseMapping.class.isAssignableFrom(field.getType()))
29+
.forEach(field -> {
30+
try {
31+
Assertions.assertTrue(test.containsKey((CaseMapping) field.get(null)), "Missing CaseMapping " + field.getName());
32+
} catch (IllegalAccessException e) {
33+
throw new RuntimeException(e);
34+
}
35+
}
36+
);
37+
4238
for (Map.Entry<CaseMapping, Pair<String, String>> entry : test.entrySet()) {
4339
Assertions.assertEquals(entry.getKey().toLowerCase(entry.getValue().getLeft()), entry.getValue().getRight(), "Incorrect lowercasing");
4440
Assertions.assertTrue(entry.getKey().areEqualIgnoringCase(entry.getValue().getLeft(), entry.getValue().getRight()), "Incorrect equalsIgnoreCase");

src/test/java/org/kitteh/irc/client/library/defaults/feature/ISupportManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testParam() {
3838
@Test
3939
public void casemapping() {
4040
DefaultISupportManager manager = this.getManager();
41-
ISupportParameter param = manager.createParameter(ISupportParameter.CaseMapping.NAME + '=' + CaseMapping.RFC1459.name());
41+
ISupportParameter param = manager.createParameter(ISupportParameter.CaseMapping.NAME + "=RFC1459");
4242
Assertions.assertTrue(ISupportParameter.CaseMapping.class.isAssignableFrom(param.getClass()));
4343
Assertions.assertEquals(CaseMapping.RFC1459, ((ISupportParameter.CaseMapping) param).getCaseMapping());
4444
}

0 commit comments

Comments
 (0)