0
# Utilities
1
2
Utility classes for efficient string operations, specialized sorting algorithms, and other performance-oriented helper functionality.
3
4
## Core Imports
5
6
```java
7
import net.sf.cglib.util.StringSwitcher;
8
import net.sf.cglib.util.ParallelSorter;
9
import net.sf.cglib.util.SorterTemplate;
10
import net.sf.cglib.util.KeyFactory;
11
import java.util.Comparator;
12
import java.util.HashMap;
13
import java.util.Map;
14
```
15
16
## Capabilities
17
18
### StringSwitcher
19
20
Efficient string-based switch statement implementation that generates optimized bytecode for string comparisons, much faster than traditional if-else chains or HashMap lookups for small sets of strings.
21
22
```java { .api }
23
/**
24
* Efficient string-based switch statement implementation
25
*/
26
public abstract class StringSwitcher {
27
/**
28
* Create StringSwitcher for given strings and corresponding integer values
29
* @param strings - Array of strings to match
30
* @param ints - Array of integers corresponding to each string
31
* @param fixedInput - Whether input strings are guaranteed to be from the strings array
32
* @return StringSwitcher instance
33
*/
34
public static StringSwitcher create(String[] strings, int[] ints, boolean fixedInput);
35
36
/**
37
* Get integer value for given string
38
* @param s - String to lookup
39
* @return Corresponding integer value, or -1 if not found
40
*/
41
public abstract int intValue(String s);
42
}
43
```
44
45
**Usage Examples:**
46
47
```java
48
import net.sf.cglib.util.StringSwitcher;
49
50
// Create string switcher for HTTP methods
51
String[] methods = {"GET", "POST", "PUT", "DELETE", "PATCH"};
52
int[] values = {1, 2, 3, 4, 5};
53
54
StringSwitcher switcher = StringSwitcher.create(methods, values, false);
55
56
// Use in switch-like logic (much faster than string comparison chains)
57
String httpMethod = "POST";
58
int methodCode = switcher.intValue(httpMethod); // Returns 2
59
60
switch (methodCode) {
61
case 1:
62
System.out.println("Handle GET request");
63
break;
64
case 2:
65
System.out.println("Handle POST request");
66
break;
67
case 3:
68
System.out.println("Handle PUT request");
69
break;
70
case 4:
71
System.out.println("Handle DELETE request");
72
break;
73
case 5:
74
System.out.println("Handle PATCH request");
75
break;
76
default:
77
System.out.println("Unknown method");
78
}
79
80
// Fixed input version (slightly more optimized when input is guaranteed to be from the array)
81
StringSwitcher fixedSwitcher = StringSwitcher.create(methods, values, true);
82
```
83
84
### ParallelSorter
85
86
Sorts multiple arrays in parallel based on the order of one primary array, maintaining correspondence between array elements across all arrays.
87
88
```java { .api }
89
/**
90
* Sorts multiple arrays in parallel based on one array's values
91
*/
92
public class ParallelSorter {
93
/**
94
* Sort two arrays in parallel based on first array's order
95
* @param a - Primary array to sort by
96
* @param b - Secondary array to rearrange in parallel
97
*/
98
public static void sort(Object[] a, Object[] b);
99
100
/**
101
* Sort multiple arrays in parallel based on first array's order
102
* @param arrays - Arrays to sort, first array determines sort order
103
*/
104
public static void sort(Object[][] arrays);
105
106
/**
107
* Sort with custom comparator
108
* @param a - Primary array to sort by
109
* @param b - Secondary array to rearrange in parallel
110
* @param cmp - Comparator for primary array
111
*/
112
public static void sort(Object[] a, Object[] b, Comparator cmp);
113
114
/**
115
* Sort multiple arrays with custom comparator
116
* @param arrays - Arrays to sort, first array determines sort order
117
* @param cmp - Comparator for primary array
118
*/
119
public static void sort(Object[][] arrays, Comparator cmp);
120
}
121
```
122
123
**Usage Examples:**
124
125
```java
126
import net.sf.cglib.util.ParallelSorter;
127
import java.util.Arrays;
128
129
// Sort students by age while keeping names in sync
130
String[] names = {"Alice", "Bob", "Charlie", "Diana"};
131
Integer[] ages = {25, 19, 30, 22};
132
133
// Sort by ages (primary array), names will be rearranged to match
134
ParallelSorter.sort(ages, names);
135
136
// Result: ages = [19, 22, 25, 30], names = ["Bob", "Diana", "Alice", "Charlie"]
137
System.out.println("Sorted ages: " + Arrays.toString(ages));
138
System.out.println("Corresponding names: " + Arrays.toString(names));
139
140
// Multiple arrays example
141
String[] firstNames = {"John", "Jane", "Bob", "Alice"};
142
String[] lastNames = {"Doe", "Smith", "Johnson", "Wilson"};
143
Integer[] scores = {85, 92, 78, 95};
144
145
// Sort by scores, keeping first and last names in sync
146
Object[][] data = {scores, firstNames, lastNames};
147
ParallelSorter.sort(data);
148
149
// With custom comparator (reverse order)
150
ParallelSorter.sort(ages, names, (a, b) -> ((Integer)b).compareTo((Integer)a));
151
```
152
153
### SorterTemplate
154
155
Abstract template for implementing custom parallel sorting algorithms.
156
157
```java { .api }
158
/**
159
* Abstract template for custom parallel sorting algorithms
160
*/
161
public abstract class SorterTemplate {
162
/**
163
* Perform the sorting operation
164
* @param from - Start index (inclusive)
165
* @param to - End index (exclusive)
166
*/
167
public final void quickSort(int from, int to);
168
169
/**
170
* Merge sort implementation
171
* @param from - Start index (inclusive)
172
* @param to - End index (exclusive)
173
*/
174
public final void mergeSort(int from, int to);
175
176
/**
177
* Compare elements at two indices
178
* @param i - First index
179
* @param j - Second index
180
* @return Negative if i < j, zero if i == j, positive if i > j
181
*/
182
protected abstract int compare(int i, int j);
183
184
/**
185
* Swap elements at two indices
186
* @param i - First index
187
* @param j - Second index
188
*/
189
protected abstract void swap(int i, int j);
190
191
/**
192
* Get length of data to sort
193
* @return Data length
194
*/
195
protected abstract int length();
196
}
197
```
198
199
### KeyFactory
200
201
Generates efficient key objects for use in caching and hash-based operations.
202
203
```java { .api }
204
/**
205
* Generates efficient key objects for caching and hash operations
206
*/
207
public abstract class KeyFactory {
208
/**
209
* Create KeyFactory for given interface
210
* @param keyInterface - Interface defining key methods
211
* @return KeyFactory instance
212
*/
213
public static KeyFactory create(Class keyInterface);
214
215
/**
216
* Create KeyFactory with custom ClassLoader
217
* @param loader - ClassLoader to use
218
* @param keyInterface - Interface defining key methods
219
* @return KeyFactory instance
220
*/
221
public static KeyFactory create(ClassLoader loader, Class keyInterface);
222
223
/**
224
* Create new key instance
225
* @return Key object implementing the key interface
226
*/
227
public abstract Object newInstance();
228
}
229
```
230
231
**Usage Examples:**
232
233
```java
234
import net.sf.cglib.util.KeyFactory;
235
236
// Define key interface
237
interface CacheKey {
238
void setUserId(int userId);
239
void setTimestamp(long timestamp);
240
void setType(String type);
241
}
242
243
// Create key factory
244
KeyFactory factory = KeyFactory.create(CacheKey.class);
245
246
// Create efficient key objects for caching
247
CacheKey key1 = (CacheKey) factory.newInstance();
248
key1.setUserId(123);
249
key1.setTimestamp(System.currentTimeMillis());
250
key1.setType("user-profile");
251
252
CacheKey key2 = (CacheKey) factory.newInstance();
253
key2.setUserId(456);
254
key2.setTimestamp(System.currentTimeMillis());
255
key2.setType("user-settings");
256
257
// Keys properly implement equals() and hashCode() for efficient caching
258
Map<Object, Object> cache = new HashMap<>();
259
cache.put(key1, "profile data");
260
cache.put(key2, "settings data");
261
```