public Rule(String lhs, String rhs) { this(lhs, new StringTuple(rhs)); }
public static StringTuple fromList(List<String> list) { List<String> items = new LinkedList<String>(list); String repr = joinList(" ", items); return new StringTuple(repr, items); }
public StringTuple remove(int index) throws IndexOutOfBoundsException{ if( index < 0 || index >= items.size()) throw new IndexOutOfBoundsException(); List<String> copy = stringToItems(repr); copy.remove(index); String modifiedRepr = joinList(" ", copy); return new StringTuple(modifiedRepr, copy); }
public StringTuple insert(int index, String value) throws IndexOutOfBoundsException{ if( index < 0 || index >= items.size()) throw new IndexOutOfBoundsException(); List<String> copy = stringToItems(repr); copy.add(index, value); String modifiedRepr = joinList(" ", copy); return new StringTuple(modifiedRepr, copy); }
public StringTuple modify(int index, ModifierFn fn) throws IndexOutOfBoundsException{ if( index < 0 || index >= items.size()) throw new IndexOutOfBoundsException(); List<String> copy = stringToItems(repr); copy.set(index, fn.modify(items.get(index))); String modifiedRepr = joinList(" ", copy); return new StringTuple(modifiedRepr, copy); }
@Test void testHashing() { HashMap<StringTuple, String> map = new HashMap<>(); map.put(new StringTuple("A B C"), "Test"); assertTrue(map.containsKey(new StringTuple("A B C"))); }
@Test void shouldSplitNaryRule() { Rule[] rules = { new Rule("$A", "$B $C $D") }; Grammar g = new Grammar(Arrays.asList(rules), null); assertTrue(g.binaryRules.containsKey(new StringTuple("$C $D"))); assertTrue(g.binaryRules.containsKey(new StringTuple("$B $A_$B"))); assertEquals(g.binaryRules.get(new StringTuple("$C $D")).get(0).getLHS(), "$A_$B"); assertEquals(g.binaryRules.get(new StringTuple("$B $A_$B")).get(0).getLHS(), "$A"); }
@Test void shouldSplitOptionals() { Rule[] rules = { new Rule("$A", "?$B $C") }; Grammar g = new Grammar(Arrays.asList(rules), null); assertTrue(g.binaryRules.containsKey(new StringTuple("$B $C"))); assertTrue(g.unaryRules.containsKey(new StringTuple("$C"))); assertEquals(g.binaryRules.get(new StringTuple("$B $C")).get(0).getLHS(), "$A"); assertEquals(g.unaryRules.get(new StringTuple("$C")).get(0).getLHS(), "$A"); }
@Test void testModify() { StringTuple s = new StringTuple("1 2 3"); StringTuple changed = s.modify(1, (item) -> "4"); assertEquals(changed, new StringTuple("1 4 3")); assertNotEquals(changed, s); }
@Test void testRemove() { StringTuple s = new StringTuple("1 2 3"); StringTuple changed = s.remove(0); assertEquals(changed, new StringTuple("2 3")); assertNotEquals(changed, s); }
@Test void testInsert() { StringTuple s = new StringTuple("2 3"); StringTuple changed = s.insert(0, "X"); assertEquals(changed, new StringTuple("X 2 3")); assertNotEquals(changed, s); }