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.apache.fop.layoutmgr.table.RowPainter.java

private int computeContentLength(PrimaryGridUnit pgu, int startIndex, int endIndex) {
    if (startIndex > endIndex) {
        // May happen if the cell contributes no content on the current page (empty
        // cell, in most cases)
        return 0;
    } else {/*from  w  ww .  ja  v  a  2  s  .co  m*/
        ListIterator iter = pgu.getElements().listIterator(startIndex);
        // Skip from the content length calculation glues and penalties occurring at the
        // beginning of the page
        boolean nextIsBox = false;
        while (iter.nextIndex() <= endIndex && !nextIsBox) {
            nextIsBox = ((KnuthElement) iter.next()).isBox();
        }
        int len = 0;
        if (((KnuthElement) iter.previous()).isBox()) {
            while (iter.nextIndex() < endIndex) {
                KnuthElement el = (KnuthElement) iter.next();
                if (el.isBox() || el.isGlue()) {
                    len += el.getWidth();
                }
            }
            len += ActiveCell.getElementContentLength((KnuthElement) iter.next());
        }
        return len;
    }
}

From source file:com.offbynull.voip.kademlia.model.NodeMostRecentSet.java

public ActivityChangeSet touch(Instant time, Node node, boolean allowLinkMismatch) {
    Validate.notNull(time);//  w  w  w .  ja  va 2 s  .  co m
    Validate.notNull(node);

    Id nodeId = node.getId();

    InternalValidate.matchesLength(baseId.getBitLength(), nodeId);
    //        Validate.isTrue(!nodeId.equals(baseId)); // Don't reject adding self

    // TODO: You can make this way more efficient if you used something like MultiTreeSet (guava) and sorted based on entry time

    // Remove existing entry
    Activity oldEntry = null;
    ListIterator<Activity> it = entries.listIterator();
    while (it.hasNext()) {
        Activity entry = it.next();

        Id entryId = entry.getNode().getId();

        if (entryId.equals(nodeId)) {
            if (!allowLinkMismatch) {
                InternalValidate.matchesLink(entry.getNode(), node);
            }

            // remove
            it.remove();
            oldEntry = entry;
            break;
        }
    }

    // Add entry
    Activity newEntry = new Activity(node, time);
    it = entries.listIterator(entries.size());
    boolean added = false;
    while (it.hasPrevious()) {
        Activity entry = it.previous();

        if (entry.getTime().isBefore(time)) {
            it.next(); // move forward 1 space, we want to add to element just after entry
            it.add(newEntry);
            added = true;
            break;
        }
    }

    if (!added) { // special case where newEntry needs to be added at the end of entries, not handled by loop above
        entries.addFirst(newEntry);
    }

    // Set has become too large, remove the item with the earliest time
    Activity discardedEntry = null;
    if (entries.size() > maxSize) {
        // if the node removed with the earliest time is the one we just added, then report that node couldn't be added
        discardedEntry = entries.removeFirst();
        if (discardedEntry.equals(newEntry)) {
            return ActivityChangeSet.NO_CHANGE;
        }
    }

    // Add successful
    if (oldEntry != null) {
        Validate.validState(discardedEntry == null); // sanity check, must not have discarded anything

        // updated existing node
        return ActivityChangeSet.updated(newEntry);
    } else {
        // if block above ensures oldEntry is null if we're in this else block, so sanity check below isn't nessecary
        // Validate.validState(oldEntry == null); // sanity check, node being touched must not have already existed

        // added new node
        Collection<Activity> addedEntries = singletonList(newEntry);
        Collection<Activity> removedEntries = discardedEntry == null ? emptyList()
                : singletonList(discardedEntry);
        Collection<Activity> updatedEntries = emptyList();
        return new ActivityChangeSet(addedEntries, removedEntries, updatedEntries);
    }
}

From source file:org.commonjava.maven.ext.manip.impl.DependencyManipulator.java

/**
 * This will load the remote overrides. It will first try to load any overrides that might have
 * been prepopulated by the REST scanner, failing that it will load from a remote POM file.
 *
 * @param state the dependency state/*from  w  w  w  .j a  va  2s  .  co m*/
 * @return the loaded overrides
 * @throws ManipulationException if an error occurs.
 */
private Map<ArtifactRef, String> loadRemoteOverrides(final DependencyState state) throws ManipulationException {
    Map<ArtifactRef, String> overrides = state.getRemoteRESTOverrides();

    if (overrides == null) {
        overrides = new LinkedHashMap<>();
        final List<ProjectVersionRef> gavs = state.getRemoteBOMDepMgmt();

        if (gavs == null || gavs.isEmpty()) {
            return overrides;
        }

        final ListIterator<ProjectVersionRef> iter = gavs.listIterator(gavs.size());
        // Iterate in reverse order so that the first GAV in the list overwrites the last
        while (iter.hasPrevious()) {
            final ProjectVersionRef ref = iter.previous();
            overrides.putAll(effectiveModelBuilder.getRemoteDependencyVersionOverrides(ref));
        }
    }
    return overrides;
}

From source file:com.offbynull.peernetic.playground.chorddht.model.FingerTable.java

/**
 * Searches the finger table for the right-most occurrence of {@code ptr}.
 * @param ptr pointer to search for//from w w  w  .j a  v  a2s.  co m
 * @return index of occurrence, or -1 if not found
 * @throws NullPointerException if any arguments are {@code null}
 * @throws IllegalArgumentException if {@code ptr}'s id has a different limit bit size than the base pointer's id
 */
public int getMaximumIndex(Pointer ptr) {
    Validate.notNull(ptr);
    Validate.isTrue(IdUtils.getBitLength(ptr.getId()) == bitCount);

    Id id = ptr.getId();
    Validate.isTrue(IdUtils.getBitLength(id) == bitCount);

    ListIterator<InternalEntry> lit = table.listIterator(table.size());
    while (lit.hasPrevious()) {
        InternalEntry ie = lit.previous();

        if (ie.pointer.equals(ptr)) {
            return lit.previousIndex() + 1;
        }
    }

    return -1;
}

From source file:at.ofai.music.util.WormFileParseException.java

public void insert(Event newEvent, boolean uniqueTimes) {
    ListIterator<Event> li = l.listIterator();
    while (li.hasNext()) {
        int sgn = newEvent.compareTo(li.next());
        if (sgn < 0) {
            li.previous();
            break;
        } else if (uniqueTimes && (sgn == 0)) {
            li.remove();/*from  w  ww  . j  a v a 2  s.c om*/
            break;
        }
    }
    li.add(newEvent);
}

From source file:com.offbynull.peernetic.playground.chorddht.model.FingerTable.java

/**
 * Searches the finger table for the closest to the id being searched for (closest in terms of being {@code <}).
 *
 * @param id id being searched for//  www  .j a  v  a  2  s  . c  o  m
 * @return closest preceding pointer
 * @throws NullPointerException if any arguments are {@code null}
 * @throws IllegalArgumentException if {@code id}'s has a different limit bit size than base pointer's id
 */
public Pointer findClosestPreceding(Id id) {
    Validate.notNull(id);
    Validate.isTrue(IdUtils.getBitLength(id) == bitCount);

    Id selfId = basePtr.getId();

    InternalEntry foundEntry = null;
    ListIterator<InternalEntry> lit = table.listIterator(table.size());
    while (lit.hasPrevious()) {
        InternalEntry ie = lit.previous();
        // if finger[i] exists between n (exclusive) and id (exclusive)
        // then return it
        Id fingerId = ie.pointer.getId();
        if (fingerId.isWithin(selfId, false, id, false)) {
            foundEntry = ie;
            break;
        }
    }

    return foundEntry == null ? basePtr : foundEntry.pointer;
}

From source file:com.offbynull.peernetic.playground.chorddht.model.FingerTable.java

/**
 * Searches the finger table for the closest id to the id being searched for (closest in terms of being {@code <=} to).
 *
 * @param id id being searched for/*from  w w w  .ja  va  2  s  .c  om*/
 * @return closest pointer (closest in terms of being {@code <=} to)
 * @throws NullPointerException if any arguments are {@code null}
 * @throws IllegalArgumentException if {@code id}'s has a different limit bit size than base pointer's id
 */
public Pointer findClosest(Id id) {
    Validate.notNull(id);
    Validate.isTrue(IdUtils.getBitLength(id) == bitCount);

    Id selfId = basePtr.getId();

    InternalEntry foundEntry = null;
    ListIterator<InternalEntry> lit = table.listIterator(table.size());
    while (lit.hasPrevious()) {
        InternalEntry ie = lit.previous();
        // if finger[i] exists between n (exclusive) and id (exclusive)
        // then return it
        Id fingerId = ie.pointer.getId();
        if (fingerId.isWithin(selfId, false, id, true)) {
            foundEntry = ie;
            break;
        }
    }

    return foundEntry == null ? basePtr : foundEntry.pointer;
}

From source file:com.offbynull.peernetic.playground.chorddht.model.FingerTable.java

/**
 * Gets finger before {@code id}.// w ww .j a v  a  2  s.  c  o  m
 *
 * @param id id to search for
 * @return finger before (@code id}, or {@code null} if not found
 * @throws NullPointerException if any arguments are {@code null}
 * @throws IllegalArgumentException if {@code id} has a different limit bit size than base pointer's id, or if {@code id} is equivalent
 * to base pointer's id
 */
public Pointer getBefore(Id id) {
    Validate.notNull(id);
    Validate.isTrue(IdUtils.getBitLength(id) == bitCount);

    Id baseId = basePtr.getId();

    if (id.equals(baseId)) {
        throw new IllegalArgumentException();
    }

    ListIterator<InternalEntry> lit = table.listIterator(table.size());
    while (lit.hasPrevious()) {
        InternalEntry ie = lit.previous();
        Id testId = ie.pointer.getId();

        if (Id.comparePosition(baseId, id, testId) > 0) {
            return lit.hasPrevious() ? lit.previous().pointer : null;
        }
    }

    return null;
}

From source file:org.openhab.binding.freeswitch.internal.FreeswitchBinding.java

/**
 * update items on call end/*from   w w  w  .j  a  v  a2s .c  om*/
 * @param config
 */
private void endCallItemUpdate(FreeswitchBindingConfig config) {

    OnOffType activeState = OnOffType.OFF;
    ;
    CallType callType = (CallType) CallType.EMPTY;
    StringType callerId = StringType.EMPTY;

    /*
     * A channel has ended that has this item associated with it
     * We still need to check if this item is associated with other
     * channels.
     * We are going to iterate backwards to get the last added channel;
     */
    ListIterator<String> it = new ArrayList<String>(itemMap.keySet()).listIterator(itemMap.size());

    //if we get a match we will stop processing
    boolean match = false;
    while (it.hasPrevious()) {
        String uuid = it.previous();
        for (FreeswitchBindingConfig c : itemMap.get(uuid)) {
            if (c.getItemName().equals(config.getItemName())) {
                Channel channel = eventCache.get(uuid);
                activeState = OnOffType.ON;
                callType = channel.getCall();
                callerId = new StringType(String.format("%s : %s", channel.getEventHeader(CID_NAME),
                        channel.getEventHeader(CID_NUMBER)));
                match = true;
                break;
            }
        }
        if (match)
            break;
    }
    if (config.getItemType().isAssignableFrom(SwitchItem.class)) {

        eventPublisher.postUpdate(config.getItemName(), activeState);
    } else if (config.getItemType().isAssignableFrom(CallItem.class)) {

        eventPublisher.postUpdate(config.getItemName(), callType);
    } else if (config.getItemType().isAssignableFrom(StringItem.class)) {

        eventPublisher.postUpdate(config.getItemName(), callerId);
    } else {
        logger.warn("handleHangupCall - postUpdate for itemType '{}' is undefined", config.getItemName());
    }
}

From source file:com.offbynull.peernetic.playground.chorddht.model.FingerTable.java

/**
 * Removes all fingers before {@code id} (does not remove {@code id} itself).
 *
 * @param id id of which all fingers before it will be removed
 * @return number of fingers that were cleared
 * @throws NullPointerException if any arguments are {@code null}
 * @throws IllegalArgumentException if {@code id} has a different limit bit size than base pointer's id, or if {@code id} is equivalent
 * to base pointer's id/*from   w ww . j av  a  2s.c om*/
 */
public int clearBefore(Id id) {
    Validate.notNull(id);
    Validate.isTrue(IdUtils.getBitLength(id) == bitCount);

    Id baseId = basePtr.getId();

    if (id.equals(baseId)) {
        throw new IllegalArgumentException();
    }

    ListIterator<InternalEntry> lit = table.listIterator(table.size());
    while (lit.hasPrevious()) {
        InternalEntry ie = lit.previous();
        Id testId = ie.pointer.getId();

        if (Id.comparePosition(baseId, id, testId) > 0) {
            int position = lit.previousIndex() + 1;
            clearBefore(position);
            return position;
        }
    }

    return 0;
}