Example usage for java.util LinkedList addFirst

List of usage examples for java.util LinkedList addFirst

Introduction

In this page you can find the example usage for java.util LinkedList addFirst.

Prototype

public void addFirst(E e) 

Source Link

Document

Inserts the specified element at the beginning of this list.

Usage

From source file:automenta.knowtention.channel.LineFileChannel.java

@Override
public void run() {

    FileInputStream fileInputStream = null;
    FileChannel channel = null;/*from ww w.  j a  va  2  s .c  o  m*/
    ByteBuffer buffer = null;
    LinkedList<String> lines = new LinkedList();
    StringBuilder builder = new StringBuilder();
    long lastSize = -1, lastLastModified = -1;

    while (running) {
        try {
            Thread.sleep(delayPeriodMS);
        } catch (InterruptedException ex) {
        }

        lines.clear();
        try {
            fileInputStream = new FileInputStream(file);

            channel = fileInputStream.getChannel();

            long lastModified = file.lastModified();
            long csize = channel.size();
            if ((lastModified == lastLastModified) && (csize == lastSize)) { //also check file update time?
                fileInputStream.close();
                continue;
            }

            int currentPos = (int) csize;

            buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, csize);
            buffer.position(currentPos);
            lastSize = csize;
            lastLastModified = lastModified;

            int count = 0;

            for (long i = csize - 1; i >= 0; i--) {

                char c = (char) buffer.get((int) i);

                if (c == '\n') {
                    count++;
                    builder.reverse();
                    lines.addFirst(builder.toString());
                    if (count == numLines) {
                        break;
                    }
                    builder.setLength(0);
                } else
                    builder.append(c);
            }

            update(lines);

            lines.clear();
            buffer.clear();
            channel.close();
            fileInputStream.close();
            fileInputStream = null;

        } catch (Exception ex) {
            Logger.getLogger(LineFileChannel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    try {
        channel.close();
    } catch (IOException ex) {
        Logger.getLogger(LineFileChannel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:appeng.items.tools.powered.ToolColorApplicator.java

private ItemStack findNextColor(final ItemStack is, final ItemStack anchor, final int scrollOffset) {
    ItemStack newColor = null;/*from  ww  w.  j ava2  s .com*/

    final IMEInventory<IAEItemStack> inv = AEApi.instance().registries().cell().getCellInventory(is, null,
            StorageChannel.ITEMS);
    if (inv != null) {
        final IItemList<IAEItemStack> itemList = inv
                .getAvailableItems(AEApi.instance().storage().createItemList());
        if (anchor == null) {
            final IAEItemStack firstItem = itemList.getFirstItem();
            if (firstItem != null) {
                newColor = firstItem.getItemStack();
            }
        } else {
            final LinkedList<IAEItemStack> list = new LinkedList<IAEItemStack>();

            for (final IAEItemStack i : itemList) {
                list.add(i);
            }

            Collections.sort(list, new Comparator<IAEItemStack>() {

                @Override
                public int compare(final IAEItemStack a, final IAEItemStack b) {
                    return ItemSorters.compareInt(a.getItemDamage(), b.getItemDamage());
                }
            });

            if (list.size() <= 0) {
                return null;
            }

            IAEItemStack where = list.getFirst();
            int cycles = 1 + list.size();

            while (cycles > 0 && !where.equals(anchor)) {
                list.addLast(list.removeFirst());
                cycles--;
                where = list.getFirst();
            }

            if (scrollOffset > 0) {
                list.addLast(list.removeFirst());
            }

            if (scrollOffset < 0) {
                list.addFirst(list.removeLast());
            }

            return list.get(0).getItemStack();
        }
    }

    if (newColor != null) {
        this.setColor(is, newColor);
    }

    return newColor;
}

From source file:srebrinb.compress.sevenzip.SevenZFile.java

private InputStream buildDecoderStack(final Folder folder, final long folderOffset,
        final int firstPackStreamIndex, final SevenZArchiveEntry entry) throws IOException {
    channel.position(folderOffset);//  w w  w.  j  a  v  a  2s. co m
    InputStream inputStreamStack = new BufferedInputStream(
            new BoundedSeekableByteChannelInputStream(channel, archive.packSizes[firstPackStreamIndex]));
    final LinkedList<SevenZMethodConfiguration> methods = new LinkedList<>();
    for (final Coder coder : folder.getOrderedCoders()) {
        if (coder.numInStreams != 1 || coder.numOutStreams != 1) {
            throw new IOException("Multi input/output stream coders are not yet supported");
        }
        final SevenZMethod method = SevenZMethod.byId(coder.decompressionMethodId);
        inputStreamStack = Coders.addDecoder(fileName, inputStreamStack, folder.getUnpackSizeForCoder(coder),
                coder, password);
        methods.addFirst(new SevenZMethodConfiguration(method,
                Coders.findByMethod(method).getOptionsFromCoder(coder, inputStreamStack)));
    }
    entry.setContentMethods(methods);
    if (folder.hasCrc) {
        return new CRC32VerifyingInputStream(inputStreamStack, folder.getUnpackSize(), folder.crc);
    }
    return inputStreamStack;
}

From source file:gate.creole.tokeniser.SimpleTokeniser.java

/**
 * Converts the finite state machine to a deterministic one.
 *
 * @param s//from  w ww .j a  va  2 s.c  o  m
 */
private AbstractSet<FSMState> lambdaClosure(Set<FSMState> s) {

    //the stack/queue used by the algorithm
    LinkedList<FSMState> list = new LinkedList<FSMState>(s);

    //the set to be returned
    AbstractSet<FSMState> lambdaClosure = new HashSet<FSMState>(s);

    FSMState top;
    FSMState currentState;
    Set<FSMState> nextStates;
    Iterator<FSMState> statesIter;

    while (!list.isEmpty()) {
        top = list.removeFirst();
        nextStates = top.nextSet(null);

        if (null != nextStates) {
            statesIter = nextStates.iterator();

            while (statesIter.hasNext()) {
                currentState = statesIter.next();
                if (!lambdaClosure.contains(currentState)) {
                    lambdaClosure.add(currentState);
                    list.addFirst(currentState);
                } //if(!lambdaClosure.contains(currentState))
            } //while(statesIter.hasNext())

        } //if(null != nextStates)
    }
    return lambdaClosure;
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private InputStream buildDecoderStack(final Folder folder, final long folderOffset,
        final int firstPackStreamIndex, final SevenZArchiveEntry entry) throws IOException {
    file.seek(folderOffset);/*from   w ww.java 2 s  . com*/
    InputStream inputStreamStack = new BufferedInputStream(
            new BoundedRandomAccessFileInputStream(file, getArchive().packSizes[firstPackStreamIndex]));
    final LinkedList<SevenZMethodConfiguration> methods = new LinkedList<SevenZMethodConfiguration>();
    for (final Coder coder : folder.getOrderedCoders()) {
        if (coder.numInStreams != 1 || coder.numOutStreams != 1) {
            throw new IOException("Multi input/output stream coders are not yet supported");
        }
        final SevenZMethod method = SevenZMethod.byId(coder.decompressionMethodId);
        inputStreamStack = Coders.addDecoder(fileName, inputStreamStack, folder.getUnpackSizeForCoder(coder),
                coder, password);
        methods.addFirst(new SevenZMethodConfiguration(method,
                Coders.findByMethod(method).getOptionsFromCoder(coder, inputStreamStack)));
    }
    entry.setContentMethods(methods);
    if (folder.hasCrc) {
        return new CRC32VerifyingInputStream(inputStreamStack, folder.getUnpackSize(), folder.crc);
    }
    return inputStreamStack;
}

From source file:ru.codeinside.gses.activiti.forms.definitions.FormParser.java

void processBlocks(Map<String, PropertyParser> nodes, List<PropertyParser> rootList) throws BuildException {
    final ArrayList<PropertyParser> allPropertyParsers = new ArrayList<PropertyParser>(nodes.values());
    final LinkedList<BlockStartParser> stack = new LinkedList<BlockStartParser>();
    for (final PropertyParser propertyParser : allPropertyParsers) {
        final BlockStartParser block = stack.peekFirst();
        propertyParser.block = block;//from   ww w .j av  a  2s. c  o  m
        final boolean end = (propertyParser instanceof EndBlockParser);
        if (!end) {
            if (block == null) {
                if (!(propertyParser instanceof SignatureParser)) {
                    rootList.add(propertyParser);
                }
            } else {
                block.items.add(propertyParser);
            }
            if (propertyParser instanceof BlockStartParser) {
                final BlockStartParser start = (BlockStartParser) propertyParser;
                start.items = new ArrayList<PropertyParser>();
                stack.addFirst(start);
            }
        } else {
            if (block == null
                    || !block.property.id.substring(1).equals(propertyParser.property.id.substring(1))) {
                throw new BuildException("   ",
                        propertyParser);
            }
            stack.removeFirst();
        }
    }

    PropertyParser badStart = stack.peekFirst();
    if (badStart != null) {
        throw new BuildException("?   ?", badStart);
    }

    for (PropertyParser propertyParser : allPropertyParsers) {
        if (propertyParser instanceof BlockStartParser) {
            final BlockStartParser start = (BlockStartParser) propertyParser;
            if (start.items.isEmpty()) {
                throw new BuildException("? ", propertyParser);
            }
        }
    }
}

From source file:org.matonto.catalog.impl.SimpleCatalogManager.java

/**
 * Gets an iterator which contains all of the Resources (commits) leading up to the provided Resource identifying a
 * commit./*from www .ja  v a2  s . c o m*/
 *
 * @param commitId The Resource identifying the commit that you want to get the chain for.
 * @param conn The RepositoryConnection which will be queried for the Commits.
 * @return Iterator of Values containing the requested commits.
 */
private Iterator<Value> getCommitChainIterator(Resource commitId, RepositoryConnection conn) {
    TupleQuery query = conn.prepareTupleQuery(GET_COMMIT_CHAIN);
    query.setBinding(COMMIT_BINDING, commitId);
    TupleQueryResult result = query.evaluate();
    LinkedList<Value> commits = new LinkedList<>();
    result.forEach(bindingSet -> bindingSet.getBinding(PARENT_BINDING)
            .ifPresent(binding -> commits.add(binding.getValue())));
    commits.addFirst(commitId);
    return commits.descendingIterator();
}

From source file:com.redhat.persistence.oql.QFrame.java

private void render(LinkedList joins, List where, QFrame oroot, QFrame root, Set emitted) {
    // If the first non empty frame is outer we treat it as inner.
    if (m_outer && !joins.isEmpty()) {
        oroot = this;
    }/*from  ww w .j a  v  a  2 s .  c  om*/

    Code table = null;
    if (m_table != null && m_duplicate == null) {
        table = new Code(m_table).add(" ").add(alias());
    } else if (m_tableExpr != null && m_duplicate == null) {
        table = m_tableExpr.emit(m_generator).add(" ").add(alias());
    }

    if (table != null) {
        joins.addFirst(JFrame.leaf(table, this, oroot));
    }

    List children = getChildren();
    for (int i = 0; i < children.size(); i++) {
        QFrame child = (QFrame) children.get(i);
        child.render(joins, where, oroot, root, emitted);
    }

    if (m_condition != null) {
        Code c = m_condition.emit(m_generator);
        if (!c.isTrue() && !emitted.contains(c)) {
            m_used.clear();
            frames(m_condition, m_used);
            boolean join = false;
            for (Iterator it = joins.iterator(); it.hasNext();) {
                JFrame frame = (JFrame) it.next();
                boolean modified = m_used.removeAll(frame.defined);
                if (m_used.isEmpty()) {
                    // We default to putting things in the where
                    // clause here because oracle won't resolve
                    // external variable references correctly when
                    // they appear in join conditions.
                    if (oroot.equals(root)) {
                        where.add(c);
                    } else if (frame.froot != null && oroot.equals(frame.froot)) {
                        frame.join = frame.join.add(" and ").add(c);
                    } else {
                        throw new IllegalStateException(
                                "unable to place condition: " + m_condition + " " + c + trace(joins));
                    }
                } else if (modified) {
                    join = true;
                    break;
                }
            }
            if (join) {
                JFrame right = (JFrame) joins.removeFirst();

                if (joins.isEmpty()) {
                    throw new IllegalStateException(
                            "unresolved variable in condition: " + m_condition + " " + c + trace(joins));
                }

                LinkedList skipped = null;
                JFrame left = (JFrame) joins.removeFirst();
                while (true) {
                    m_used.clear();
                    frames(m_condition, m_used);
                    m_used.removeAll(right.defined);
                    boolean cross = m_used.removeAll(left.defined);
                    if (m_used.isEmpty()) {
                        joins.addFirst(JFrame.join(left, right, c));
                        break;
                    } else if (joins.isEmpty()) {
                        throw new IllegalStateException(
                                "unresolved variable in condition: " + m_condition + " " + c + trace(joins));
                    } else if (cross) {
                        JFrame lefter = (JFrame) joins.removeFirst();
                        left = JFrame.cross(lefter, left);
                    } else {
                        if (skipped == null) {
                            skipped = new LinkedList();
                        }
                        skipped.addLast(left);
                        left = (JFrame) joins.removeFirst();
                    }
                }

                if (skipped != null) {
                    while (!skipped.isEmpty()) {
                        joins.addFirst(skipped.removeLast());
                    }
                }
            }

            emitted.add(c);
        }
    }
}

From source file:org.myrian.persistence.oql.QFrame.java

private void render(LinkedList joins, List where, QFrame oroot, QFrame root, Set emitted) {
    // If the first non empty frame is outer we treat it as inner.
    if (m_outer && !joins.isEmpty()) {
        oroot = this;
    }/*ww w  .  j  a v a 2  s  .c  om*/

    Code table = null;
    if (m_table != null && m_duplicate == null) {
        table = new Code(m_table).add(" ").add(alias());
    } else if (m_tableExpr != null && m_duplicate == null) {
        table = m_tableExpr.emit(m_generator).add(" ").add(alias());
    }

    if (table != null) {
        joins.addFirst(JFrame.leaf(table, this, oroot));
    }

    List children = getChildren();
    for (int i = 0; i < children.size(); i++) {
        QFrame child = (QFrame) children.get(i);
        child.render(joins, where, oroot, root, emitted);
    }

    if (m_condition != null) {
        Code c = m_condition.emit(m_generator);
        if (!c.isTrue() && !emitted.contains(c)) {
            m_used.clear();
            frames(m_condition, m_used);
            boolean join = false;
            for (Iterator it = joins.iterator(); it.hasNext();) {
                JFrame frame = (JFrame) it.next();
                boolean modified = m_used.removeAll(frame.defined);
                if (m_used.isEmpty()) {
                    // We default to putting things in the where
                    // clause here because oracle won't resolve
                    // external variable references correctly when
                    // they appear in join conditions.
                    if (oroot.equals(root)) {
                        where.add(c);
                    } else if (frame.froot != null && oroot.equals(frame.froot)) {
                        frame.join = frame.join.add(" and ").add(c);
                    } else {
                        /*
                         * XXX rhs needs to make sure this is
                         * the right thing to do
                         */
                        where.add(c);
                    }
                } else if (modified) {
                    join = true;
                    break;
                }
            }
            if (join) {
                JFrame right = (JFrame) joins.removeFirst();

                if (joins.isEmpty()) {
                    throw new IllegalStateException(
                            "unresolved variable in condition: " + m_condition + " " + c + trace(joins));
                }

                LinkedList skipped = null;
                JFrame left = (JFrame) joins.removeFirst();
                while (true) {
                    m_used.clear();
                    frames(m_condition, m_used);
                    m_used.removeAll(right.defined);
                    boolean cross = m_used.removeAll(left.defined);
                    if (m_used.isEmpty()) {
                        joins.addFirst(JFrame.join(left, right, c));
                        break;
                    } else if (joins.isEmpty()) {
                        throw new IllegalStateException(
                                "unresolved variable in condition: " + m_condition + " " + c + trace(joins));
                    } else if (cross) {
                        JFrame lefter = (JFrame) joins.removeFirst();
                        left = JFrame.cross(lefter, left);
                    } else {
                        if (skipped == null) {
                            skipped = new LinkedList();
                        }
                        skipped.addLast(left);
                        left = (JFrame) joins.removeFirst();
                    }
                }

                if (skipped != null) {
                    while (!skipped.isEmpty()) {
                        joins.addFirst(skipped.removeLast());
                    }
                }
            }

            emitted.add(c);
        }
    }
}