0
# Utility Classes
1
2
General-purpose utility classes from the `net.sf.cglib.util` package that provide high-performance alternatives to common operations like string mapping and parallel array sorting.
3
4
## Core Imports
5
6
```java
7
import net.sf.cglib.util.StringSwitcher;
8
import net.sf.cglib.util.ParallelSorter;
9
```
10
11
## Capabilities
12
13
### StringSwitcher
14
15
Efficient string-to-integer mapping, providing a fast alternative to large switch statements or Map lookups.
16
17
```java { .api }
18
/**
19
* Efficient string-to-int mapping (switch statement replacement)
20
*/
21
public abstract class StringSwitcher {
22
/**
23
* Create string switcher
24
* @param strings Array of string values
25
* @param ints Array of corresponding integer values
26
* @param fixedInput Whether input strings are fixed (enables optimizations)
27
* @return StringSwitcher instance
28
*/
29
public static StringSwitcher create(String[] strings, int[] ints, boolean fixedInput);
30
31
/**
32
* Get integer value for string
33
* @param s Input string
34
* @return Corresponding integer value, or -1 if not found
35
*/
36
public abstract int intValue(String s);
37
}
38
```
39
40
**Usage Examples:**
41
42
```java
43
// Create string-to-int mapping
44
String[] operations = {"CREATE", "READ", "UPDATE", "DELETE"};
45
int[] opcodes = {1, 2, 3, 4};
46
47
StringSwitcher switcher = StringSwitcher.create(operations, opcodes, true);
48
49
// Use switcher (much faster than Map or if-else chains)
50
int createOp = switcher.intValue("CREATE"); // 1
51
int readOp = switcher.intValue("READ"); // 2
52
int updateOp = switcher.intValue("UPDATE"); // 3
53
int deleteOp = switcher.intValue("DELETE"); // 4
54
int unknown = switcher.intValue("UNKNOWN"); // -1
55
56
// Use in processing loop
57
List<String> commands = getCommands();
58
for (String command : commands) {
59
switch (switcher.intValue(command)) {
60
case 1: // CREATE
61
handleCreate();
62
break;
63
case 2: // READ
64
handleRead();
65
break;
66
case 3: // UPDATE
67
handleUpdate();
68
break;
69
case 4: // DELETE
70
handleDelete();
71
break;
72
default:
73
handleUnknown(command);
74
}
75
}
76
77
// HTTP status code mapping
78
String[] statusNames = {"OK", "NOT_FOUND", "SERVER_ERROR", "BAD_REQUEST"};
79
int[] statusCodes = {200, 404, 500, 400};
80
StringSwitcher statusSwitcher = StringSwitcher.create(statusNames, statusCodes, true);
81
82
int code = statusSwitcher.intValue("NOT_FOUND"); // 404
83
```
84
85
### ParallelSorter
86
87
Utility for parallel sorting of multiple arrays, keeping corresponding elements aligned.
88
89
```java { .api }
90
/**
91
* Parallel sorting of multiple arrays
92
*/
93
public abstract class ParallelSorter {
94
/**
95
* Create parallel sorter for arrays
96
* @param arrays Arrays to sort in parallel
97
* @return ParallelSorter instance
98
*/
99
public static ParallelSorter create(Object[] arrays);
100
101
/**
102
* Quick sort using array at index as sort key
103
* @param index Index of array to use as sort key
104
*/
105
public abstract void quickSort(int index);
106
107
/**
108
* Merge sort using array at index as sort key
109
* @param index Index of array to use as sort key
110
*/
111
public abstract void mergeSort(int index);
112
113
/**
114
* Heap sort using array at index as sort key
115
* @param index Index of array to use as sort key
116
*/
117
public abstract void heapSort(int index);
118
}
119
```
120
121
**Usage Examples:**
122
123
```java
124
// Create parallel arrays
125
String[] names = {"Charlie", "Alice", "Bob", "David"};
126
Integer[] ages = {30, 25, 35, 28};
127
String[] cities = {"NYC", "LA", "Chicago", "Boston"};
128
129
// Create parallel sorter
130
ParallelSorter sorter = ParallelSorter.create(new Object[]{names, ages, cities});
131
132
// Sort by names (index 0)
133
sorter.quickSort(0);
134
// Now: names = ["Alice", "Bob", "Charlie", "David"]
135
// ages = [25, 35, 30, 28]
136
// cities = ["LA", "Chicago", "NYC", "Boston"]
137
138
// Reset arrays
139
names = new String[]{"Charlie", "Alice", "Bob", "David"};
140
ages = new Integer[]{30, 25, 35, 28};
141
cities = new String[]{"NYC", "LA", "Chicago", "Boston"};
142
143
// Sort by ages (index 1)
144
sorter = ParallelSorter.create(new Object[]{names, ages, cities});
145
sorter.mergeSort(1);
146
// Now: names = ["Alice", "David", "Charlie", "Bob"]
147
// ages = [25, 28, 30, 35]
148
// cities = ["LA", "Boston", "NYC", "Chicago"]
149
150
// Useful for maintaining relationships between related data
151
Double[] scores = {85.5, 92.3, 78.1, 95.2};
152
String[] students = {"John", "Jane", "Mike", "Sarah"};
153
String[] grades = {"B", "A", "C", "A"};
154
155
ParallelSorter gradeSorter = ParallelSorter.create(new Object[]{scores, students, grades});
156
gradeSorter.heapSort(0); // Sort by scores
157
// Results maintain student-score-grade relationships
158
```
159
160
## Performance Benefits
161
162
### StringSwitcher Performance
163
164
StringSwitcher provides significant performance improvements over traditional approaches:
165
166
```java
167
// Performance comparison
168
Map<String, Integer> statusMap = new HashMap<>();
169
statusMap.put("OK", 200);
170
statusMap.put("NOT_FOUND", 404);
171
// ... etc
172
173
// StringSwitcher is typically much faster than Map lookup
174
long start = System.nanoTime();
175
for (int i = 0; i < 100000; i++) {
176
statusSwitcher.intValue("NOT_FOUND");
177
}
178
long switcherTime = System.nanoTime() - start;
179
180
start = System.nanoTime();
181
for (int i = 0; i < 100000; i++) {
182
statusMap.get("NOT_FOUND");
183
}
184
long mapTime = System.nanoTime() - start;
185
// StringSwitcher is typically 2-10x faster than HashMap
186
```
187
188
### ParallelSorter Benefits
189
190
ParallelSorter avoids the overhead of creating temporary objects and provides better cache locality:
191
192
- **Memory Efficiency**: No temporary object creation
193
- **Cache Performance**: Better memory access patterns
194
- **Type Safety**: Maintains array relationships
195
- **Algorithm Choice**: Multiple sorting algorithms available