@Override public ParseResult parse(final TokenList tokens, final int templatePos) { final Object o1 = tokens.get(templatePos); final Object o2 = tokens.get(templatePos + 2); QuotedString ret; if ((o1 instanceof QuotedString) && (o2 instanceof QuotedString)) { ret = new QuotedString(((QuotedString) o1).value + ((QuotedString) o2).value); } else if (o1 instanceof QuotedString) { ret = new QuotedString(((QuotedString) o1).value + o2.toString()); } else if (o2 instanceof QuotedString) { ret = new QuotedString(o1.toString() + ((QuotedString) o2).value); } else return ParseResult.fail(); return ParseResult.success( tokens.replaceWithTokens(templatePos, templatePos + template.size(), ret)); }
public class StringAppender extends Parser { private static final long serialVersionUID = -7820633700412274072L; private static TokenList template = TokenList.createD(Object.class, "+", Object.class); @Override public ParseResult parse(final TokenList tokens, final int templatePos) { final Object o1 = tokens.get(templatePos); final Object o2 = tokens.get(templatePos + 2); QuotedString ret; if ((o1 instanceof QuotedString) && (o2 instanceof QuotedString)) { ret = new QuotedString(((QuotedString) o1).value + ((QuotedString) o2).value); } else if (o1 instanceof QuotedString) { ret = new QuotedString(((QuotedString) o1).value + o2.toString()); } else if (o2 instanceof QuotedString) { ret = new QuotedString(o1.toString() + ((QuotedString) o2).value); } else return ParseResult.fail(); return ParseResult.success( tokens.replaceWithTokens(templatePos, templatePos + template.size(), ret)); } @Override public TokenList getTemplate() { return template; } @Override public int hashCode() { return "BoolFunctionsParser".hashCode(); } @Override public boolean equals(final Object obj) { return obj instanceof StringAppender; } }
// We serialize manually to ensure that TokenLists are serialized // efficiently private void writeObject(final ObjectOutputStream out) throws IOException { out.writeObject(question); out.writeInt(answer.size()); for (final Object o : answer) { out.writeObject(o); } }
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { question = (String) in.readObject(); final Object[] answer = new Object[in.readInt()]; for (int x = 0; x < answer.length; x++) { answer[x] = in.readObject(); } this.answer = TokenList.createD(answer); }
public class RadixConverter extends Parser { private static final long serialVersionUID = 9120544485351922021L; private static final TokenList template = TokenList.createD( Lists.newArrayList(LargeInteger.class, Radix.class), Lists.newArrayList("to", "as", "in"), Lists.newArrayList( "bin", "binary", "hex", "hexadecimal", "oct", "octal", "dec", "decimal")); @Override public ParseResult parse(final TokenList tokens, final int templatePos) { final Object num = tokens.get(templatePos); final long li = num instanceof LargeInteger ? ((LargeInteger) tokens.get(templatePos)).longValue() : ((Radix) num).integer; final String convertTo = (String) tokens.get(templatePos + 2); Object result; if (convertTo.startsWith("bin")) { result = new Radix(li, 2); } else if (convertTo.startsWith("hex")) { result = new Radix(li, 16); } else if (convertTo.startsWith("oct")) { result = new Radix(li, 8); } else { result = LargeInteger.valueOf(li); } return ParseResult.success( tokens.replaceWithTokens(templatePos, templatePos + template.size(), result)); } @Override public TokenList getTemplate() { return template; } @Override public int hashCode() { return "RadixConverter".hashCode(); } @Override public String toString() { return ""; } @Override public boolean equals(final Object obj) { return obj instanceof RadixConverter; } }
@Override public ParseResult parse(final TokenList tokens, final int templatePos) { final Object num = tokens.get(templatePos); final long li = num instanceof LargeInteger ? ((LargeInteger) tokens.get(templatePos)).longValue() : ((Radix) num).integer; final String convertTo = (String) tokens.get(templatePos + 2); Object result; if (convertTo.startsWith("bin")) { result = new Radix(li, 2); } else if (convertTo.startsWith("hex")) { result = new Radix(li, 16); } else if (convertTo.startsWith("oct")) { result = new Radix(li, 8); } else { result = LargeInteger.valueOf(li); } return ParseResult.success( tokens.replaceWithTokens(templatePos, templatePos + template.size(), result)); }