@Override public int indexOf(Object o) { if (!(o instanceof List)) { return -1; } List<?> list = (List<?>) o; if (list.size() != axes.size()) { return -1; } ListIterator<?> itr = list.listIterator(); int computedIndex = 0; while (itr.hasNext()) { int axisIndex = itr.nextIndex(); int elemIndex = axes.get(axisIndex).indexOf(itr.next()); if (elemIndex == -1) { return -1; } computedIndex += elemIndex * axesSizeProduct[axisIndex + 1]; } return computedIndex; }
/** An implementation of {@link List#lastIndexOf(Object)}. */ static int lastIndexOfImpl(List<?> list, @Nullable Object element) { if (list instanceof RandomAccess) { return lastIndexOfRandomAccess(list, element); } else { ListIterator<?> listIterator = list.listIterator(list.size()); while (listIterator.hasPrevious()) { if (Objects.equal(element, listIterator.previous())) { return listIterator.nextIndex(); } } return -1; } }
/** * Gets the ith element from the given list by repositioning the specified * list listIterator. */ private static <T> T get(ListIterator<? extends T> i, int index) { T obj = null; int pos = i.nextIndex(); if (pos <= index) { do { obj = i.next(); } while (pos++ < index); } else { do { obj = i.previous(); } while (--pos > index); } return obj; }
/** * Returns the index of the element that would be returned by a * subsequent call to {@link #next}. * <p> * As would be expected, if the iterator is at the physical end of * the underlying list, 0 is returned, signifying the beginning of * the list. * * @return the index of the element that would be returned if next() were called * @throws NoSuchElementException if there are no elements in the list */ public int nextIndex() { if (list.isEmpty()) { throw new NoSuchElementException( "There are no elements for this iterator to loop on"); } if (iterator.hasNext() == false) { return 0; } else { return iterator.nextIndex(); } }
/** * Reset the stream back to the position described by the supplied marker. This method does nothing if the mark is invalid. * For example, it is not possible to advance the token stream beyond the current position. * * @param marker the marker * @return true if the token stream was reset, or false if the marker was invalid * @see #advance(Marker) */ public boolean rewind(Marker marker) { if (marker.tokenIndex >= 0 && marker.tokenIndex <= this.tokenIterator.nextIndex()) { completed = false; currentToken = null; tokenIterator = this.tokens.listIterator(marker.tokenIndex); moveToNextToken(); return true; } return false; }
/** An implementation of {@link List#lastIndexOf(Object)}. */ static int lastIndexOfImpl(List<?> list, @NullableDecl Object element) { if (list instanceof RandomAccess) { return lastIndexOfRandomAccess(list, element); } else { ListIterator<?> listIterator = list.listIterator(list.size()); while (listIterator.hasPrevious()) { if (Objects.equal(element, listIterator.previous())) { return listIterator.nextIndex(); } } return -1; } }
private void walkBackward(ListIterator expected, ListIterator testing) { while(expected.hasPrevious()) { assertEquals(expected.nextIndex(),testing.nextIndex()); assertEquals(expected.previousIndex(),testing.previousIndex()); assertTrue(testing.hasPrevious()); assertEquals(expected.previous(),testing.previous()); } }
private <T> void toString(List<T> resources, StringBuilder sb) { ListIterator<T> i = resources.listIterator(); while (i.hasNext()) { if (i.nextIndex() != 0) { sb.append(", "); } sb.append(i.next()); } }
/** An implementation of {@link List#lastIndexOf(Object)}. */ static int lastIndexOfImpl(List<?> list, @NullableDecl Object element) { if (list instanceof RandomAccess) { return lastIndexOfRandomAccess(list, element); } else { ListIterator<?> listIterator = list.listIterator(list.size()); while (listIterator.hasPrevious()) { if (Objects.equal(element, listIterator.previous())) { return listIterator.nextIndex(); } } return -1; } }
/** * Returns the index of the element that would be returned by a * subsequent call to {@link #next}. * <p> * As would be expected, if the iterator is at the physical end of * the underlying list, 0 is returned, signifying the beginning of * the list. * * @return the index of the element that would be returned if next() were called * @throws NoSuchElementException if there are no elements in the list */ public int nextIndex() { if (list.isEmpty()) { throw new NoSuchElementException( "There are no elements for this iterator to loop on"); } if (iterator.hasNext() == false) { return 0; } else { return iterator.nextIndex(); } }
/** * Advance the stream back to the position described by the supplied marker. This method does nothing if the mark is invalid. * For example, it is not possible to rewind the token stream beyond the current position. * * @param marker the marker * @return true if the token stream was advanced, or false if the marker was invalid * @see #rewind(Marker) */ public boolean advance(Marker marker) { if (marker.tokenIndex >= 0 && marker.tokenIndex >= this.tokenIterator.nextIndex()) { completed = false; currentToken = null; tokenIterator = this.tokens.listIterator(marker.tokenIndex); moveToNextToken(); return true; } return false; }
/** * Searches this list for the specified object and returns the index of the * last occurrence. * * @param object * the object to search for. * @return the index of the last occurrence of the object, or -1 if the * object was not found. */ public int lastIndexOf(Object object) { ListIterator<?> it = listIterator(size()); if (object != null) { while (it.hasPrevious()) { if (object.equals(it.previous())) { return it.nextIndex(); } } } else { while (it.hasPrevious()) { if (it.previous() == null) { return it.nextIndex(); } } } return -1; }
/** * Traverses to the beginning of the given iterator. * * @param iter the iterator to traverse * @param i the starting index */ private void backwardTest(ListIterator iter, int i) { List list = getList(); while (i > 0) { assertTrue("Iterator should have previous, i:" + i, iter.hasPrevious()); assertEquals("Iterator.nextIndex should work, i:" + i, iter.nextIndex(), i); assertEquals("Iterator.previousIndex should work, i:" + i, iter.previousIndex(), i - 1); Object o = iter.previous(); assertEquals("Iterator returned correct element", list.get(i - 1), o); i--; } assertTrue("Iterator shouldn't have previous", !iter.hasPrevious()); int nextIndex = iter.nextIndex(); assertEquals("nextIndex should be 0, actual value: " + nextIndex, nextIndex, 0); int prevIndex = iter.previousIndex(); assertEquals("previousIndex should be -1, actual value: " + prevIndex, prevIndex, -1); try { iter.previous(); fail("Exhausted iterator should raise NoSuchElement"); } catch (NoSuchElementException e) { // expected } }
private <T> void toString(List<T> resources, StringBuilder sb) { ListIterator<T> i = resources.listIterator(); while (i.hasNext()) { if (i.nextIndex() != 0) { sb.append(", "); } sb.append(i.next()); } }
/** An implementation of {@link List#lastIndexOf(Object)}. */ static int lastIndexOfImpl(List<?> list, @NullableDecl Object element) { if (list instanceof RandomAccess) { return lastIndexOfRandomAccess(list, element); } else { ListIterator<?> listIterator = list.listIterator(list.size()); while (listIterator.hasPrevious()) { if (Objects.equal(element, listIterator.previous())) { return listIterator.nextIndex(); } } return -1; } }
@Override protected S createUpcomingSelection() { if (!rightSubSelectionIterator.hasNext()) { if (!leftSubSelectionIterator.hasNext()) { return noUpcomingSelection(); } leftSubSelection = leftSubSelectionIterator.next(); if (!leftEqualsRight) { rightSubSelectionIterator = rightSubSelector.listIterator(); if (!rightSubSelectionIterator.hasNext()) { return noUpcomingSelection(); } } else { // Select A-B, A-C, B-C. Do not select B-A, C-A, C-B. Do not select A-A, B-B, C-C. if (!leftSubSelectionIterator.hasNext()) { return noUpcomingSelection(); } rightSubSelectionIterator = rightSubSelector.listIterator(leftSubSelectionIterator.nextIndex()); // rightEntityIterator's first hasNext() always returns true because of the nextIndex() } } SubS rightSubSelection = rightSubSelectionIterator.next(); return newSwapSelection(leftSubSelection, rightSubSelection); }