List of usage examples for java.util ListIterator next
E next();
From source file:Main.java
public static <E> E setInSortedList(List<E> list, int index, E item, Comparator<? super E> comparator, BiPredicate<? super E, ? super E> distincter) { if (distincter != null) { ListIterator<E> iter = list.listIterator(); while (iter.hasNext()) { int currIndex = iter.nextIndex(); E currItem = iter.next(); if (index != currIndex && distincter.test(currItem, item)) { return null; }/*from w w w . j av a 2 s . com*/ } } E previousItem = list.set(index, item); list.sort(comparator); return previousItem; }
From source file:Main.java
private static <T> T findPrevious(ListIterator<T> iter) { T prev = null;//from ww w . ja v a2s . c o m iter.previous(); // rewind if (iter.hasPrevious()) { prev = iter.previous(); iter.next(); // come back } iter.next(); // come back return prev; }
From source file:Main.java
/** * Finds an item by iterating through the specified iterator. * /* w w w . j a va 2s.c o m*/ * @param item the object to find * @param iter the iterator to examine * @return list index of the item or -1 if the item was not found */ public static int findObject(Object item, ListIterator<?> iter) { while (iter.hasNext()) { Object o = iter.next(); if (item == null ? o == null : item.equals(o)) { return iter.previousIndex(); } } return -1; }
From source file:Main.java
/** * Returns the indexth element from the list by using the list iterator to navigate. * @param <T> the element type// w ww .ja va2 s.c om * @param it the list iterator * @param index the target index * @return the value */ private static <T> T get(ListIterator<? extends T> it, int index) { T obj = null; int pos = it.nextIndex(); if (pos <= index) { do { obj = it.next(); } while (pos++ < index); } else { do { obj = it.previous(); } while (--pos > index); } return obj; }
From source file:Main.java
private static <E> void updateList(List<E> origList, Collection<? extends E> updateList, boolean add, boolean replace, boolean remove, BiPredicate<? super E, ? super E> equalTester, BiConsumer<List<E>, Collection<? extends E>> adder) { List<E> itemsToRemove = null; List<E> itemsToAdd = null; for (E update : updateList) { boolean origListContainsUpdate = false; ListIterator<E> origIter = origList.listIterator(); while (origIter.hasNext()) { E orig = origIter.next(); if (equalTester.test(orig, update)) { origListContainsUpdate = true; if (remove) { if (itemsToRemove == null) { itemsToRemove = new ArrayList<>(origList); }/* w w w . j a v a 2s .c o m*/ itemsToRemove.remove(orig); } if (replace) { origIter.set(update); } break; } } if (!origListContainsUpdate && add) { if (itemsToAdd == null) { itemsToAdd = new ArrayList<>(); } itemsToAdd.add(update); } } if (remove) { if (itemsToRemove != null) { origList.removeAll(itemsToRemove); } else { origList.clear(); } } if (itemsToAdd != null) { adder.accept(origList, itemsToAdd); } }
From source file:Main.java
public static <E> boolean addToSortedList(List<E> list, E item, Comparator<? super E> comparator, BiPredicate<? super E, ? super E> distincter) { int addIndex = list.size(); boolean foundAddIndex = false; ListIterator<E> iter = list.listIterator(); while (iter.hasNext()) { int currIndex = iter.nextIndex(); E currItem = iter.next(); if (distincter != null && distincter.test(currItem, item)) { return false; }/*from w w w .j a v a 2s . co m*/ if (!foundAddIndex) { int comparison = comparator.compare(currItem, item); if (comparison > 0) { addIndex = currIndex; // if we don't have to check the remaining items for distinct we can break the loop now if (distincter == null) { break; } foundAddIndex = true; } else if (comparison == 0) { addIndex = currIndex + 1; // if we don't have to check the remaining items for distinct we can break the loop now if (distincter == null) { break; } foundAddIndex = true; } } } list.add(addIndex, item); return true; }
From source file:Main.java
public static <T> boolean listEquals(List<T> left, List<T> right) { if (left == right) return true; ListIterator<T> leftIterator = left.listIterator(); ListIterator<T> rightIterator = right.listIterator(); while (leftIterator.hasNext() && rightIterator.hasNext()) { T leftElement = leftIterator.next(); T rightElement = rightIterator.next(); if (!(leftElement == null ? rightElement == null : leftElement.equals(rightElement))) return false; }/*from w w w.j a v a 2 s. com*/ return !(leftIterator.hasNext() || rightIterator.hasNext()); }
From source file:Main.java
/** * Copies all of the elements from one list into another. After the * operation, the index of each copied element in the destination list will * be identical to its index in the source list. The destination list must * be at least as long as the source list. If it is longer, the remaining * elements in the destination list are unaffected. * <p>/* www .j a v a 2 s . co m*/ * This method runs in linear time. * @param <T> . * @param dest The destination list. * @param src The source list. * * @return boolean isCopyValide */ public static <T> Boolean copy(List<? super T> dest, List<? extends T> src) { Boolean isCopyValide = null; int srcSize = src.size(); if (srcSize > dest.size()) { isCopyValide = false; throw new IndexOutOfBoundsException("Source does not fit in dest"); } if (srcSize < COPY_THRESHOLD || (src instanceof RandomAccess && dest instanceof RandomAccess)) { for (int i = 0; i < srcSize; i++) { dest.set(i, src.get(i)); } } else { ListIterator<? super T> di = dest.listIterator(); ListIterator<? extends T> si = src.listIterator(); for (int i = 0; i < srcSize; i++) { di.next(); di.set(si.next()); } } isCopyValide = true; return isCopyValide; }
From source file:edu.cornell.mannlib.vitro.webapp.auth.policy.ServletPolicyList.java
/** * Replace the first instance of this class of policy in the list. If no * instance is found, add the policy to the end of the list. *///w w w . j a v a 2s . c o m public static void replacePolicy(ServletContext sc, PolicyIface policy) { if (policy == null) { return; } Class<?> clzz = policy.getClass(); PolicyList policies = getPolicyList(sc); ListIterator<PolicyIface> it = policies.listIterator(); while (it.hasNext()) { if (clzz.isAssignableFrom(it.next().getClass())) { it.set(policy); return; } } addPolicy(sc, policy); }
From source file:mitm.common.util.StringReplaceUtils.java
/** * For each String in the list the non-XML characters are replaced. The returned list is the same instance as input * (it's an in place replacement)//from w w w . j a v a2s.com */ public static List<String> replaceNonXML(List<String> input, String replaceWith) { Check.notNull(replaceWith, "replaceWith"); if (input == null) { return null; } ListIterator<String> listIterator = input.listIterator(); while (listIterator.hasNext()) { String line = listIterator.next(); listIterator.set(StringReplaceUtils.replaceNonXML(line, "#")); } return input; }