Example usage for java.util ListIterator previous

List of usage examples for java.util ListIterator previous

Introduction

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

Prototype

E previous();

Source Link

Document

Returns the previous element in the list and moves the cursor position backwards.

Usage

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.
 *//*  w  ww . j av  a2s .  c om*/
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.ludoscity.findmybikes.activities.NearbyActivity.java

@Override
public void onFavoriteSheetEditDone() {

    ArrayList<FavoriteItemBase> newlyOrderedFavList = new ArrayList<>();
    newlyOrderedFavList.addAll(mFavoriteRecyclerViewAdapter.getCurrentFavoriteList());

    DBHelper.dropFavoriteAll(this);
    mFavoriteRecyclerViewAdapter.clearFavoriteList();

    ListIterator<FavoriteItemBase> li = newlyOrderedFavList.listIterator(newlyOrderedFavList.size());

    while (li.hasPrevious()) {
        addFavorite(li.previous(), true, false);
    }//from  w w  w.  j  av a2s  .  co m
}

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

/**
 * Look for single edits surrounded on both sides by equalities
 * which can be shifted sideways to align the edit to a word boundary.
 * e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
 *
 * @param diffs LinkedList of Diff objects.
 *//*w w w .j  a v a2 s .  c  o  m*/
public void diff_cleanupSemanticLossless(LinkedList<Diff<T>> diffs) {
    List<T> equality1, edit, equality2;
    List<T> commonString;
    int commonOffset;
    int score, bestScore;
    List<T> bestEquality1, bestEdit, bestEquality2;
    // Create a new iterator at the start.
    ListIterator<Diff<T>> pointer = diffs.listIterator();
    Diff<T> prevDiff = pointer.hasNext() ? pointer.next() : null;
    Diff<T> thisDiff = pointer.hasNext() ? pointer.next() : null;
    Diff<T> nextDiff = pointer.hasNext() ? pointer.next() : null;
    // Intentionally ignore the first and last element (don't need checking).
    while (nextDiff != null) {
        if (prevDiff.operation == Operation.EQUAL && nextDiff.operation == Operation.EQUAL) {
            // This is a single edit surrounded by equalities.
            equality1 = prevDiff.text;
            edit = thisDiff.text;
            equality2 = nextDiff.text;

            // First, shift the edit as far left as possible.
            commonOffset = diff_commonSuffix(equality1, edit);
            if (commonOffset != 0) {
                commonString = edit.subList(edit.size() - commonOffset, edit.size());
                equality1 = equality1.subList(0, equality1.size() - commonOffset);
                edit = ListUtils.union(commonString, edit.subList(0, edit.size() - commonOffset));
                equality2 = ListUtils.union(commonString, equality2);
            }

            // Second, step character by character right, looking for the best fit.
            bestEquality1 = equality1;
            bestEdit = edit;
            bestEquality2 = equality2;
            bestScore = diff_cleanupSemanticScore(equality1, edit) + diff_cleanupSemanticScore(edit, equality2);
            while (edit.size() != 0 && equality2.size() != 0 && edit.get(0).equals(equality2.get(0))) {
                equality1 = ListUtils.union(equality1, Arrays.asList(edit.get(0)));
                edit = ListUtils.union(edit.subList(1, edit.size()), Arrays.asList(equality2.get(0)));
                equality2 = equality2.subList(1, equality2.size());
                score = diff_cleanupSemanticScore(equality1, edit) + diff_cleanupSemanticScore(edit, equality2);
                // The >= encourages trailing rather than leading whitespace on edits.
                if (score >= bestScore) {
                    bestScore = score;
                    bestEquality1 = equality1;
                    bestEdit = edit;
                    bestEquality2 = equality2;
                }
            }

            if (!prevDiff.text.equals(bestEquality1)) {
                // We have an improvement, save it back to the diff.
                if (bestEquality1.size() != 0) {
                    prevDiff.text = bestEquality1;
                } else {
                    pointer.previous(); // Walk past nextDiff.
                    pointer.previous(); // Walk past thisDiff.
                    pointer.previous(); // Walk past prevDiff.
                    pointer.remove(); // Delete prevDiff.
                    pointer.next(); // Walk past thisDiff.
                    pointer.next(); // Walk past nextDiff.
                }
                thisDiff.text = bestEdit;
                if (bestEquality2.size() != 0) {
                    nextDiff.text = bestEquality2;
                } else {
                    pointer.remove(); // Delete nextDiff.
                    nextDiff = thisDiff;
                    thisDiff = prevDiff;
                }
            }
        }
        prevDiff = thisDiff;
        thisDiff = nextDiff;
        nextDiff = pointer.hasNext() ? pointer.next() : null;
    }
}

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

/**
 * Find hyphenation points for every word in the current paragraph.
 *
 * @param currPar the paragraph whose words will be hyphenated
 *///from   w  ww.  j  a va 2s .co  m
private void findHyphenationPoints(Paragraph currPar) {
    // hyphenate every word
    ListIterator currParIterator = currPar.listIterator(currPar.ignoreAtStart);
    // list of TLM involved in hyphenation
    List updateList = new LinkedList();
    KnuthElement firstElement;
    KnuthElement nextElement;
    // current InlineLevelLayoutManager
    InlineLevelLayoutManager currLM = null;
    // number of KnuthBox elements containing word fragments
    int boxCount;
    // number of auxiliary KnuthElements between KnuthBoxes
    int auxCount;
    StringBuffer sbChars;

    // find all hyphenation points
    while (currParIterator.hasNext()) {
        firstElement = (KnuthElement) currParIterator.next();
        //
        if (firstElement.getLayoutManager() != currLM) {
            currLM = (InlineLevelLayoutManager) firstElement.getLayoutManager();
            if (currLM != null) {
                updateList.add(new Update(currLM, currParIterator.previousIndex()));
            } else {
                break;
            }
        } else if (currLM == null) {
            break;
        }

        // collect word fragments, ignoring auxiliary elements;
        // each word fragment was created by a different TextLM
        if (firstElement.isBox() && !firstElement.isAuxiliary()) {
            boxCount = 1;
            auxCount = 0;
            sbChars = new StringBuffer();
            sbChars.append(currLM.getWordChars(firstElement.getPosition()));
            // look if next elements are boxes too
            while (currParIterator.hasNext()) {
                nextElement = (KnuthElement) currParIterator.next();
                if (nextElement.isBox() && !nextElement.isAuxiliary()) {
                    // a non-auxiliary KnuthBox: append word chars
                    if (currLM != nextElement.getLayoutManager()) {
                        currLM = (InlineLevelLayoutManager) nextElement.getLayoutManager();
                        updateList.add(new Update(currLM, currParIterator.previousIndex()));
                    }
                    // append text to recreate the whole word
                    boxCount++;
                    sbChars.append(currLM.getWordChars(nextElement.getPosition()));
                } else if (!nextElement.isAuxiliary()) {
                    // a non-auxiliary non-box KnuthElement: stop
                    // go back to the last box or auxiliary element
                    currParIterator.previous();
                    break;
                } else {
                    if (currLM != nextElement.getLayoutManager()) {
                        currLM = (InlineLevelLayoutManager) nextElement.getLayoutManager();
                        updateList.add(new Update(currLM, currParIterator.previousIndex()));
                    }
                    // an auxiliary KnuthElement: simply ignore it
                    auxCount++;
                }
            }
            if (log.isTraceEnabled()) {
                log.trace(" Word to hyphenate: " + sbChars.toString());
            }
            // find hyphenation points
            HyphContext hc = getHyphenContext(sbChars);
            // ask each LM to hyphenate its word fragment
            if (hc != null) {
                KnuthElement element = null;
                for (int i = 0; i < (boxCount + auxCount); i++) {
                    currParIterator.previous();
                }
                for (int i = 0; i < (boxCount + auxCount); i++) {
                    element = (KnuthElement) currParIterator.next();
                    if (element.isBox() && !element.isAuxiliary()) {
                        ((InlineLevelLayoutManager) element.getLayoutManager()).hyphenate(element.getPosition(),
                                hc);
                    } else {
                        // nothing to do, element is an auxiliary KnuthElement
                    }
                }
            }
        }
    }
    processUpdates(currPar, updateList);
}

From source file:net.sourceforge.fenixedu.domain.student.Registration.java

final public ICurriculum getCurriculum(final DateTime when, final ExecutionYear executionYear,
        final CycleType cycleType) {
    if (getStudentCurricularPlansSet().isEmpty()) {
        return Curriculum.createEmpty(executionYear);
    }//  www  .j  a  v a  2s. c  o m

    if (getDegreeType().isBolonhaType()) {
        final StudentCurricularPlan studentCurricularPlan = getLastStudentCurricularPlan();
        if (studentCurricularPlan == null) {
            return Curriculum.createEmpty(executionYear);
        }

        if (cycleType == null) {
            return studentCurricularPlan.getCurriculum(when, executionYear);
        }

        final CycleCurriculumGroup cycleCurriculumGroup = studentCurricularPlan.getCycle(cycleType);
        if (cycleCurriculumGroup == null) {
            return Curriculum.createEmpty(executionYear);
        }

        return cycleCurriculumGroup.getCurriculum(when, executionYear);
    } else {
        final List<StudentCurricularPlan> sortedSCPs = getSortedStudentCurricularPlans();
        final ListIterator<StudentCurricularPlan> sortedSCPsIterator = sortedSCPs
                .listIterator(sortedSCPs.size());
        final StudentCurricularPlan lastStudentCurricularPlan = sortedSCPsIterator.previous();

        final ICurriculum curriculum;
        if (lastStudentCurricularPlan.isBoxStructure()) {
            curriculum = lastStudentCurricularPlan.getCurriculum(when, executionYear);

            for (; sortedSCPsIterator.hasPrevious();) {
                final StudentCurricularPlan studentCurricularPlan = sortedSCPsIterator.previous();
                if (executionYear == null
                        || studentCurricularPlan.getStartExecutionYear().isBeforeOrEquals(executionYear)) {
                    ((Curriculum) curriculum).add(studentCurricularPlan.getCurriculum(when, executionYear));
                }
            }

            return curriculum;

        } else {
            curriculum = new StudentCurriculum(this, executionYear);
        }

        return curriculum;
    }
}

From source file:org.regenstrief.util.Util.java

/**
 * Resets a ListIterator//from   w w  w .  j a v a 2s  .c  om
 * 
 * @param i the ListIterator
 * @param <E> the element type
 * @return the ListIterator
 **/
public final static <E> ListIterator<E> reset(final ListIterator<E> i) {
    for (; (i != null) && i.hasPrevious(); i.previous()) {
        ;
    }
    return i;
}

From source file:com.gst.portfolio.loanaccount.domain.Loan.java

private LocalDate determineExpectedMaturityDate() {
    final int numberOfInstallments = this.repaymentScheduleInstallments.size();
    List<LoanRepaymentScheduleInstallment> installments = getRepaymentScheduleInstallments();
    LocalDate maturityDate = installments.get(numberOfInstallments - 1).getDueDate();
    ListIterator<LoanRepaymentScheduleInstallment> iterator = installments.listIterator(numberOfInstallments);
    while (iterator.hasPrevious()) {
        LoanRepaymentScheduleInstallment loanRepaymentScheduleInstallment = iterator.previous();
        if (!loanRepaymentScheduleInstallment.isRecalculatedInterestComponent()) {
            maturityDate = loanRepaymentScheduleInstallment.getDueDate();
            break;
        }// w  ww .  j  a v  a 2  s  .  c o m
    }
    return maturityDate;
}