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:Main.java

public static long iterAdd(List list) {
    ListIterator iter = list.listIterator();
    long start, stop, result = 0;
    for (int i = 0; i < 100; i++) {
        start = System.nanoTime();
        iter.add(list.size() + 1 + i);
        stop = System.nanoTime();
        result += stop - start;//from   w  w w  .j a  v  a  2 s. com
    }
    return result / 100;
}

From source file:org.jcodec.movtool.Util.java

public static Pair<List<Edit>> split(List<Edit> edits, Rational trackByMv, long tvMv) {
    long total = 0;
    List<Edit> l = new ArrayList<Edit>();
    List<Edit> r = new ArrayList<Edit>();
    ListIterator<Edit> lit = edits.listIterator();
    while (lit.hasNext()) {
        Edit edit = lit.next();// w w w  .j  a  va 2s .c o  m
        if (total + edit.getDuration() > tvMv) {
            int leftDurMV = (int) (tvMv - total);
            int leftDurMedia = trackByMv.multiplyS(leftDurMV);

            Edit left = new Edit(leftDurMV, edit.getMediaTime(), 1.0f);
            Edit right = new Edit(edit.getDuration() - leftDurMV, leftDurMedia + edit.getMediaTime(), 1.0f);

            lit.remove();
            if (left.getDuration() > 0) {
                lit.add(left);
                l.add(left);
            }
            if (right.getDuration() > 0) {
                lit.add(right);
                r.add(right);
            }
            break;
        } else {
            l.add(edit);
        }
        total += edit.getDuration();
    }
    while (lit.hasNext()) {
        r.add(lit.next());
    }
    return new Pair<List<Edit>>(l, r);
}

From source file:exm.stc.ic.ICUtil.java

/**
 * Replace the current instruction with the provided sequence
 * After this is done, next() will return the instruction after
 * the inserted sequence/*from   ww  w  . j  a  va 2 s  .c o  m*/
 */
public static void replaceInsts(Block block, ListIterator<Statement> it,
        List<? extends Statement> replacements) {
    for (Statement stmt : replacements) {
        stmt.setParent(block);
    }
    if (replacements.size() == 1) {
        it.set(replacements.get(0));
    } else if (replacements.size() == 0) {
        it.remove();
    } else {
        it.set(replacements.get(0));
        List<? extends Statement> rest = replacements.subList(1, replacements.size());
        for (Statement newInst : rest) {
            it.add(newInst);
        }
    }
}

From source file:Main.java

/**
 * Returns a list iterator that swaps all previous/next calls.
 * <p><b>Important:</b> The returned iterator violates the {@link ListIterator#nextIndex()} and {@link ListIterator#previousIndex()} specifications.
 */// w  w  w .j  a v  a2  s .c o  m
public static <E> ListIterator<E> reverse(ListIterator<E> iterator) {
    return new ListIterator<E>() {
        @Override
        public boolean hasNext() {
            return iterator.hasPrevious();
        }

        @Override
        public E next() {
            return iterator.previous();
        }

        @Override
        public boolean hasPrevious() {
            return iterator.hasNext();
        }

        @Override
        public E previous() {
            return iterator.next();
        }

        @Override
        public int nextIndex() {
            return iterator.previousIndex();
        }

        @Override
        public int previousIndex() {
            return iterator.nextIndex();
        }

        @Override
        public void remove() {
            iterator.remove();
        }

        @Override
        public void set(E e) {
            iterator.set(e);
        }

        @Override
        public void add(E e) {
            iterator.add(e);
        }
    };
}

From source file:com.replaymod.replaystudio.studio.StudioReplayPart.java

@Override
public void add(long at, Iterable<Packet> packets) {
    ListIterator<PacketData> iter = this.packets.listIterator().skipTo(at);
    for (Packet packet : packets) {
        iter.add(new PacketData(at, packet));
    }/*from ww w . ja v  a  2 s.c om*/
}

From source file:therian.operator.add.AddToListIterator.java

@Override
@SuppressWarnings("unchecked")
public boolean perform(TherianContext context, Add<?, ? extends ListIterator<?>> add) {
    @SuppressWarnings("rawtypes")
    final ListIterator listIterator = add.getTargetPosition().getValue();

    int mark = 0;
    while (listIterator.hasNext()) {
        listIterator.next();/*  w w w . ja  va  2s  .c o m*/
        mark++;
    }
    try {
        listIterator.add(add.getSourcePosition().getValue());
        add.setResult(true);
        return true;
    } catch (UnsupportedOperationException e) {
        return false;
    } finally {
        for (; mark >= 0; mark--) {
            listIterator.previous();
        }
    }
}

From source file:pku.sei.checkedcoverage.tracer.instrumentation.PauseTracingInstrumenter.java

@SuppressWarnings("unchecked")
public void transformMethod(final MethodNode method, final ListIterator<MethodNode> methodIt,
        final String className) {
    if ((method.access & ACC_ABSTRACT) != 0 || (method.access & ACC_NATIVE) != 0)
        return;//www  . jav a2  s . com

    int tracerLocalVarIndex = (method.access & Opcodes.ACC_STATIC) == 0 ? 1 : 0;
    for (final Type t : Type.getArgumentTypes(method.desc))
        tracerLocalVarIndex += t.getSize();

    // increment number of local variables by one (for the threadtracer)
    ++method.maxLocals;

    // and increment all local variable indexes after the new one by one
    for (final Object o : method.localVariables) {
        final LocalVariableNode localVar = (LocalVariableNode) o;
        if (localVar.index >= tracerLocalVarIndex)
            ++localVar.index;
    }
    final LabelNode l0 = new LabelNode();
    final LabelNode l1 = new LabelNode();

    final ListIterator<AbstractInsnNode> insnIt = method.instructions.iterator();

    insnIt.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(Tracer.class), "getInstance",
            "()L" + Type.getInternalName(Tracer.class) + ";"));
    insnIt.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(Tracer.class), "getThreadTracer",
            "()L" + Type.getInternalName(ThreadTracer.class) + ";"));
    insnIt.add(new InsnNode(DUP));
    insnIt.add(new VarInsnNode(ASTORE, tracerLocalVarIndex));
    insnIt.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "pauseTracing",
            "()V"));
    insnIt.add(l0);

    while (insnIt.hasNext()) {
        final AbstractInsnNode insn = insnIt.next();
        switch (insn.getType()) {
        case AbstractInsnNode.INSN:
            switch (insn.getOpcode()) {
            case IRETURN:
            case LRETURN:
            case FRETURN:
            case DRETURN:
            case ARETURN:
            case RETURN:
                insnIt.previous();
                insnIt.add(new VarInsnNode(ALOAD, tracerLocalVarIndex));
                insnIt.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class),
                        "resumeTracing", "()V"));
                insnIt.next();
            }
            break;
        case AbstractInsnNode.IINC_INSN:
            if (((IincInsnNode) insn).var >= tracerLocalVarIndex)
                ++((IincInsnNode) insn).var;
            break;
        case AbstractInsnNode.VAR_INSN:
            if (((VarInsnNode) insn).var >= tracerLocalVarIndex)
                ++((VarInsnNode) insn).var;
            break;
        default:
            break;
        }
    }

    method.instructions.add(l1);

    method.instructions.add(new VarInsnNode(ALOAD, tracerLocalVarIndex));
    method.instructions.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class),
            "resumeTracing", "()V"));
    method.instructions.add(new InsnNode(ATHROW));

    method.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, null));

    // finally: create a copy of the method that gets the ThreadTracer as argument
    if (!"<clinit>".equals(method.name) && this.tracer.wasRedefined(className)) {
        final Type[] oldMethodArguments = Type.getArgumentTypes(method.desc);
        final Type[] newMethodArguments = Arrays.copyOf(oldMethodArguments, oldMethodArguments.length + 1);
        newMethodArguments[oldMethodArguments.length] = Type.getType(ThreadTracer.class);
        final String newMethodDesc = Type.getMethodDescriptor(Type.getReturnType(method.desc),
                newMethodArguments);
        final MethodNode newMethod = new MethodNode(method.access, method.name, newMethodDesc, method.signature,
                (String[]) method.exceptions.toArray(new String[method.exceptions.size()]));
        methodIt.add(newMethod);

        final Map<LabelNode, LabelNode> newMethodLabels = LazyMap.decorate(new HashMap<LabelNode, LabelNode>(),
                new Factory() {
                    public Object create() {
                        return new LabelNode();
                    }
                });

        // copy the local variables information to the new method
        for (final Object o : method.localVariables) {
            final LocalVariableNode lv = (LocalVariableNode) o;
            newMethod.localVariables.add(new LocalVariableNode(lv.name, lv.desc, lv.signature,
                    newMethodLabels.get(lv.start), newMethodLabels.get(lv.end), lv.index));
        }

        newMethod.maxLocals = method.maxLocals;
        newMethod.maxStack = method.maxStack;

        // copy the try-catch-blocks
        for (final Object o : method.tryCatchBlocks) {
            final TryCatchBlockNode tcb = (TryCatchBlockNode) o;
            newMethod.tryCatchBlocks.add(new TryCatchBlockNode(newMethodLabels.get(tcb.start),
                    newMethodLabels.get(tcb.end), newMethodLabels.get(tcb.handler), tcb.type));
        }

        // skip the first 4 instructions, replace them with this:
        newMethod.instructions.add(new VarInsnNode(ALOAD, tracerLocalVarIndex));
        final Iterator<AbstractInsnNode> oldInsnIt = method.instructions.iterator(4);
        // and add all the other instructions
        while (oldInsnIt.hasNext()) {
            final AbstractInsnNode insn = oldInsnIt.next();
            newMethod.instructions.add(insn.clone(newMethodLabels));
        }
    }
}

From source file:de.unisb.cs.st.javaslicer.tracer.instrumentation.PauseTracingInstrumenter.java

@SuppressWarnings("unchecked")
public void transformMethod(final MethodNode method, final ListIterator<MethodNode> methodIt,
        final String className) {
    if ((method.access & ACC_ABSTRACT) != 0 || (method.access & ACC_NATIVE) != 0)
        return;//from   w w w .  j av a2s . c o m

    int tracerLocalVarIndex = (method.access & Opcodes.ACC_STATIC) == 0 ? 1 : 0;
    for (final Type t : Type.getArgumentTypes(method.desc))
        tracerLocalVarIndex += t.getSize();

    // increment number of local variables by one (for the threadtracer)
    ++method.maxLocals;

    // and increment all local variable indexes after the new one by one
    for (final Object o : method.localVariables) {
        final LocalVariableNode localVar = (LocalVariableNode) o;
        if (localVar.index >= tracerLocalVarIndex)
            ++localVar.index;
    }
    final LabelNode l0 = new LabelNode();
    final LabelNode l1 = new LabelNode();

    final ListIterator<AbstractInsnNode> insnIt = method.instructions.iterator();

    insnIt.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(Tracer.class), "getInstance",
            "()L" + Type.getInternalName(Tracer.class) + ";"));
    insnIt.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(Tracer.class), "getThreadTracer",
            "()L" + Type.getInternalName(ThreadTracer.class) + ";"));
    insnIt.add(new InsnNode(DUP));
    insnIt.add(new VarInsnNode(ASTORE, tracerLocalVarIndex));
    insnIt.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "pauseTracing",
            "()V"));
    insnIt.add(l0);

    while (insnIt.hasNext()) {
        final AbstractInsnNode insn = insnIt.next();
        switch (insn.getType()) {
        case AbstractInsnNode.INSN:
            switch (insn.getOpcode()) {
            case IRETURN:
            case LRETURN:
            case FRETURN:
            case DRETURN:
            case ARETURN:
            case RETURN:
                insnIt.previous();
                insnIt.add(new VarInsnNode(ALOAD, tracerLocalVarIndex));
                insnIt.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class),
                        "resumeTracing", "()V"));
                insnIt.next();
            }
            break;
        case AbstractInsnNode.IINC_INSN:
            if (((IincInsnNode) insn).var >= tracerLocalVarIndex)
                ++((IincInsnNode) insn).var;
            break;
        case AbstractInsnNode.VAR_INSN:
            if (((VarInsnNode) insn).var >= tracerLocalVarIndex)
                ++((VarInsnNode) insn).var;
            break;
        default:
            break;
        }
    }

    method.instructions.add(l1);

    method.instructions.add(new VarInsnNode(ALOAD, tracerLocalVarIndex));
    method.instructions.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class),
            "resumeTracing", "()V"));
    method.instructions.add(new InsnNode(ATHROW));

    method.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, null));

    // finally: create a copy of the method that gets the ThreadTracer as argument
    if (!"<clinit>".equals(method.name) && this.tracer.wasRedefined(className)) {
        final Type[] oldMethodArguments = Type.getArgumentTypes(method.desc);
        final Type[] newMethodArguments = Arrays.copyOf(oldMethodArguments, oldMethodArguments.length + 1);
        newMethodArguments[oldMethodArguments.length] = Type.getType(ThreadTracer.class);
        final String newMethodDesc = Type.getMethodDescriptor(Type.getReturnType(method.desc),
                newMethodArguments);
        final MethodNode newMethod = new MethodNode(method.access, method.name, newMethodDesc, method.signature,
                (String[]) method.exceptions.toArray(new String[method.exceptions.size()]));
        methodIt.add(newMethod);

        final Map<LabelNode, LabelNode> newMethodLabels = LazyMap.decorate(new HashMap<LabelNode, LabelNode>(),
                new Factory() {
                    @Override
                    public Object create() {
                        return new LabelNode();
                    }
                });

        // copy the local variables information to the new method
        for (final Object o : method.localVariables) {
            final LocalVariableNode lv = (LocalVariableNode) o;
            newMethod.localVariables.add(new LocalVariableNode(lv.name, lv.desc, lv.signature,
                    newMethodLabels.get(lv.start), newMethodLabels.get(lv.end), lv.index));
        }

        newMethod.maxLocals = method.maxLocals;
        newMethod.maxStack = method.maxStack;

        // copy the try-catch-blocks
        for (final Object o : method.tryCatchBlocks) {
            final TryCatchBlockNode tcb = (TryCatchBlockNode) o;
            newMethod.tryCatchBlocks.add(new TryCatchBlockNode(newMethodLabels.get(tcb.start),
                    newMethodLabels.get(tcb.end), newMethodLabels.get(tcb.handler), tcb.type));
        }

        // skip the first 4 instructions, replace them with this:
        newMethod.instructions.add(new VarInsnNode(ALOAD, tracerLocalVarIndex));
        final Iterator<AbstractInsnNode> oldInsnIt = method.instructions.iterator(4);
        // and add all the other instructions
        while (oldInsnIt.hasNext()) {
            final AbstractInsnNode insn = oldInsnIt.next();
            newMethod.instructions.add(insn.clone(newMethodLabels));
        }
    }
}

From source file:org.openehealth.ipf.commons.ihe.xds.core.transform.hl7.PatientInfoTransformer.java

/**
 * Creates a {@link PatientInfo} instance via an HL7 XCN string.
 * @param hl7PID//from w  ww  .  j ava2  s.  c  o m
 *          the HL7 PID strings. Can be <code>null</code>.
 * @return the created {@link PatientInfo} instance. <code>null</code> if no relevant 
 *          data was found in the HL7 string or the input was <code>null</code>.
 */
public PatientInfo fromHL7(List<String> hl7PID) {
    if (hl7PID == null || hl7PID.isEmpty()) {
        return null;
    }

    PatientInfo patientInfo = new PatientInfo();

    for (String hl7PIDLine : hl7PID) {
        String[] fields = PipeParser.split(hl7PIDLine.trim(), "|");
        if (fields.length == 2) {
            ListIterator<String> iterator = patientInfo.getHl7FieldIterator(fields[0]);
            while (iterator.hasNext()) {
                iterator.next();
            }
            String[] strings = PipeParser.split(fields[1], "~");
            for (String string : strings) {
                iterator.add(string);
            }
        }
    }

    return patientInfo;
}

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

/**
 * Add an image handler. The handler itself is inspected to find out what it supports.
 * @param handler the ImageHandler instance
 *///from   w ww  .jav a  2  s  .  c  om
public synchronized void addHandler(ImageHandler handler) {
    Class<? extends Image> imageClass = handler.getSupportedImageClass();
    //List
    this.handlers.put(imageClass, handler);

    //Sorted insert (sort by priority)
    ListIterator<ImageHandler> iter = this.handlerList.listIterator();
    while (iter.hasNext()) {
        ImageHandler h = iter.next();
        if (HANDLER_COMPARATOR.compare(handler, h) < 0) {
            iter.previous();
            break;
        }
    }
    iter.add(handler);
    this.handlerRegistrations++;
}