/** * Adds a symbol to the table to be built. The table must not contains a symbol with the * same resource type and name. * * @param symbol the symbol to add * @return {@code this} for use with fluent-style notation */ public Builder add(@NonNull Symbol symbol) { String key = key(symbol); if (symbols.containsKey(key)) { throw new IllegalArgumentException( "Duplicate symbol in table with resource " + "type '" + symbol.getResourceType() + "' and symbol name '" + symbol.getName() + "'"); } symbols.put(key, symbol); return this; }
/** * Writes a symbol table to a symbol file. * * @param table the table * @param file the file where the table should be written * @throws UncheckedIOException I/O error */ public static void write(@NonNull SymbolTable table, @NonNull File file) { List<String> lines = new ArrayList<>(); for (Symbol s : table.allSymbols()) { lines.add( s.getJavaType() + " " + s.getResourceType() + " " + s.getName() + " " + s.getValue()); } try ( FileOutputStream fos = new FileOutputStream(file); PrintWriter pw = new PrintWriter(fos)) { lines.forEach(pw::println); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Obtains a unique key for a symbol. * * @param symbol the symbol * @return the unique ID */ @NonNull private static String key(@NonNull Symbol symbol) { return key(symbol.getResourceType(), symbol.getName()); }
table.allSymbols().forEach(s -> resourceTypes.add(s.getResourceType())); if (sym.getResourceType().equals(rt)) { syms.add(sym);