RuneHive-Game
Loading...
Searching...
No Matches
StringUtils.java
Go to the documentation of this file.
1package com.runehive.util;
2
3import com.runehive.fs.cache.FileSystem;
4
5import java.text.NumberFormat;
6import java.util.stream.IntStream;
7
8public class StringUtils {
9
10 /** An array containing valid username characters. */
11 private static final char VALID_USERNAME_CHARACTERS[] = {
12 '_',
13 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
14 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
15 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
16 };
17
18 public static String formatPrice(long amount) {
19 if (amount >= 1_000 && amount < 1_000_000) {
20 return (amount / 1_000) + "K";
21 } else if (amount >= 1_000_000 && amount < 1_000_000_000) {
22 return (amount / 1_000_000) + "M";
23 } else if (amount >= 1_000_000_000) {
24 return (amount / 1_000_000_000) + "B";
25 }
26 return "" + amount;
27 }
28
29 public static int convertAmount(String input) {
30 try {
31 input = input.toLowerCase().replace(" ", "");
32
33 int pos = input.indexOf('k');
34
35 if (pos != -1) {
36 return Integer.parseInt(input.substring(0, pos)) * 1_000;
37 }
38
39 pos = input.indexOf('m');
40
41 if (pos != -1) {
42 return Integer.parseInt(input.substring(0, pos)) * 1_000_000;
43 }
44
45 pos = input.indexOf('b');
46
47 if (pos != -1) {
48 return Integer.parseInt(input.substring(0, pos)) * 1_000_000_000;
49 }
50
51 } catch (Exception ex) {
52
53 }
54 return 0;
55 }
56
57 /**
58 * Determines the indefinite article of {@code thing}.
59 *
60 * @param thing the thing to determine for.
61 * @return the indefinite article.
62 */
63 private static String determineIndefiniteArticle(String thing) {
64 char first = thing.toLowerCase().charAt(0);
65 boolean vowel = first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u';
66 return vowel ? "an" : "a";
67 }
68
69 /**
70 * Determines the plural check of {@code thing}.
71 *
72 * @param thing the thing to determine for.
73 * @return the plural check.
74 */
75 private static String determinePluralCheck(String thing) {
76 boolean needsPlural = !thing.endsWith("s") && !thing.endsWith(")");
77 return needsPlural ? "s" : "";
78 }
79
80 /**
81 * Appends the determined plural check to {@code thing}.
82 *
83 * @param thing the thing to append.
84 * @return the {@code thing} after the plural check has been appended.
85 */
86 public static String appendPluralCheck(String thing) {
87 return thing.concat(determinePluralCheck(thing));
88 }
89
90 /**
91 * Appends the determined indefinite article to {@code thing}.
92 *
93 * @param thing the thing to append.
94 * @return the {@code thing} after the indefinite article has been appended.
95 */
96 public static String appendIndefiniteArticle(String thing) {
97 return determineIndefiniteArticle(thing).concat(" " + thing);
98 }
99
100 public static String capitalize(String string) {
101 return Character.toUpperCase(string.charAt(0)) + string.substring(1);
102 }
103
104 public static String format(int num) {
105 return NumberFormat.getInstance().format(num);
106 }
107
108 public static String formatText(String s) {
109 for (int i = 0; i < s.length(); i++) {
110 if (i == 0) {
111 s = String.format("%s%s", Character.toUpperCase(s.charAt(0)), s.substring(1));
112 }
113 if (!Character.isLetterOrDigit(s.charAt(i))) {
114 if (i + 1 < s.length()) {
115 s = String.format("%s%s%s", s.subSequence(0, i + 1), Character.toUpperCase(s.charAt(i + 1)), s.substring(i + 2));
116 }
117 }
118 }
119 return s.replace("_", " ");
120 }
121
122 public static String getAOrAn(String nextWord) {
123 String s = "a";
124 final char c = nextWord.toUpperCase().charAt(0);
125 if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
126 s = "an";
127 }
128 return s;
129 }
130
131 public static String hashToString(long input) {
132 int length = 12;
133 char decoded[] = new char[length];
134
135 while (input != 0) {
136 long hash = input;
137 input /= 37;
138 int index = (int) (hash - input * 37);
139 decoded[length--] = VALID_USERNAME_CHARACTERS[index];
140 }
141
142 return new String(decoded, length, decoded.length - length);
143 }
144
145 /**
146 * Hashes a given string input.
147 *
148 * @param input The string to hash.
149 * @return A hash as a {@code long}.
150 */
151 public static long hash(String input) {
152 long hash = 0;
153
154 for (int index = 0; index < input.length(); index++) {
155 char key = input.charAt(index);
156
157 hash *= 37;
158
159 if (key >= 'A' && key <= 'Z') {
160 hash += (1 + key) - 65;
161 } else if (key >= 'a' && key <= 'z') {
162 hash += (1 + key) - 97;
163 } else if (key >= '0' && key <= '9') {
164 hash += (27 + key) - 48;
165 }
166 }
167
168 while (hash % 37 == 0 && hash != 0) hash /= 37;
169 return hash;
170 }
171
172 /**
173 * Hashes a {@code String} using Jagex's algorithm, this method should be
174 * used to convert actual names to hashed names to lookup files within the
175 * {@link FileSystem}.
176 * @param string The string to hash.
177 * @return The hashed string.
178 */
179 public static int hashArchive(String string) {
180 return _hash(string.toUpperCase());
181 }
182
183 /**
184 * Hashes a {@code String} using Jagex's algorithm, this method should be
185 * used to convert actual names to hashed names to lookup files within the
186 * {@link FileSystem}.
187 * <p>
188 * <p>
189 * This method should <i>only</i> be used internally, it is marked
190 * deprecated as it does not properly hash the specified {@code String}. The
191 * functionality of this method is used to register a proper {@code String}
192 * {@link #hash(String) <i>hashing method</i>}. The scope of this method has
193 * been marked as {@code private} to prevent confusion.
194 * </p>
195 * @param string The string to hash.
196 * @return The hashed string.
197 * @deprecated This method should only be used internally as it does not
198 * correctly hash the specified {@code String}. See the note
199 * below for more information.
200 */
201 @Deprecated
202 private static int _hash(String string) {
203 return IntStream.range(0, string.length()).reduce(0, (hash, index) -> hash * 61 + string.charAt(index) - 32);
204 }
205
206}
static String getAOrAn(String nextWord)
static String formatPrice(long amount)
static String formatText(String s)
static String capitalize(String string)
static int convertAmount(String input)
static String appendPluralCheck(String thing)
Appends the determined plural check to thing.
static String appendIndefiniteArticle(String thing)
Appends the determined indefinite article to thing.
static int hashArchive(String string)
Hashes a String using Jagex's algorithm, this method should be used to convert actual names to hashed...
static final char VALID_USERNAME_CHARACTERS[]
An array containing valid username characters.
static long hash(String input)
Hashes a given string input.
static String determinePluralCheck(String thing)
Determines the plural check of thing.
static String format(int num)
static int _hash(String string)
Hashes a String using Jagex's algorithm, this method should be used to convert actual names to hashed...
static String determineIndefiniteArticle(String thing)
Determines the indefinite article of thing.
static String hashToString(long input)