Skip to content

Instantly share code, notes, and snippets.

@edefazio
Created December 17, 2019 21:39
Show Gist options
  • Save edefazio/6532b07dd82a213d3fa5f8c96cb62c17 to your computer and use it in GitHub Desktop.
Save edefazio/6532b07dd82a213d3fa5f8c96cb62c17 to your computer and use it in GitHub Desktop.
Using the LutBuilder to statically generate code & compile & use a LookupTable Class replacing a Map lookup with switch code
package use.jdraft.lut;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
public class LutBuilderTest extends TestCase {
//this will house the Lut
public static final Lut.String_to_int nameToAge;
static { //Im assuming you want to build this statically
Map<String,Integer> na = new HashMap<>();
na.put("Able", 1);
na.put("Ben", 2);
na.put("Charles", 3);
nameToAge = LutBuilder.runtimeStringToIntLut("internal.NameToAge", na);
}
public static void main(String[]args) {
assertEquals(1, nameToAge.lookup("Able") );
assertEquals(2, nameToAge.lookup("Ben") );
assertEquals(3, nameToAge.lookup("Charles") );
try {
nameToAge.lookup("noone");
fail("expected Exception");
}catch(RuntimeException re) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment