Example usage for java.util Deque size

List of usage examples for java.util Deque size

Introduction

In this page you can find the example usage for java.util Deque size.

Prototype

int size();

Source Link

Document

Returns the number of elements in this deque.

Usage

From source file:org.molasdin.wbase.xml.parser.light.basic.BasicParser.java

@Override
public Element parse(String value) throws ParserException {
    Deque<BasicElement> elements = new LinkedList<BasicElement>();

    int index = 0;
    boolean leftAngleFound = false;
    int leftAngleIndex = 0;

    boolean possibleCloseTag = false;
    int closeSlashIndex = 0;

    BasicElement rootElement = new BasicElement();
    rootElement.setValid(true);/*from   ww w.ja va 2s. c o  m*/
    elements.push(rootElement);

    while (index < value.length()) {
        if (value.charAt(index) == '<') {
            //if '<' is before '<'
            if (leftAngleFound) {
                //treat it as '<' symbol
                //build string from the first '<' till the current
                String entry = value.substring(leftAngleIndex, index);
                appendText(entry, elements.peekFirst());

                invokeHandlers(value, leftAngleIndex, ErrorType.LESS_FOUND, "");
            }
            leftAngleFound = true;
            leftAngleIndex = index;
            possibleCloseTag = false;
        } else if (value.charAt(index) == '/') {
            //if '<' has been found
            if (leftAngleFound) {
                //slash may be in closing tag
                closeSlashIndex = index;
                possibleCloseTag = true;
            } else {
                appendText("/", elements.peekFirst());
            }
        } else if (value.charAt(index) == '>') {
            //if '>' without '<' before
            if (!leftAngleFound) {
                //treat '>' as symbol
                appendText(">", elements.peekFirst());
                invokeHandlers(value, index, ErrorType.GREATER_FOUND, "");
            } else {
                leftAngleFound = false;
                BasicElement elem = elements.peekFirst();
                //check if it is a closing tag
                if (possibleCloseTag && isEmptyRange(value, leftAngleIndex + 1, closeSlashIndex)) {
                    String tag = StringUtils.trim(value.substring(leftAngleIndex + 2, index));
                    //if tag is most possible closing
                    if (!elem.isValid()) {
                        //check first opening
                        elem = elements.pop();
                        if (!elem.tagName().equals(tag)) {
                            BasicElement tmp = elem;
                            elem = elements.pop();
                            //check outer opening
                            if (!elem.tagName().equals(tag)) {
                                throw new BadClosingTag(elem.tagName(), tag);
                            }
                            invokeHandlers(value, -1, ErrorType.NO_CLOSING, tmp.tagName());
                            elem.consumeContent(tmp);
                            elem.setValid(true);
                        }
                    } else {
                        //closing tag without opening
                        throw new BadClosingTag("", tag);
                    }
                    elem.setClosed(true);
                    elem.setValid(true);
                    elem.setValidClosing(true);

                } else {
                    //tag is most possible opening or self closing
                    int rightOffset = index;
                    if (possibleCloseTag) {
                        //check if tag is closing but with characters between "<" and "/"
                        if (!elem.isValid()) {
                            String possibleTag = value.substring(closeSlashIndex + 1, index);
                            if (elem.tagName().equals(possibleTag)) {
                                throw new CharactersInClosing(leftAngleIndex);
                            }
                        }

                        //check if "/" is in attributes
                        if (value.substring(closeSlashIndex + 1, rightOffset).trim().length() == 0) {
                            rightOffset = closeSlashIndex;
                        } else {
                            //tag is no closing
                            possibleCloseTag = false;
                        }

                    }

                    //possible start tag
                    String tagName = value.substring(leftAngleIndex + 1, rightOffset);

                    //if no tag but '<>'
                    if (tagName.length() == 0) {
                        //add them to characters
                        String entry = value.substring(leftAngleIndex, index + 1);
                        appendText(entry, elem);
                        invokeHandlers(value, leftAngleIndex, ErrorType.EMPTY_TAG_FOUND, entry);
                    } else {
                        Pair<String, List<Pair<String, String>>> tag = extractTag(tagName);
                        if (tag == null || tag.getLeft() == null) {
                            invokeHandlers(value, leftAngleIndex, ErrorType.INVALID_TEXT_IN_TAG_NAME,
                                    String.valueOf(index));
                            String entry = value.substring(leftAngleIndex, index + 1);
                            appendText(entry, elements.peekFirst());
                        } else {
                            tagName = tag.getLeft();
                            //if tag is allowed
                            if (!predicate.evaluate(tagName)) {
                                throw new DisallowedTagFound(tagName);
                            }
                            //add new element with this tag
                            BasicElement newElem = new BasicElement();
                            newElem.setTagName(tagName);
                            newElem.setAttributes(tag.getRight());
                            elements.peekFirst().addChild(newElem);
                            if (possibleCloseTag) {
                                newElem.setClosed(true);
                                newElem.setShortenIfEmpty(true);
                                newElem.setValid(true);
                                newElem.setValidClosing(true);
                            } else {
                                elements.push(newElem);
                            }
                        }
                    }
                }

                possibleCloseTag = false;
            }

        } else if (!leftAngleFound) {
            //characters block
            BasicElement elem = elements.peekFirst();
            //if other elements exist between tag characters parts
            elem.addCharacter(value.charAt(index));
            /* if (elementTextBreak) {
            elementTextBreak = false;
            elem.addCharacter(value.charAt(index));
             } else {
            elem.lastCharacters().append(value.charAt(index));
             }*/
        }
        index++;
    }

    //if last '<' has not been closed
    if (leftAngleFound) {
        //treat it as symbol
        appendText(value.substring(leftAngleIndex, value.length()), elements.peekFirst());
        invokeHandlers(value, leftAngleIndex, ErrorType.LESS_FOUND, "");
    }

    //find unclosed elements
    if (elements.size() > 1) {
        for (BasicElement elem : elements) {
            if (elem == rootElement) {
                continue;
            }
            if (!elem.isClosed() && !elem.isValid()) {
                invokeHandlers(value, -1, ErrorType.NO_CLOSING, elem.tagName());
                ((BasicElement) elem.parent()).consumeContent(elem);
            }
        }
    }
    return rootElement;
}

From source file:org.nuxeo.ecm.core.storage.ExpressionEvaluator.java

private static Set<String> parseFullText(String string, int phraseSize) {
    if (string == null) {
        return Collections.emptySet();
    }/*from w  w w  .jav  a2s.  c om*/
    Set<String> set = new HashSet<String>();
    Deque<String> phraseWords = new LinkedList<>();
    for (String word : WORD_PATTERN.split(string)) {
        word = parseWord(word);
        if (word != null) {
            word = word.toLowerCase();
            set.add(word);
            if (phraseSize > 1) {
                phraseWords.addLast(word);
                if (phraseWords.size() > 1) {
                    if (phraseWords.size() > phraseSize) {
                        phraseWords.removeFirst();
                    }
                    addPhraseWords(set, phraseWords);
                }
            }
        }
    }
    while (phraseWords.size() > 2) {
        phraseWords.removeFirst();
        addPhraseWords(set, phraseWords);
    }
    return set;
}

From source file:org.onehippo.cms7.essentials.dashboard.instruction.FileInstruction.java

private void processDirectories(final Deque<String> directories) throws IOException {
    if (!directories.isEmpty()) {
        folderMessage = directories.size() > 1 ? directories.size() - 1 + " directories" : "directory";
        createdFolders = directories.getLast().substring(directories.getFirst().length());
        createdFoldersTarget = directories.getLast();
        Files.createDirectories(new File(directories.getLast()).toPath());
        eventBus.post(new InstructionEvent(messageFolderCreate));
    }/*from   ww w  . j a  va  2 s . co m*/
}

From source file:org.polymap.core.data.pipeline.DefaultPipelineIncubator.java

protected boolean findTransformation(ProcessorDescription from, ProcessorDescription to, LayerUseCase usecase,
        Deque<ProcessorDescription> chain) {
    log.debug(StringUtils.repeat("    ", chain.size()) + "findTransformation: " + from + " => " + to + " -- "
            + usecase);/*from  w  w  w  . jav a2 s .c om*/

    // recursion break
    if (chain.size() > 16) {
        return false;
    }

    // recursion start
    if (from.getSignature().isCompatible(to.getSignature())) {
        chain.addLast(to);
        log.debug(StringUtils.repeat("    ", chain.size()) + "Transformation found: " + chain);
        return true;
    }

    // recursion step
    else {
        for (ProcessorDescription desc : allTransformers(usecase)) {
            if (from.getSignature().isCompatible(desc.getSignature()) && !chain.contains(desc)) {
                chain.addLast(desc);
                if (findTransformation(desc, to, usecase, chain)) {
                    //log.debug( "      transformation found: " + desc );
                    return true;
                }
                chain.removeLast();
            }
        }
        return false;
    }
}