Example usage for java.util ListIterator set

List of usage examples for java.util ListIterator set

Introduction

In this page you can find the example usage for java.util ListIterator set.

Prototype

void set(E e);

Source Link

Document

Replaces the last element returned by #next or #previous with the specified element (optional operation).

Usage

From source file:org.apache.fop.layoutmgr.inline.TextLayoutManager.java

/** {@inheritDoc} */
public List addALetterSpaceTo(final List oldList, int depth) {
    // old list contains only a box, or the sequence: box penalty glue box;
    // look at the Position stored in the first element in oldList
    // which is always a box
    ListIterator oldListIterator = oldList.listIterator();
    KnuthElement knuthElement = (KnuthElement) oldListIterator.next();
    Position pos = knuthElement.getPosition();
    LeafPosition leafPos = (LeafPosition) pos.getPosition(depth);
    int index = leafPos.getLeafPos();
    //element could refer to '-1' position, for non-collapsed spaces (?)
    if (index > -1) {
        AreaInfo areaInfo = getAreaInfo(index);
        areaInfo.letterSpaceCount++;/*from  ww w . j  a  v a  2 s .  c o  m*/
        areaInfo.addToAreaIPD(letterSpaceIPD);
        if (TextLayoutManager.BREAK_CHARS.indexOf(foText.charAt(tempStart - 1)) >= 0) {
            // the last character could be used as a line break
            // append new elements to oldList
            oldListIterator = oldList.listIterator(oldList.size());
            oldListIterator
                    .add(new KnuthPenalty(0, KnuthPenalty.FLAGGED_PENALTY, true, auxiliaryPosition, false));
            oldListIterator.add(new KnuthGlue(letterSpaceIPD, auxiliaryPosition, false));
        } else if (letterSpaceIPD.isStiff()) {
            // constant letter space: replace the box
            // give it the unwrapped position of the replaced element
            oldListIterator.set(new KnuthInlineBox(areaInfo.areaIPD.getOpt(), alignmentContext, pos, false));
        } else {
            // adjustable letter space: replace the glue
            oldListIterator.next(); // this would return the penalty element
            oldListIterator.next(); // this would return the glue element
            oldListIterator.set(
                    new KnuthGlue(letterSpaceIPD.mult(areaInfo.letterSpaceCount), auxiliaryPosition, true));
        }
    }
    return oldList;
}

From source file:org.trnltk.experiment.morphology.ambiguity.DataDiffUtil.java

/**
 * Reduce the number of edits by eliminating semantically trivial equalities.
 *
 * @param diffs LinkedList of Diff objects.
 *///from w w w. j  ava 2 s  .c  o m
public void diff_cleanupSemantic(LinkedList<Diff<T>> diffs) {
    if (diffs.isEmpty()) {
        return;
    }
    boolean changes = false;
    Stack<Diff<T>> equalities = new Stack<Diff<T>>(); // Stack of qualities.
    List<T> lastequality = null; // Always equal to equalities.lastElement().text
    ListIterator<Diff<T>> pointer = diffs.listIterator();
    // Number of characters that changed prior to the equality.
    int length_insertions1 = 0;
    int length_deletions1 = 0;
    // Number of characters that changed after the equality.
    int length_insertions2 = 0;
    int length_deletions2 = 0;
    Diff<T> thisDiff = pointer.next();
    while (thisDiff != null) {
        if (thisDiff.operation == Operation.EQUAL) {
            // Equality found.
            equalities.push(thisDiff);
            length_insertions1 = length_insertions2;
            length_deletions1 = length_deletions2;
            length_insertions2 = 0;
            length_deletions2 = 0;
            lastequality = thisDiff.text;
        } else {
            // An insertion or deletion.
            if (thisDiff.operation == Operation.INSERT) {
                length_insertions2 += thisDiff.text.size();
            } else {
                length_deletions2 += thisDiff.text.size();
            }
            // Eliminate an equality that is smaller or equal to the edits on both
            // sides of it.
            if (lastequality != null && (lastequality.size() <= Math.max(length_insertions1, length_deletions1))
                    && (lastequality.size() <= Math.max(length_insertions2, length_deletions2))) {
                //System.out.println("Splitting: '" + lastequality + "'");
                // Walk back to offending equality.
                while (thisDiff != equalities.lastElement()) {
                    thisDiff = pointer.previous();
                }
                pointer.next();

                // Replace equality with a delete.
                pointer.set(new Diff(Operation.DELETE, lastequality));
                // Insert a corresponding an insert.
                pointer.add(new Diff(Operation.INSERT, lastequality));

                equalities.pop(); // Throw away the equality we just deleted.
                if (!equalities.empty()) {
                    // Throw away the previous equality (it needs to be reevaluated).
                    equalities.pop();
                }
                if (equalities.empty()) {
                    // There are no previous equalities, walk back to the start.
                    while (pointer.hasPrevious()) {
                        pointer.previous();
                    }
                } else {
                    // There is a safe equality we can fall back to.
                    thisDiff = equalities.lastElement();
                    while (thisDiff != pointer.previous()) {
                        // Intentionally empty loop.
                    }
                }

                length_insertions1 = 0; // Reset the counters.
                length_insertions2 = 0;
                length_deletions1 = 0;
                length_deletions2 = 0;
                lastequality = null;
                changes = true;
            }
        }
        thisDiff = pointer.hasNext() ? pointer.next() : null;
    }

    // Normalize the diff.
    if (changes) {
        diff_cleanupMerge(diffs);
    }
    diff_cleanupSemanticLossless(diffs);

    // Find any overlaps between deletions and insertions.
    // e.g: <del>abcxxx</del><ins>xxxdef</ins>
    //   -> <del>abc</del>xxx<ins>def</ins>
    // e.g: <del>xxxabc</del><ins>defxxx</ins>
    //   -> <ins>def</ins>xxx<del>abc</del>
    // Only extract an overlap if it is as big as the edit ahead or behind it.
    pointer = diffs.listIterator();
    Diff<T> prevDiff = null;
    thisDiff = null;
    if (pointer.hasNext()) {
        prevDiff = pointer.next();
        if (pointer.hasNext()) {
            thisDiff = pointer.next();
        }
    }
    while (thisDiff != null) {
        if (prevDiff.operation == Operation.DELETE && thisDiff.operation == Operation.INSERT) {
            List<T> deletion = prevDiff.text;
            List<T> insertion = thisDiff.text;
            int overlap_length1 = this.diff_commonOverlap(deletion, insertion);
            int overlap_length2 = this.diff_commonOverlap(insertion, deletion);
            if (overlap_length1 >= overlap_length2) {
                if (overlap_length1 >= deletion.size() / 2.0 || overlap_length1 >= insertion.size() / 2.0) {
                    // Overlap found. Insert an equality and trim the surrounding edits.
                    pointer.previous();
                    pointer.add(new Diff<T>(Operation.EQUAL, insertion.subList(0, overlap_length1)));
                    prevDiff.text = deletion.subList(0, deletion.size() - overlap_length1);
                    thisDiff.text = insertion.subList(overlap_length1, insertion.size());
                    // pointer.add inserts the element before the cursor, so there is
                    // no need to step past the new element.
                }
            } else {
                if (overlap_length2 >= deletion.size() / 2.0 || overlap_length2 >= insertion.size() / 2.0) {
                    // Reverse overlap found.
                    // Insert an equality and swap and trim the surrounding edits.
                    pointer.previous();
                    pointer.add(new Diff<T>(Operation.EQUAL, deletion.subList(0, overlap_length2)));
                    prevDiff.operation = Operation.INSERT;
                    prevDiff.text = insertion.subList(0, insertion.size() - overlap_length2);
                    thisDiff.operation = Operation.DELETE;
                    thisDiff.text = deletion.subList(overlap_length2, deletion.size());
                    // pointer.add inserts the element before the cursor, so there is
                    // no need to step past the new element.
                }
            }
            thisDiff = pointer.hasNext() ? pointer.next() : null;
        }
        prevDiff = thisDiff;
        thisDiff = pointer.hasNext() ? pointer.next() : null;
    }
}

From source file:com.koma.music.service.MusicService.java

/**
 * Removes the range of tracks specified from the play list. If a file
 * within the range is the file currently being played, playback will move
 * to the next file after the range.//from   w  ww. j a  v a2 s .  co  m
 *
 * @param first The first file to be removed
 * @param last  The last file to be removed
 * @return the number of tracks deleted
 */
private int removeTracksInternal(int first, int last) {
    synchronized (this) {
        if (last < first) {
            return 0;
        } else if (first < 0) {
            first = 0;
        } else if (last >= mPlaylist.size()) {
            last = mPlaylist.size() - 1;
        }

        boolean gotonext = false;
        if (first <= mPlayPos && mPlayPos <= last) {
            mPlayPos = first;
            gotonext = true;
        } else if (mPlayPos > last) {
            mPlayPos -= last - first + 1;
        }
        final int numToRemove = last - first + 1;

        if (first == 0 && last == mPlaylist.size() - 1) {
            mPlayPos = -1;
            mNextPlayPos = -1;
            mPlaylist.clear();
            mHistory.clear();
        } else {
            for (int i = 0; i < numToRemove; i++) {
                mPlaylist.remove(first);
            }

            // remove the items from the history
            // this is not ideal as the history shouldn't be impacted by this
            // but since we are removing items from the array, it will throw
            // an exception if we keep it around.  Idealistically with the queue
            // rewrite this should be all be fixed
            // https://cyanogen.atlassian.net/browse/MUSIC-44
            ListIterator<Integer> positionIterator = mHistory.listIterator();
            while (positionIterator.hasNext()) {
                int pos = positionIterator.next();
                if (pos >= first && pos <= last) {
                    positionIterator.remove();
                } else if (pos > last) {
                    positionIterator.set(pos - numToRemove);
                }
            }
        }
        if (gotonext) {
            if (mPlaylist.size() == 0) {
                stop(true);
                mPlayPos = -1;
                closeCursor();
            } else {
                if (mShuffleMode != MusicServiceConstants.SHUFFLE_NONE) {
                    mPlayPos = getNextPosition(true);
                } else if (mPlayPos >= mPlaylist.size()) {
                    mPlayPos = 0;
                }
                final boolean wasPlaying = isPlaying();
                stop(false);
                openCurrentAndNext();
                if (wasPlaying) {
                    play();
                }
            }
            notifyChange(META_CHANGED);
        }
        return last - first + 1;
    }
}

From source file:com.bluros.music.MusicService.java

private int removeTracksInternal(int first, int last) {
    synchronized (this) {
        if (last < first) {
            return 0;
        } else if (first < 0) {
            first = 0;//from w  w  w . j av  a 2 s. c  om
        } else if (last >= mPlaylist.size()) {
            last = mPlaylist.size() - 1;
        }

        boolean gotonext = false;
        if (first <= mPlayPos && mPlayPos <= last) {
            mPlayPos = first;
            gotonext = true;
        } else if (mPlayPos > last) {
            mPlayPos -= last - first + 1;
        }
        final int numToRemove = last - first + 1;

        if (first == 0 && last == mPlaylist.size() - 1) {
            mPlayPos = -1;
            mNextPlayPos = -1;
            mPlaylist.clear();
            mHistory.clear();
        } else {
            for (int i = 0; i < numToRemove; i++) {
                mPlaylist.remove(first);
            }

            ListIterator<Integer> positionIterator = mHistory.listIterator();
            while (positionIterator.hasNext()) {
                int pos = positionIterator.next();
                if (pos >= first && pos <= last) {
                    positionIterator.remove();
                } else if (pos > last) {
                    positionIterator.set(pos - numToRemove);
                }
            }
        }
        if (gotonext) {
            if (mPlaylist.size() == 0) {
                stop(true);
                mPlayPos = -1;
                closeCursor();
            } else {
                if (mShuffleMode != SHUFFLE_NONE) {
                    mPlayPos = getNextPosition(true);
                } else if (mPlayPos >= mPlaylist.size()) {
                    mPlayPos = 0;
                }
                final boolean wasPlaying = isPlaying();
                stop(false);
                openCurrentAndNext();
                if (wasPlaying) {
                    play();
                }
            }
            notifyChange(META_CHANGED);
        }
        return last - first + 1;
    }
}

From source file:com.av.remusic.service.MediaService.java

private int removeTracksInternal(int first, int last) {
    synchronized (this) {
        if (last < first) {
            return 0;
        } else if (first < 0) {
            first = 0;// w  w  w  . j  a  v  a2  s .c o m
        } else if (last >= mPlaylist.size()) {
            last = mPlaylist.size() - 1;
        }

        boolean gotonext = false;
        if (first <= mPlayPos && mPlayPos <= last) {
            mPlayPos = first;
            gotonext = true;
        } else if (mPlayPos > last) {
            mPlayPos -= last - first + 1;
        }
        final int numToRemove = last - first + 1;

        if (first == 0 && last == mPlaylist.size() - 1) {
            mPlayPos = -1;
            mNextPlayPos = -1;
            mPlaylist.clear();
            mHistory.clear();
        } else {
            for (int i = 0; i < numToRemove; i++) {
                mPlaylistInfo.remove(mPlaylist.get(first).mId);
                mPlaylist.remove(first);

            }

            ListIterator<Integer> positionIterator = mHistory.listIterator();
            while (positionIterator.hasNext()) {
                int pos = positionIterator.next();
                if (pos >= first && pos <= last) {
                    positionIterator.remove();
                } else if (pos > last) {
                    positionIterator.set(pos - numToRemove);
                }
            }
        }
        if (gotonext) {
            if (mPlaylist.size() == 0) {
                stop(true);
                mPlayPos = -1;
                closeCursor();
            } else {
                if (mShuffleMode != SHUFFLE_NONE) {
                    mPlayPos = getNextPosition(true);
                } else if (mPlayPos >= mPlaylist.size()) {
                    mPlayPos = 0;
                }
                final boolean wasPlaying = isPlaying();
                stop(false);
                openCurrentAndNext();
                if (wasPlaying) {
                    play();
                }
            }
            notifyChange(META_CHANGED);
        }
        return last - first + 1;
    }
}

From source file:org.trnltk.experiment.morphology.ambiguity.DataDiffUtil.java

/**
 * Reduce the number of edits by eliminating operationally trivial equalities.
 *
 * @param diffs LinkedList of Diff objects.
 *//*from   w  w  w  . j  a v a 2s . c  o  m*/
public void diff_cleanupEfficiency(LinkedList<Diff<T>> diffs) {
    if (diffs.isEmpty()) {
        return;
    }
    boolean changes = false;
    Stack<Diff> equalities = new Stack<Diff>(); // Stack of equalities.
    List<T> lastequality = null; // Always equal to equalities.lastElement().text
    ListIterator<Diff<T>> pointer = diffs.listIterator();
    // Is there an insertion operation before the last equality.
    boolean pre_ins = false;
    // Is there a deletion operation before the last equality.
    boolean pre_del = false;
    // Is there an insertion operation after the last equality.
    boolean post_ins = false;
    // Is there a deletion operation after the last equality.
    boolean post_del = false;
    Diff<T> thisDiff = pointer.next();
    Diff<T> safeDiff = thisDiff; // The last Diff that is known to be unsplitable.
    while (thisDiff != null) {
        if (thisDiff.operation == Operation.EQUAL) {
            // Equality found.
            if (thisDiff.text.size() < Diff_EditCost && (post_ins || post_del)) {
                // Candidate found.
                equalities.push(thisDiff);
                pre_ins = post_ins;
                pre_del = post_del;
                lastequality = thisDiff.text;
            } else {
                // Not a candidate, and can never become one.
                equalities.clear();
                lastequality = null;
                safeDiff = thisDiff;
            }
            post_ins = post_del = false;
        } else {
            // An insertion or deletion.
            if (thisDiff.operation == Operation.DELETE) {
                post_del = true;
            } else {
                post_ins = true;
            }
            /*
             * Five types to be split:
             * <ins>A</ins><del>B</del>XY<ins>C</ins><del>D</del>
             * <ins>A</ins>X<ins>C</ins><del>D</del>
             * <ins>A</ins><del>B</del>X<ins>C</ins>
             * <ins>A</del>X<ins>C</ins><del>D</del>
             * <ins>A</ins><del>B</del>X<del>C</del>
             */
            if (lastequality != null && ((pre_ins && pre_del && post_ins && post_del)
                    || ((lastequality.size() < Diff_EditCost / 2) && ((pre_ins ? 1 : 0) + (pre_del ? 1 : 0)
                            + (post_ins ? 1 : 0) + (post_del ? 1 : 0)) == 3))) {
                //System.out.println("Splitting: '" + lastequality + "'");
                // Walk back to offending equality.
                while (thisDiff != equalities.lastElement()) {
                    thisDiff = pointer.previous();
                }
                pointer.next();

                // Replace equality with a delete.
                pointer.set(new Diff(Operation.DELETE, lastequality));
                // Insert a corresponding an insert.
                pointer.add(thisDiff = new Diff(Operation.INSERT, lastequality));

                equalities.pop(); // Throw away the equality we just deleted.
                lastequality = null;
                if (pre_ins && pre_del) {
                    // No changes made which could affect previous entry, keep going.
                    post_ins = post_del = true;
                    equalities.clear();
                    safeDiff = thisDiff;
                } else {
                    if (!equalities.empty()) {
                        // Throw away the previous equality (it needs to be reevaluated).
                        equalities.pop();
                    }
                    if (equalities.empty()) {
                        // There are no previous questionable equalities,
                        // walk back to the last known safe diff.
                        thisDiff = safeDiff;
                    } else {
                        // There is an equality we can fall back to.
                        thisDiff = equalities.lastElement();
                    }
                    while (thisDiff != pointer.previous()) {
                        // Intentionally empty loop.
                    }
                    post_ins = post_del = false;
                }

                changes = true;
            }
        }
        thisDiff = pointer.hasNext() ? pointer.next() : null;
    }

    if (changes) {
        diff_cleanupMerge(diffs);
    }
}

From source file:com.cyanogenmod.eleven.MusicPlaybackService.java

/**
 * Removes the range of tracks specified from the play list. If a file
 * within the range is the file currently being played, playback will move
 * to the next file after the range.// w  ww . j a  va 2  s .c  om
 *
 * @param first The first file to be removed
 * @param last  The last file to be removed
 * @return the number of tracks deleted
 */
private int removeTracksInternal(int first, int last) {
    synchronized (this) {
        if (last < first) {
            return 0;
        } else if (first < 0) {
            first = 0;
        } else if (last >= mPlaylist.size()) {
            last = mPlaylist.size() - 1;
        }

        boolean gotonext = false;
        if (first <= mPlayPos && mPlayPos <= last) {
            mPlayPos = first;
            gotonext = true;
        } else if (mPlayPos > last) {
            mPlayPos -= last - first + 1;
        }
        final int numToRemove = last - first + 1;

        if (first == 0 && last == mPlaylist.size() - 1) {
            mPlayPos = -1;
            mNextPlayPos = -1;
            mPlaylist.clear();
            mHistory.clear();
        } else {
            for (int i = 0; i < numToRemove; i++) {
                mPlaylist.remove(first);
            }

            // remove the items from the history
            // this is not ideal as the history shouldn't be impacted by this
            // but since we are removing items from the array, it will throw
            // an exception if we keep it around.  Idealistically with the queue
            // rewrite this should be all be fixed
            // https://cyanogen.atlassian.net/browse/MUSIC-44
            ListIterator<Integer> positionIterator = mHistory.listIterator();
            while (positionIterator.hasNext()) {
                int pos = positionIterator.next();
                if (pos >= first && pos <= last) {
                    positionIterator.remove();
                } else if (pos > last) {
                    positionIterator.set(pos - numToRemove);
                }
            }
        }
        if (gotonext) {
            if (mPlaylist.size() == 0) {
                stop(true);
                mPlayPos = -1;
                closeCursor();
            } else {
                if (mShuffleMode != SHUFFLE_NONE) {
                    mPlayPos = getNextPosition(true);
                } else if (mPlayPos >= mPlaylist.size()) {
                    mPlayPos = 0;
                }
                final boolean wasPlaying = isPlaying();
                stop(false);
                openCurrentAndNext();
                if (wasPlaying) {
                    play();
                }
            }
            notifyChange(META_CHANGED);
        }
        return last - first + 1;
    }
}

From source file:org.oscm.serviceprovisioningservice.bean.ServiceProvisioningServiceBean.java

private void cleanUpProducts(List<Product> products, Set<Long> prodKeysToBeRemoved,
        Map<Long, Product> prodKeysToBeReplaced) {
    ListIterator<Product> productsIterator = products.listIterator();
    while (productsIterator.hasNext()) {
        Long productKey = Long.valueOf(productsIterator.next().getKey());
        if (prodKeysToBeRemoved.contains(productKey) && !prodKeysToBeReplaced.containsKey(productKey)) {
            // remove obsolete products (only if they won't be replaced)
            productsIterator.remove();//  ww w  .j av  a2s  .c o m
        } else {
            Product specificCopy = prodKeysToBeReplaced.get(productKey);
            if (specificCopy != null) {
                // replace templates by specific copies
                productsIterator.set(specificCopy);
            }
        }
    }
}