@Test(expected = NoSuchElementException.class) public void iteratorNoSuchElement() { NavDestination destination = createFirstDestination(); NavGraph graph = createGraphWithDestination(destination); Iterator<NavDestination> iterator = graph.iterator(); iterator.next(); iterator.next(); }
@NonNull @Override public Iterator<NavDestination> iterator() { return new Iterator<NavDestination>() { private int mIndex = -1; private boolean mWentToNext = false; @Override public boolean hasNext() { return mIndex + 1 < mNodes.size(); } @Override public NavDestination next() { if (!hasNext()) { throw new NoSuchElementException(); } mWentToNext = true; return mNodes.valueAt(++mIndex); } @Override public void remove() { if (!mWentToNext) { throw new IllegalStateException( "You must call next() before you can remove an element"); } mNodes.valueAt(mIndex).setParent(null); mNodes.removeAt(mIndex); mIndex--; mWentToNext = false; } }; }
NavGraph extends NavDestination implements Iterable<NavDestination> { @NonNull @Override public Iterator<NavDestination> iterator() { return new Iterator<NavDestination>() { private int mIndex = -1; private boolean mWentToNext = false; @Override public boolean hasNext() { return mIndex + 1 < mNodes.size(); } @Override public NavDestination next() { if (!hasNext()) { throw new NoSuchElementException(); } mWentToNext = true; return mNodes.valueAt(++mIndex); } @Override public void remove() { if (!mWentToNext) { throw new IllegalStateException( "You must call next() before you can remove an element"); } mNodes.valueAt(mIndex).setParent(null); mNodes.removeAt(mIndex); mIndex--; mWentToNext = false; } }; } }
NavGraph extends NavDestination implements Iterable<NavDestination> { @NonNull @Override public Iterator<NavDestination> iterator() { return new Iterator<NavDestination>() { private int mIndex = -1; private boolean mWentToNext = false; @Override public boolean hasNext() { return mIndex + 1 < mNodes.size(); } @Override public NavDestination next() { if (!hasNext()) { throw new NoSuchElementException(); } mWentToNext = true; return mNodes.valueAt(++mIndex); } @Override public void remove() { if (!mWentToNext) { throw new IllegalStateException( "You must call next() before you can remove an element"); } mNodes.valueAt(mIndex).setParent(null); mNodes.removeAt(mIndex); mIndex--; mWentToNext = false; } }; } NavGraph(@NonNull NavigatorProvider navigatorProvider);  NavGraph(@NonNull Navigator<? extends NavGraph> navGraphNavigator); }
NavGraph extends NavDestination implements Iterable<NavDestination> { @NonNull @Override public Iterator<NavDestination> iterator() { return new Iterator<NavDestination>() { private int mIndex = -1; private boolean mWentToNext = false; @Override public boolean hasNext() { return mIndex + 1 < mNodes.size(); } @Override public NavDestination next() { if (!hasNext()) { throw new NoSuchElementException(); } mWentToNext = true; return mNodes.valueAt(++mIndex); } @Override public void remove() { if (!mWentToNext) { throw new IllegalStateException( "You must call next() before you can remove an element"); } mNodes.valueAt(mIndex).setParent(null); mNodes.removeAt(mIndex); mIndex--; mWentToNext = false; } }; } NavGraph(@NonNull NavigatorProvider navigatorProvider);  NavGraph(@NonNull Navigator<? extends NavGraph> navGraphNavigator); @Override void onInflate(@NonNull Context context, @NonNull AttributeSet attrs); void addDestination(@NonNull NavDestination node); void addDestinations(@NonNull Collection<NavDestination> nodes); void addDestinations(@NonNull NavDestination... nodes); NavDestination findNode(@IdRes int resid); @NonNull @Override Iterator<NavDestination> iterator(); void addAll(@NonNull NavGraph other); void remove(@NonNull NavDestination node); void clear(); @IdRes int getStartDestination(); void setStartDestination(@IdRes int startDestId); }
NavGraph extends NavDestination implements Iterable<NavDestination> { @NonNull @Override public Iterator<NavDestination> iterator() { return new Iterator<NavDestination>() { private int mIndex = -1; private boolean mWentToNext = false; @Override public boolean hasNext() { return mIndex + 1 < mNodes.size(); } @Override public NavDestination next() { if (!hasNext()) { throw new NoSuchElementException(); } mWentToNext = true; return mNodes.valueAt(++mIndex); } @Override public void remove() { if (!mWentToNext) { throw new IllegalStateException( "You must call next() before you can remove an element"); } mNodes.valueAt(mIndex).setParent(null); mNodes.removeAt(mIndex); mIndex--; mWentToNext = false; } }; } NavGraph(@NonNull NavigatorProvider navigatorProvider);  NavGraph(@NonNull Navigator<? extends NavGraph> navGraphNavigator); @Override void onInflate(@NonNull Context context, @NonNull AttributeSet attrs); void addDestination(@NonNull NavDestination node); void addDestinations(@NonNull Collection<NavDestination> nodes); void addDestinations(@NonNull NavDestination... nodes); NavDestination findNode(@IdRes int resid); @NonNull @Override Iterator<NavDestination> iterator(); void addAll(@NonNull NavGraph other); void remove(@NonNull NavDestination node); void clear(); @IdRes int getStartDestination(); void setStartDestination(@IdRes int startDestId);  }