|
| 1 | +package com.jsoniter; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class CodegenImplEnum { |
| 6 | + public static String genEnum(Class clazz) { |
| 7 | + StringBuilder lines = new StringBuilder(); |
| 8 | + append(lines, "if (iter.readNull()) { return null; }"); |
| 9 | + append(lines, "com.jsoniter.Slice field = com.jsoniter.CodegenAccess.readSlice(iter);"); |
| 10 | + append(lines, "switch (field.len()) {"); |
| 11 | + append(lines, renderTriTree(buildTriTree(Arrays.asList(clazz.getEnumConstants())))); |
| 12 | + append(lines, "}"); // end of switch |
| 13 | + append(lines, String.format("throw iter.reportError(\"decode enum\", field + \" is not valid enum for %s\");", clazz.getName())); |
| 14 | + return lines.toString(); |
| 15 | + } |
| 16 | + |
| 17 | + private static Map<Integer, Object> buildTriTree(List<Object> allConsts) { |
| 18 | + Map<Integer, Object> trieTree = new HashMap<Integer, Object>(); |
| 19 | + for (Object e : allConsts) { |
| 20 | + byte[] fromNameBytes = e.toString().getBytes(); |
| 21 | + Map<Byte, Object> current = (Map<Byte, Object>) trieTree.get(fromNameBytes.length); |
| 22 | + if (current == null) { |
| 23 | + current = new HashMap<Byte, Object>(); |
| 24 | + trieTree.put(fromNameBytes.length, current); |
| 25 | + } |
| 26 | + for (int i = 0; i < fromNameBytes.length - 1; i++) { |
| 27 | + byte b = fromNameBytes[i]; |
| 28 | + Map<Byte, Object> next = (Map<Byte, Object>) current.get(b); |
| 29 | + if (next == null) { |
| 30 | + next = new HashMap<Byte, Object>(); |
| 31 | + current.put(b, next); |
| 32 | + } |
| 33 | + current = next; |
| 34 | + } |
| 35 | + current.put(fromNameBytes[fromNameBytes.length - 1], e); |
| 36 | + } |
| 37 | + return trieTree; |
| 38 | + } |
| 39 | + |
| 40 | + private static String renderTriTree(Map<Integer, Object> trieTree) { |
| 41 | + StringBuilder switchBody = new StringBuilder(); |
| 42 | + for (Map.Entry<Integer, Object> entry : trieTree.entrySet()) { |
| 43 | + Integer len = entry.getKey(); |
| 44 | + append(switchBody, "case " + len + ": "); |
| 45 | + Map<Byte, Object> current = (Map<Byte, Object>) entry.getValue(); |
| 46 | + addFieldDispatch(switchBody, len, 0, current, new ArrayList<Byte>()); |
| 47 | + append(switchBody, "break;"); |
| 48 | + } |
| 49 | + return switchBody.toString(); |
| 50 | + } |
| 51 | + |
| 52 | + private static void addFieldDispatch( |
| 53 | + StringBuilder lines, int len, int i, Map<Byte, Object> current, List<Byte> bytesToCompare) { |
| 54 | + for (Map.Entry<Byte, Object> entry : current.entrySet()) { |
| 55 | + Byte b = entry.getKey(); |
| 56 | + if (i == len - 1) { |
| 57 | + append(lines, "if ("); |
| 58 | + for (int j = 0; j < bytesToCompare.size(); j++) { |
| 59 | + Byte a = bytesToCompare.get(j); |
| 60 | + append(lines, String.format("field.at(%d)==%s && ", i - bytesToCompare.size() + j, a)); |
| 61 | + } |
| 62 | + append(lines, String.format("field.at(%d)==%s", i, b)); |
| 63 | + append(lines, ") {"); |
| 64 | + Object e = entry.getValue(); |
| 65 | + append(lines, String.format("return %s.%s;", e.getClass().getName(), e.toString())); |
| 66 | + append(lines, "}"); |
| 67 | + continue; |
| 68 | + } |
| 69 | + Map<Byte, Object> next = (Map<Byte, Object>) entry.getValue(); |
| 70 | + if (next.size() == 1) { |
| 71 | + ArrayList<Byte> nextBytesToCompare = new ArrayList<Byte>(bytesToCompare); |
| 72 | + nextBytesToCompare.add(b); |
| 73 | + addFieldDispatch(lines, len, i + 1, next, nextBytesToCompare); |
| 74 | + continue; |
| 75 | + } |
| 76 | + append(lines, "if ("); |
| 77 | + for (int j = 0; j < bytesToCompare.size(); j++) { |
| 78 | + Byte a = bytesToCompare.get(j); |
| 79 | + append(lines, String.format("field.at(%d)==%s && ", i - bytesToCompare.size() + j, a)); |
| 80 | + } |
| 81 | + append(lines, String.format("field.at(%d)==%s", i, b)); |
| 82 | + append(lines, ") {"); |
| 83 | + addFieldDispatch(lines, len, i + 1, next, new ArrayList<Byte>()); |
| 84 | + append(lines, "}"); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static void append(StringBuilder lines, String str) { |
| 89 | + lines.append(str); |
| 90 | + lines.append("\n"); |
| 91 | + } |
| 92 | +} |
0 commit comments