Code generation library for creating dynamic proxies, bean utilities, and fast reflection alternatives with bundled dependencies
—
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.
import net.sf.cglib.util.StringSwitcher;
import net.sf.cglib.util.ParallelSorter;Efficient string-to-integer mapping, providing a fast alternative to large switch statements or Map lookups.
/**
* Efficient string-to-int mapping (switch statement replacement)
*/
public abstract class StringSwitcher {
/**
* Create string switcher
* @param strings Array of string values
* @param ints Array of corresponding integer values
* @param fixedInput Whether input strings are fixed (enables optimizations)
* @return StringSwitcher instance
*/
public static StringSwitcher create(String[] strings, int[] ints, boolean fixedInput);
/**
* Get integer value for string
* @param s Input string
* @return Corresponding integer value, or -1 if not found
*/
public abstract int intValue(String s);
}Usage Examples:
// Create string-to-int mapping
String[] operations = {"CREATE", "READ", "UPDATE", "DELETE"};
int[] opcodes = {1, 2, 3, 4};
StringSwitcher switcher = StringSwitcher.create(operations, opcodes, true);
// Use switcher (much faster than Map or if-else chains)
int createOp = switcher.intValue("CREATE"); // 1
int readOp = switcher.intValue("READ"); // 2
int updateOp = switcher.intValue("UPDATE"); // 3
int deleteOp = switcher.intValue("DELETE"); // 4
int unknown = switcher.intValue("UNKNOWN"); // -1
// Use in processing loop
List<String> commands = getCommands();
for (String command : commands) {
switch (switcher.intValue(command)) {
case 1: // CREATE
handleCreate();
break;
case 2: // READ
handleRead();
break;
case 3: // UPDATE
handleUpdate();
break;
case 4: // DELETE
handleDelete();
break;
default:
handleUnknown(command);
}
}
// HTTP status code mapping
String[] statusNames = {"OK", "NOT_FOUND", "SERVER_ERROR", "BAD_REQUEST"};
int[] statusCodes = {200, 404, 500, 400};
StringSwitcher statusSwitcher = StringSwitcher.create(statusNames, statusCodes, true);
int code = statusSwitcher.intValue("NOT_FOUND"); // 404Utility for parallel sorting of multiple arrays, keeping corresponding elements aligned.
/**
* Parallel sorting of multiple arrays
*/
public abstract class ParallelSorter {
/**
* Create parallel sorter for arrays
* @param arrays Arrays to sort in parallel
* @return ParallelSorter instance
*/
public static ParallelSorter create(Object[] arrays);
/**
* Quick sort using array at index as sort key
* @param index Index of array to use as sort key
*/
public abstract void quickSort(int index);
/**
* Merge sort using array at index as sort key
* @param index Index of array to use as sort key
*/
public abstract void mergeSort(int index);
/**
* Heap sort using array at index as sort key
* @param index Index of array to use as sort key
*/
public abstract void heapSort(int index);
}Usage Examples:
// Create parallel arrays
String[] names = {"Charlie", "Alice", "Bob", "David"};
Integer[] ages = {30, 25, 35, 28};
String[] cities = {"NYC", "LA", "Chicago", "Boston"};
// Create parallel sorter
ParallelSorter sorter = ParallelSorter.create(new Object[]{names, ages, cities});
// Sort by names (index 0)
sorter.quickSort(0);
// Now: names = ["Alice", "Bob", "Charlie", "David"]
// ages = [25, 35, 30, 28]
// cities = ["LA", "Chicago", "NYC", "Boston"]
// Reset arrays
names = new String[]{"Charlie", "Alice", "Bob", "David"};
ages = new Integer[]{30, 25, 35, 28};
cities = new String[]{"NYC", "LA", "Chicago", "Boston"};
// Sort by ages (index 1)
sorter = ParallelSorter.create(new Object[]{names, ages, cities});
sorter.mergeSort(1);
// Now: names = ["Alice", "David", "Charlie", "Bob"]
// ages = [25, 28, 30, 35]
// cities = ["LA", "Boston", "NYC", "Chicago"]
// Useful for maintaining relationships between related data
Double[] scores = {85.5, 92.3, 78.1, 95.2};
String[] students = {"John", "Jane", "Mike", "Sarah"};
String[] grades = {"B", "A", "C", "A"};
ParallelSorter gradeSorter = ParallelSorter.create(new Object[]{scores, students, grades});
gradeSorter.heapSort(0); // Sort by scores
// Results maintain student-score-grade relationshipsStringSwitcher provides significant performance improvements over traditional approaches:
// Performance comparison
Map<String, Integer> statusMap = new HashMap<>();
statusMap.put("OK", 200);
statusMap.put("NOT_FOUND", 404);
// ... etc
// StringSwitcher is typically much faster than Map lookup
long start = System.nanoTime();
for (int i = 0; i < 100000; i++) {
statusSwitcher.intValue("NOT_FOUND");
}
long switcherTime = System.nanoTime() - start;
start = System.nanoTime();
for (int i = 0; i < 100000; i++) {
statusMap.get("NOT_FOUND");
}
long mapTime = System.nanoTime() - start;
// StringSwitcher is typically 2-10x faster than HashMapParallelSorter avoids the overhead of creating temporary objects and provides better cache locality:
Install with Tessl CLI
npx tessl i tessl/maven-cglib--cglib-nodep