Is there a more optimal way of retrieving the character for the letter? I would assume that I would just replace my Entry set loop with another map, but I do not want to bloat the code, because it will reduce scalability.
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class AciiLetters {
private enum Letter {
_A("010101111101101"), _B("110101110101110"), _C("011100100100011"),
_D("110101101101110"), _E("111100110100111"), _F("111100110100100"),
_G("011100101101011"), _H("101101111101101"), _I("111010010010111"),
_J("011001001101010"), _K("101101110101101"), _L("100100100100111"),
_M("101111111101101"), _N("111101101101101"), _O("010101101101010"),
_P("110101110100100"), _Q("010101101011001"), _R("110101110101101"),
_S("011100010001110"), _T("111010010010010"), _U("101101101101111"),
_V("101101101101010"), _W("101101111111101"), _X("101101010101101"),
_Y("101101010010010"), _Z("111001010100111"), _0("111101101101111"),
_1("010110010010111"), _2("111001111100111"), _3("111001011001111"),
_4("101101111001001"), _5("111100111001111"), _6("111100111101111"),
_7("111001001001001"), _8("111101111101111"), _9("111101111001111"),
SPACE("000000000000000"), UNKNOWN("010101001010010");
private static final Map<Character, Letter> cache;
static {
cache = new HashMap<Character, Letter>();
cache.put('A', _A); cache.put('B', _B); cache.put('C', _C);
cache.put('D', _D); cache.put('E', _E); cache.put('F', _F);
cache.put('G', _G); cache.put('H', _H); cache.put('I', _I);
cache.put('J', _J); cache.put('K', _K); cache.put('L', _L);
cache.put('M', _M); cache.put('N', _N); cache.put('O', _O);
cache.put('P', _P); cache.put('Q', _Q); cache.put('R', _R);
cache.put('S', _S); cache.put('T', _T); cache.put('U', _U);
cache.put('V', _V); cache.put('W', _W); cache.put('X', _X);
cache.put('Y', _Y); cache.put('Z', _Z); cache.put('0', _0);
cache.put('1', _1); cache.put('2', _2); cache.put('3', _3);
cache.put('4', _4); cache.put('5', _5); cache.put('6', _6);
cache.put('7', _7); cache.put('8', _8); cache.put('9', _9);
cache.put(' ', SPACE); cache.put('?', UNKNOWN);
}
public static final int WIDTH = 3, HEIGHT = 5;
private String bitSequence;
Letter(String bitSequence) {
this.bitSequence = bitSequence;
}
public static Letter getEnum(Character value) {
Letter letter = cache.get(value);
if (letter != null)
return letter;
return UNKNOWN;
}
public char getChar() {
for (Entry<Character, Letter> entry : cache.entrySet()) {
if (entry.getValue() == this)
return entry.getKey();
}
return '?';
}
}
public AciiLetters() {
printText("ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 ?", " ");
}
public void printText(String text, String spacing) {
Letter letter;
int offset;
String seq;
Character val;
for (int row = 0; row < Letter.HEIGHT; row++) {
for (Character tCh : text.toCharArray()) {
letter = Letter.getEnum(tCh);
offset = row * Letter.WIDTH;
seq = letter.bitSequence.substring(offset, offset + Letter.WIDTH);
val = letter.getChar();
for (Character ch : seq.toCharArray()) {
System.out.printf("%c", ch == '0' ? ' ' : val);
}
System.out.print(spacing);
}
System.out.println();
}
}
public static void main(String[] args) {
new AciiLetters();
}
}
Output:
A BB CC DD EEE FFF GG H H III JJ K K L M M NNN O PP Q RR SS TTT U U V V W W X X Y Y ZZZ 000 1 222 333 4 4 555 666 777 888 999 ?
A A B B C D D E F G H H I J K K L MMM N N O O P P Q Q R R S T U U V V W W X X Y Y Z 0 0 11 2 3 4 4 5 6 7 8 8 9 9 ? ?
AAA BB C D D EE FF G G HHH I J KK L MMM N N O O PP Q Q RR S T U U V V WWW X Y Z 0 0 1 222 33 444 555 666 7 888 999 ?
A A B B C D D E F G G H H I J J K K L M M N N O O P QQ R R S T U U V V WWW X X Y Z 0 0 1 2 3 4 5 6 6 7 8 8 9 ?
A A BB CC DD EEE F GG H H III J K K LLL M M N N O P Q R R SS T UUU V W W X X Y ZZZ 000 111 222 333 4 555 666 7 888 999 ?