Example usage for java.util ListIterator add

List of usage examples for java.util ListIterator add

Introduction

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

Prototype

void add(E e);

Source Link

Document

Inserts the specified element into the list (optional operation).

Usage

From source file:org.apache.fop.render.AbstractImageHandlerRegistry.java

/**
 * Add an image handler. The handler itself is inspected to find out what it supports.
 * @param handler the ImageHandler instance
 *///w w w  . j  a  v a 2  s.c  o m
public synchronized void addHandler(ImageHandlerBase handler) {
    this.handlers.put(handler.getSupportedImageClass(), handler);

    //Sorted insert
    ListIterator iter = this.handlerList.listIterator();
    while (iter.hasNext()) {
        ImageHandlerBase h = (ImageHandlerBase) iter.next();
        if (getHandlerComparator().compare(handler, h) < 0) {
            iter.previous();
            break;
        }
    }
    iter.add(handler);
    this.handlerRegistrations++;
}

From source file:com.google.gwt.emultest.java.util.ListTestBase.java

public void testListIteratorHasNextHasPreviousAndIndexes() {
    List l = makeEmptyList();/*from   ww w  .j a  va 2  s. c om*/
    ListIterator i = l.listIterator();
    assertFalse(i.hasNext());
    assertFalse(i.hasPrevious());
    i.add(new Integer(1));
    assertEquals(1, i.nextIndex());
    assertEquals(0, i.previousIndex());
    i = l.listIterator();
    assertEquals(0, i.nextIndex());
    assertEquals(-1, i.previousIndex());
    assertTrue(i.hasNext());
    assertFalse(i.hasPrevious());
    i.next();
    assertEquals(1, i.nextIndex());
    assertEquals(0, i.previousIndex());
    assertFalse(i.hasNext());
    assertTrue(i.hasPrevious());
}

From source file:com.edgenius.wiki.render.macro.BaseMacro.java

/**
 * This is facility method to help you handle reset HTMLNode to markup text with given  tidy style. 
 * //  ww  w.j av  a  2s. co m
 * !! You must ensure both endMarkup and node.getPair() is not null if you want to put endMarkup into node.getPair() node!
 * 
 * !!!!
 * If tidyStyle is TIDY_STYLE_BLOCK, this node will be insert some LINE_START or LINE_END tags.  
 * Iter.next() is unaffected. However, Iter.previous() will return new inserted LINK_END tag. 
 * 
 */
protected void resetMacroMarkup(int tidyStyle, HTMLNode node, ListIterator<HTMLNode> iter, String startMarkup,
        String endMarkup) {
    if (tidyStyle == Macro.TIDY_STYLE_BLOCK) {
        //add HTMLNode.LINE_START and END surrounding markup
        node.reset(HTMLNode.LINE_START_TAG, false);
        iter.add(new HTMLNode(startMarkup, true));
        HTMLNode lastNode = new HTMLNode(HTMLNode.LINE_END_TAG, false);
        iter.add(lastNode);

        if (node.getPair() != null) {
            if (!StringUtils.isBlank(endMarkup)) {
                while (iter.hasNext()) {
                    HTMLNode cursor = iter.next();
                    if (node.getPair() == cursor) {
                        node.getPair().reset(HTMLNode.LINE_START_TAG, false);
                        iter.add(new HTMLNode(endMarkup, true));
                        iter.add(new HTMLNode(HTMLNode.LINE_END_TAG, false));

                        //!!! reset ListIterator back to last node - to ensure iter.next() is unaffected, use lastNode
                        moveIteratorCursorTo(lastNode, iter, false);
                        break;
                    }
                }
            } else {
                node.getPair().reset("", true);
            }
        }
    } else {
        node.reset(startMarkup, true);
        if (node.getPair() != null) {
            if (!StringUtils.isBlank(endMarkup)) {
                node.getPair().reset(endMarkup, true);
            } else {
                node.getPair().reset("", true);
            }

        }
    }
}

From source file:com.google.gwt.emultest.java.util.ListTestBase.java

public void testListIteratorAddInSeveralPositions() {
    List l = makeEmptyList();/*w ww  .j av a  2 s  . c  om*/
    ListIterator i = l.listIterator();
    l.add(new Integer(0));
    for (int n = 2; n < 5; n += 2) {
        l.add(new Integer(n));
    }
    i = l.listIterator();
    i.next();
    i.add(new Integer(1));
    i.next();
    i.next();
    i.previous();
    i.add(new Integer(3));
    i.next();
    i.add(new Integer(5));
    i.add(new Integer(6));
    for (int n = 0; n < 6; n++) {
        assertEquals(new Integer(n), l.get(n));
    }
}

From source file:edu.umd.cfar.lamp.viper.geometry.BoundingBox.java

/**
 * Gets a set of boxes which covers all and only the pixels covered by
 * <code>A</code> and <code>B</code>.
 * //from w ww . j  ava2s.  co m
 * @param A
 *            a set of boxes to union with
 * @param B
 *            a set of boxes to union with
 * @return a set of boxes corresponding to the region shared by A and B
 */
public static BoundingBox union(BoundingBox A, BoundingBox B) {
    BoundingBox temp = new BoundingBox();
    LinkedList aList;
    temp.composed = true;
    int x = Math.min(A.rect.x, B.rect.x);
    int y = Math.min(A.rect.y, B.rect.y);
    int x2 = Math.max(A.rect.x + A.rect.width, B.rect.x + B.rect.width);
    int y2 = Math.max(A.rect.y + A.rect.height, B.rect.y + B.rect.height);

    temp.rect = new Rectangle(x, y, x2, y2);

    if (A.composed)
        aList = (LinkedList) A.pieces.clone();
    else {
        aList = new LinkedList();
        aList.add(A);
    }

    if (B.composed)
        temp = B.copy();
    else {
        temp.pieces = new LinkedList();
        temp.pieces.add(B.copy());
        temp.composed = true;
    }

    ListIterator iter = aList.listIterator(0);
    while (iter.hasNext()) {
        BoundingBox child = (BoundingBox) iter.next();
        Iterator ti = temp.pieces.iterator();
        LinkedList childRemains = null;

        /* remove an offending piece of the child */
        while (ti.hasNext() && (null == (childRemains = ((BoundingBox) ti.next()).subtractFrom(child))))
            ;

        /*
         * Add the broken pieces into the list and break back to top loop
         * remove the offending rectangle and replace it with its shards,
         * then clean up those.
         */
        if (childRemains != null) {
            ti = childRemains.iterator();
            iter.remove();
            while (ti.hasNext()) {
                iter.add(ti.next());
                iter.previous();
            }
        }
    }
    temp.pieces.addAll(aList);
    temp.simplify();
    return (temp);
}

From source file:org.archive.crawler.restlet.EnhDirectoryResource.java

/** 
 * Add EditRepresentation as a variant when appropriate. 
 * /*from ww w  .  j  a va 2 s .  c  o  m*/
 * @see com.noelios.restlet.local.DirectoryResource#getVariants()
 */
@Override
public List<Variant> getVariants() {
    List<Variant> variants = super.getVariants();
    Form f = getRequest().getResourceRef().getQueryAsForm();
    String format = f.getFirstValue("format");
    if ("textedit".equals(format)) {
        if (variants.isEmpty()) {
            // create empty placeholder file if appropriate
            try {
                File file = new File(new URI(getTargetUri()));
                if (getEnhDirectory().allowsEdit(file)) {
                    file.createNewFile();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            variants = super.getVariants();
        }
        // wrap FileRepresentations in EditRepresentations
        ListIterator<Variant> iter = variants.listIterator();
        while (iter.hasNext()) {
            Variant v = iter.next();
            if (v instanceof FileRepresentation) {
                File file = ((FileRepresentation) v).getFile();
                if (getEnhDirectory().allowsEdit(file)) {
                    iter.remove();
                    // any editable file for our purposes should 
                    // be XML/UTF-8
                    v.setCharacterSet(CharacterSet.UTF_8);
                    iter.add(new EditRepresentation((FileRepresentation) v, this));
                }
                ;
            }
        }
    } else if ("paged".equals(format)) {
        ListIterator<Variant> iter = variants.listIterator();
        while (iter.hasNext()) {
            Variant v = iter.next();
            if (v instanceof FileRepresentation) {
                File file = ((FileRepresentation) v).getFile();
                if (getEnhDirectory().allowsPaging(file)) {
                    iter.remove();
                    iter.add(new PagedRepresentation((FileRepresentation) v, this, f.getFirstValue("pos"),
                            f.getFirstValue("lines"), f.getFirstValue("reverse")));
                }
                ;
            }
        }
    } else {
        ListIterator<Variant> iter = variants.listIterator();
        while (iter.hasNext()) {
            Variant v = iter.next();
            v.setCharacterSet(CharacterSet.UTF_8);
        }
    }

    return variants;
}

From source file:storm.mesos.resources.RangeResource.java

private List<ResourceEntry> removeAndGet(Collection<ReservationType> reservationTypes,
        RangeResourceEntry desiredRange) {
    // Because of the way Storm does assignments, we will only ever need a single port at a time, bail out if this is not the case
    assert desiredRange.getBegin() == desiredRange.getEnd();

    List<ResourceEntry> removedResources = new ArrayList<>();
    Long desiredPort = desiredRange.getBegin();

    // Iterate over all of the available resources
    for (ReservationType reservationType : reservationTypes) {
        List<RangeResourceEntry> availableRanges = availableResourcesByReservationType.get(reservationType);
        ListIterator<RangeResourceEntry> iterator = availableRanges.listIterator();
        while (iterator.hasNext()) {
            RangeResourceEntry availableRange = iterator.next();
            if (desiredPort >= availableRange.getBegin() && desiredPort <= availableRange.getEnd()) {
                iterator.remove();/*from w  w w.ja  va2 s. com*/
                // Salvage resources before the beginning of the requested port
                if (availableRange.getBegin() < desiredPort) {
                    iterator.add(new RangeResourceEntry(reservationType, availableRange.getBegin(),
                            desiredPort - 1));
                }
                // Salvage resources after the end of the requested port
                if (availableRange.getEnd() > desiredPort) {
                    iterator.add(
                            new RangeResourceEntry(reservationType, desiredPort + 1, availableRange.getEnd()));
                }
                // Now that we've salvaged all available resources, add the resources for the specifically requested range
                removedResources.add(new RangeResourceEntry(reservationType, desiredPort, desiredPort));
                break;
            }
        }
    }
    return removedResources;
}

From source file:chat.viska.commons.pipelines.Pipeline.java

public void addTowardsOutboundEnd(final Pipe next, final String name, final Pipe pipe) {
    Completable.fromAction(() -> {// w  w  w  . j  a  v a 2 s  . c o m
        pipeLock.writeLock().lockInterruptibly();
        try {
            if (getIteratorOf(name) != null) {
                throw new IllegalArgumentException("Name collision: " + name);
            }
            ListIterator<Map.Entry<String, Pipe>> iterator = getIteratorOf(next);
            if (iterator == null) {
                throw new NoSuchElementException();
            }
            iterator.add(new AbstractMap.SimpleImmutableEntry<>(name, pipe));
            pipe.onAddedToPipeline(this);
        } finally {
            pipeLock.writeLock().unlock();
        }
    }).onErrorComplete().subscribeOn(Schedulers.io()).subscribe();
}

From source file:chat.viska.commons.pipelines.Pipeline.java

public void addTowardsOutboundEnd(final String next, final String name, final Pipe pipe) {
    Validate.notBlank(next);//from  ww w . ja  v a 2s . c  o m
    Completable.fromAction(() -> {
        pipeLock.writeLock().lockInterruptibly();
        try {
            if (getIteratorOf(name) != null) {
                throw new IllegalArgumentException("Name collision: " + name);
            }
            ListIterator<Map.Entry<String, Pipe>> iterator = getIteratorOf(next);
            if (iterator == null) {
                throw new NoSuchElementException(next);
            }
            iterator.add(new AbstractMap.SimpleImmutableEntry<>(name, pipe));
            pipe.onAddedToPipeline(this);
        } finally {
            pipeLock.writeLock().unlock();
        }
    }).onErrorComplete().subscribeOn(Schedulers.io()).subscribe();
}

From source file:chat.viska.commons.pipelines.Pipeline.java

public void addTowardsInboundEnd(final Pipe previous, final String name, final Pipe pipe) {
    Completable.fromAction(() -> {//from ww  w.  ja v a 2s  . c  om
        pipeLock.writeLock().lockInterruptibly();
        try {
            if (getIteratorOf(name) != null) {
                throw new IllegalArgumentException("Name collision: " + name);
            }
            ListIterator<Map.Entry<String, Pipe>> iterator = getIteratorOf(previous);
            if (iterator == null) {
                throw new NoSuchElementException();
            }
            iterator.next();
            iterator.add(new AbstractMap.SimpleImmutableEntry<>(name, pipe));
            pipe.onAddedToPipeline(this);
        } finally {
            pipeLock.writeLock().unlock();
        }
    }).onErrorComplete().subscribeOn(Schedulers.io()).subscribe();
}