/** * Removes val from underlying list. * * @param val * @return the modified RDFList. */ private RDFList baseRemove(final RDFList val) { RDFList prev = null; RDFList cell = holder.getBaseItem(); final boolean searching = true; while (searching && !cell.isEmpty()) { if (cell.equals(val)) { // found the value to be removed final RDFList tail = cell.getTail(); if (prev != null) { prev.setTail(tail); } cell.removeProperties(); // return this unless we have removed the head element return (prev == null) ? tail : this; } else { // not found yet prev = cell; cell = cell.getTail(); } } // not found return this; }
findElement( true, 0 ).setTail( list );
findElement( true, 0 ).setTail( list );
prev.setTail( tail );
prev.setTail( tail );
public void testSetTail() { final Model m = ModelFactory.createDefaultModel(); final Resource root = m.createResource(TestList.NS + "root"); final Property p = m.createProperty(TestList.NS, "p"); final Resource nil = RDF.nil; m.add(nil, RDF.type, RDF.List); final Resource list0 = m.createResource(); m.add(list0, RDF.type, RDF.List); m.add(list0, RDF.first, "fred"); m.add(list0, RDF.rest, nil); m.add(root, p, list0); final RDFList l1 = getListRoot(m); checkValid("settail1", l1, true); final Resource list1 = m.createResource(); m.add(list1, RDF.type, RDF.List); m.add(list1, RDF.first, "george"); m.add(list1, RDF.rest, nil); final RDFList l2 = list1.as(RDFList.class); Assert.assertNotNull("as(RDFList) should not return null for root", l2); checkValid("settail2", l2, true); Assert.assertEquals("l1 should have length 1", 1, l1.size()); Assert.assertEquals("l2 should have length 1", 1, l2.size()); // use set tail to join the lists together l1.setTail(l2); checkValid("settail3", l1, true); checkValid("settail4", l2, true); Assert.assertEquals("l1 should have length 2", 2, l1.size()); Assert.assertEquals("l2 should have length 1", 1, l2.size()); }
public void testSetTail() { final Model m = ModelFactory.createDefaultModel(); final Resource root = m.createResource(TestList.NS + "root"); final Property p = m.createProperty(TestList.NS, "p"); final Resource nil = RDF.nil; m.add(nil, RDF.type, RDF.List); final Resource list0 = m.createResource(); m.add(list0, RDF.type, RDF.List); m.add(list0, RDF.first, "fred"); m.add(list0, RDF.rest, nil); m.add(root, p, list0); final RDFList l1 = getListRoot(m); checkValid("settail1", l1, true); final Resource list1 = m.createResource(); m.add(list1, RDF.type, RDF.List); m.add(list1, RDF.first, "george"); m.add(list1, RDF.rest, nil); final RDFList l2 = list1.as(RDFList.class); Assert.assertNotNull("as(RDFList) should not return null for root", l2); checkValid("settail2", l2, true); Assert.assertEquals("l1 should have length 1", 1, l1.size()); Assert.assertEquals("l2 should have length 1", 1, l2.size()); // use set tail to join the lists together l1.setTail(l2); checkValid("settail3", l1, true); checkValid("settail4", l2, true); Assert.assertEquals("l1 should have length 2", 2, l1.size()); Assert.assertEquals("l2 should have length 1", 1, l2.size()); }