FragmentUtils { protected static List<IBond> traverse(IAtomContainer atomContainer, IAtom atom, List<IBond> bondList) { List<IBond> connectedBonds = atomContainer.getConnectedBondsList(atom); for (IBond aBond : connectedBonds) { if (bondList.contains(aBond)) continue; bondList.add(aBond); IAtom nextAtom = aBond.getOther(atom); if (atomContainer.getConnectedBondsCount(nextAtom) == 1) continue; traverse(atomContainer, nextAtom, bondList); } return bondList; }    }
@Test public void testTraversal_Chain() { IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance(); IAtom[] atoms = new IAtom[]{builder.newInstance(IAtom.class, "C"), builder.newInstance(IAtom.class, "C"), builder.newInstance(IAtom.class, "C"), builder.newInstance(IAtom.class, "C"), builder.newInstance(IAtom.class, "C"), builder.newInstance(IAtom.class, "C")}; IBond[] bonds = new IBond[]{builder.newInstance(IBond.class, atoms[0], atoms[1]), builder.newInstance(IBond.class, atoms[1], atoms[2]), builder.newInstance(IBond.class, atoms[2], atoms[3]), builder.newInstance(IBond.class, atoms[3], atoms[4]), builder.newInstance(IBond.class, atoms[4], atoms[5])}; IAtomContainer m = builder.newInstance(IAtomContainer.class, 0, 0, 0, 0); m.setAtoms(atoms); m.setBonds(bonds); List<IBond> accumulator = new ArrayList<IBond>(); FragmentUtils.traverse(m, atoms[0], accumulator); assertThat(accumulator.size(), is(5)); assertThat(accumulator.get(0), is(bonds[0])); assertThat(accumulator.get(1), is(bonds[1])); assertThat(accumulator.get(2), is(bonds[2])); assertThat(accumulator.get(3), is(bonds[3])); assertThat(accumulator.get(4), is(bonds[4])); accumulator.clear(); FragmentUtils.traverse(m, atoms[3], accumulator); assertThat(accumulator.size(), is(5)); assertThat(accumulator.get(0), is(bonds[2])); assertThat(accumulator.get(1), is(bonds[1])); assertThat(accumulator.get(2), is(bonds[0])); assertThat(accumulator.get(3), is(bonds[3])); assertThat(accumulator.get(4), is(bonds[4])); }