forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PriorityItemDisplayNameShortener.java
104 lines (81 loc) · 3.29 KB
/
PriorityItemDisplayNameShortener.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
package com.Acrobot.ChestShop.ItemNaming;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import com.Acrobot.Breeze.Utils.StringUtil;
public class PriorityItemDisplayNameShortener implements ItemDisplayNameShortener {
private final Map<Integer, StringShortenerPairHolder> pairHolderMap;
public PriorityItemDisplayNameShortener() {
pairHolderMap = new TreeMap<>();
}
/**
* Adds a mapping from a 'from' string to a 'to' string with a specified priority.
* If the priority already exists in the map, the mapping is added to the existing priority level.
* The lowest priority will be mapped first
*
* @param priority
* the priority level of the mapping
* @param from
* the string to be replaced
* @param to
* the replacement string
*/
public void addMapping(Integer priority, String from, String to) {
StringShortenerPairHolder pairHolder;
if (!pairHolderMap.containsKey(priority)) {
pairHolder = new StringShortenerPairHolder();
pairHolderMap.put(priority, pairHolder);
} else
pairHolder = pairHolderMap.get(priority);
pairHolder.addShortenerPair(from.toLowerCase(), to.toLowerCase());
}
@Override
public String shorten(String string, int length) {
if (string.length() <= length)
return string;
String result = string;
String strippedResult = StringUtil.stripWhitespaces(result);
if (strippedResult.length() <= length)
return strippedResult;
for (Map.Entry<Integer, StringShortenerPairHolder> integerStringShortenerPairHolderEntry : pairHolderMap.entrySet()) {
StringShortenerPairHolder value = integerStringShortenerPairHolderEntry.getValue();
result = value.apply(result);
if (result.length() <= length)
return StringUtil.capitalizeFirstLetter(result);
strippedResult = StringUtil.stripWhitespaces(result);
if (strippedResult.length() <= length)
return strippedResult;
}
return StringUtil.capitalizeFirstLetter(result);
}
private static class StringShortenerPairHolder {
private final Set<StringShortenerPair> shortenerPairs;
public StringShortenerPairHolder() {
this.shortenerPairs = new TreeSet<>(StringShortenerPair::compareTo);
}
public void addShortenerPair(String from, String to) {
shortenerPairs.add(new StringShortenerPair(from, to));
}
public String apply(String string) {
String result = string;
for (StringShortenerPair pair : shortenerPairs) {
result = pair.apply(result);
}
return result;
}
}
private record StringShortenerPair(String from, String to) implements Comparable<StringShortenerPair> {
public String apply(String string) {
return string.replaceAll("(?<=( |^))" + from + "(?=( |$))", to);
}
@Override
public int compareTo(StringShortenerPair o) {
if (equals(o))
return 0;
if (o.to.equals(from))
return -1;
return 1;
}
}
}