Example usage for java.util ListIterator remove

List of usage examples for java.util ListIterator remove

Introduction

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

Prototype

void remove();

Source Link

Document

Removes from the list the last element that was returned by #next or #previous (optional operation).

Usage

From source file:org.apache.sling.testing.clients.interceptors.StickyCookieInterceptor.java

public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
    final HttpClientContext clientContext = HttpClientContext.adapt(httpContext);
    List<Cookie> cookies = clientContext.getCookieStore().getCookies();
    boolean set = (null != StickyCookieHolder.getTestStickySessionCookie());
    boolean found = false;
    ListIterator<Cookie> it = cookies.listIterator();
    while (it.hasNext()) {
        Cookie cookie = it.next();/*from   w  w w  .j  a  va2s  .  c  o  m*/
        if (cookie.getName().equals(StickyCookieHolder.COOKIE_NAME)) {
            found = true;
            if (set) {
                // set the cookie with the value saved for each thread using the rule
                it.set(StickyCookieHolder.getTestStickySessionCookie());
            } else {
                // if the cookie is not set in TestStickySessionRule, remove it from here
                it.remove();
            }
        }
    }
    // if the cookie needs to be set from TestStickySessionRule but did not exist in the client cookie list, add it here.
    if (!found && set) {
        cookies.add(StickyCookieHolder.getTestStickySessionCookie());
    }
    BasicCookieStore cs = new BasicCookieStore();
    cs.addCookies(cookies.toArray(new Cookie[cookies.size()]));
    httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cs);
}

From source file:com.apigee.sdk.apm.http.impl.client.cache.CacheEntryUpdater.java

private void removeCacheEntry1xxWarnings(List<Header> cacheEntryHeaderList, HttpCacheEntry entry) {
    ListIterator<Header> cacheEntryHeaderListIter = cacheEntryHeaderList.listIterator();

    while (cacheEntryHeaderListIter.hasNext()) {
        String cacheEntryHeaderName = cacheEntryHeaderListIter.next().getName();

        if (HeaderConstants.WARNING.equals(cacheEntryHeaderName)) {
            for (Header cacheEntryWarning : entry.getHeaders(HeaderConstants.WARNING)) {
                if (cacheEntryWarning.getValue().startsWith("1")) {
                    cacheEntryHeaderListIter.remove();
                }//from   w  w w. jav a2s .c o  m
            }
        }
    }
}

From source file:org.kuali.rice.krms.impl.ui.TermSpecificationMaintainable.java

/**
 * For context removal, removes the given item from the persisted contextValidTerms collection on the data object.
 *
 * <p>Without this step, the context is only removed from a transient collection and the severed relationship will
 * never be persisted. </p>//from   w  w  w . j  a  v a  2  s .  co  m
 */
@Override
public void processCollectionDeleteLine(ViewModel viewModel, String collectionId, String collectionPath,
        int lineIndex) {
    super.processCollectionDeleteLine(viewModel, collectionId, collectionPath, lineIndex);

    List collection = ObjectPropertyUtils.getPropertyValue(viewModel, collectionPath);
    Object deletedItem = collection.get(lineIndex);

    // we only want to do our custom processing if a context has been deleted
    if (deletedItem == null || !(deletedItem instanceof ContextBo)) {
        return;
    }

    ContextBo context = (ContextBo) deletedItem;
    TermSpecificationBo termSpec = getDataObjectFromForm(viewModel);

    // find the context and remove it using the special powers of ListIterator
    ListIterator<ContextValidTermBo> contextValidTermListIter = termSpec.getContextValidTerms().listIterator();
    while (contextValidTermListIter.hasNext()) {
        ContextValidTermBo contextValidTerm = contextValidTermListIter.next();

        if (contextValidTerm.getContextId().equals(context.getId())) {
            contextValidTerm.setTermSpecification(null);
            contextValidTermListIter.remove();
        }
    }
}

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

public ActivityChangeSet touch(Instant time, Node node, boolean allowLinkMismatch) {
    Validate.notNull(time);//from   w  w  w .  j a v  a2s  . 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:com.opengamma.elsql.ElSqlParser.java

private void parseContainerSection(ContainerSqlFragment container, ListIterator<Line> lineIterator,
        int indent) {
    while (lineIterator.hasNext()) {
        Line line = lineIterator.next();
        if (line.isComment()) {
            lineIterator.remove();
            continue;
        }/*  w w w .ja v  a 2  s.  c o  m*/
        if (line.indent() <= indent) {
            lineIterator.previous();
            return;
        }
        String trimmed = line.lineTrimmed();
        if (trimmed.startsWith("@NAME")) {
            Matcher nameMatcher = NAME_PATTERN.matcher(trimmed);
            if (nameMatcher.matches() == false) {
                throw new IllegalArgumentException("@NAME found with invalid format: " + line);
            }
            NameSqlFragment nameFragment = new NameSqlFragment(nameMatcher.group(1));
            parseContainerSection(nameFragment, lineIterator, line.indent());
            if (nameFragment.getFragments().size() == 0) {
                throw new IllegalArgumentException("@NAME found with no subsequent indented lines: " + line);
            }
            container.addFragment(nameFragment);
            _namedFragments.put(nameFragment.getName(), nameFragment);

        } else if (indent < 0) {
            throw new IllegalArgumentException(
                    "Invalid fragment found at root level, only @NAME is permitted: " + line);

        } else if (trimmed.startsWith("@PAGING")) {
            Matcher pagingMatcher = PAGING_PATTERN.matcher(trimmed);
            if (pagingMatcher.matches() == false) {
                throw new IllegalArgumentException("@PAGING found with invalid format: " + line);
            }
            PagingSqlFragment whereFragment = new PagingSqlFragment(pagingMatcher.group(1),
                    pagingMatcher.group(2));
            parseContainerSection(whereFragment, lineIterator, line.indent());
            if (whereFragment.getFragments().size() == 0) {
                throw new IllegalArgumentException("@PAGING found with no subsequent indented lines: " + line);
            }
            container.addFragment(whereFragment);

        } else if (trimmed.startsWith("@WHERE")) {
            if (trimmed.equals("@WHERE") == false) {
                throw new IllegalArgumentException("@WHERE found with invalid format: " + line);
            }
            WhereSqlFragment whereFragment = new WhereSqlFragment();
            parseContainerSection(whereFragment, lineIterator, line.indent());
            if (whereFragment.getFragments().size() == 0) {
                throw new IllegalArgumentException("@WHERE found with no subsequent indented lines: " + line);
            }
            container.addFragment(whereFragment);

        } else if (trimmed.startsWith("@AND")) {
            Matcher andMatcher = AND_PATTERN.matcher(trimmed);
            if (andMatcher.matches() == false) {
                throw new IllegalArgumentException("@AND found with invalid format: " + line);
            }
            AndSqlFragment andFragment = new AndSqlFragment(andMatcher.group(1),
                    StringUtils.strip(andMatcher.group(2), " ="));
            parseContainerSection(andFragment, lineIterator, line.indent());
            if (andFragment.getFragments().size() == 0) {
                throw new IllegalArgumentException("@AND found with no subsequent indented lines: " + line);
            }
            container.addFragment(andFragment);

        } else if (trimmed.startsWith("@OR")) {
            Matcher orMatcher = OR_PATTERN.matcher(trimmed);
            if (orMatcher.matches() == false) {
                throw new IllegalArgumentException("@OR found with invalid format: " + line);
            }
            OrSqlFragment orFragment = new OrSqlFragment(orMatcher.group(1),
                    StringUtils.strip(orMatcher.group(2), " ="));
            parseContainerSection(orFragment, lineIterator, line.indent());
            if (orFragment.getFragments().size() == 0) {
                throw new IllegalArgumentException("@OR found with no subsequent indented lines: " + line);
            }
            container.addFragment(orFragment);

        } else if (trimmed.startsWith("@IF")) {
            Matcher ifMatcher = IF_PATTERN.matcher(trimmed);
            if (ifMatcher.matches() == false) {
                throw new IllegalArgumentException("@IF found with invalid format: " + line);
            }
            IfSqlFragment ifFragment = new IfSqlFragment(ifMatcher.group(1),
                    StringUtils.strip(ifMatcher.group(2), " ="));
            parseContainerSection(ifFragment, lineIterator, line.indent());
            if (ifFragment.getFragments().size() == 0) {
                throw new IllegalArgumentException("@IF found with no subsequent indented lines: " + line);
            }
            container.addFragment(ifFragment);

        } else {
            parseLine(container, line);
        }
    }
}

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

public ActivityChangeSet touch(Instant time, Node node, boolean allowLinkMismatch) {
    Validate.notNull(time);//www. j a v  a  2s.c o 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();
    boolean added = false;
    while (it.hasNext()) {
        Activity entry = it.next();

        if (entry.getTime().isAfter(time)) {
            it.previous(); // move back 1 space, we want to add to element just before 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.addLast(newEntry);
    }

    // Set has become too large, remove the item with the latest time
    Activity discardedEntry = null;
    if (entries.size() > maxSize) {
        // if the node removed with the latest time is the one we just added, then report that node couldn't be added
        discardedEntry = entries.removeLast();
        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.libreplan.business.planner.entities.TaskGroup.java

public void setTaskChildrenTo(List<TaskElement> children) {
    Validate.noNullElements(children);/*from   www.j a  v a2s  .  c o m*/
    int positionOnTaskElements = 0;

    for (int i = 0; i < children.size(); i++) {
        TaskElement element = children.get(i);
        element.setParent(this);

        if (positionOnTaskElements >= taskElements.size()) {
            taskElements.add(element);
        } else {
            while (positionOnTaskElements < taskElements.size()
                    && isMilestone(taskElements.get(positionOnTaskElements))) {

                positionOnTaskElements++;
            }

            if (positionOnTaskElements >= taskElements.size()) {
                taskElements.add(element);
            } else {
                taskElements.set(positionOnTaskElements, element);
            }
        }
        positionOnTaskElements++;
    }
    ListIterator<TaskElement> listIterator = taskElements.listIterator(positionOnTaskElements);
    while (listIterator.hasNext()) {
        TaskElement current = listIterator.next();
        if (!isMilestone(current)) {
            listIterator.remove();
        }
    }
}

From source file:com.quigley.filesystem.FilesystemPath.java

public FilesystemPath simplify() {
    List<String> elementsCopy = new ArrayList<String>(elements);
    ListIterator<String> i = elementsCopy.listIterator();
    boolean saw = false;
    while (i.hasNext()) {
        String value = i.next();/* ww w  .j a va  2s  .c o  m*/
        if (value.equals(".")) {
            i.remove();
        } else if (value.equals("..")) {
            if (saw) {
                if (i.hasPrevious()) {
                    i.remove();
                    i.previous();
                    i.remove();
                }
            }
        } else {
            saw = true;
        }
    }
    FilesystemPath pathCopy = new FilesystemPath(elementsCopy);
    pathCopy.isAbsolute = isAbsolute;

    return pathCopy;
}

From source file:org.openo.msb.ConsulClientApp.java

private void stopNodeListen(String serviceName) {
    try {//from www  . j  a v a2  s.c o m

        ListIterator<HealthCache> cacheListLit = cacheList.get().listIterator();
        while (cacheListLit.hasNext()) {
            HealthCache cache = (HealthCache) cacheListLit.next();
            if (cache.getServiceName().equals(serviceName)) {

                cache.stop();
                cacheListLit.remove();
                LOGGER.info(cache.getServiceName() + " NodeListen stoped");
                break;
            }
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        LOGGER.error("stop Node:[" + serviceName + "] Listen throw exception", e);
    }

}

From source file:org.openecomp.sdnc.sli.SliPluginUtils.SvcLogicContextList.java

public void remove(Map<String, String> primary_key) {
    ListIterator<HashMap<String, String>> itr = this.list.listIterator();
    while (itr.hasNext()) {
        boolean found = true;
        HashMap<String, String> list_element = itr.next();
        for (Map.Entry<String, String> key : primary_key.entrySet()) {
            if (!key.getValue().equals(list_element.get(key.getKey()))) {
                found = false;//from  ww  w .ja v a 2  s  . c  o m
                break;
            }
        }

        if (found) {
            itr.remove();
        }
    }
}