Example usage for java.util Deque addFirst

List of usage examples for java.util Deque addFirst

Introduction

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

Prototype

void addFirst(E e);

Source Link

Document

Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

Usage

From source file:Main.java

public static void main(String[] args) {

    Deque<Integer> deque = new ArrayDeque<Integer>(5);

    deque.add(25);//from w  ww .j  a  v a 2 s .  co  m
    deque.add(30);
    deque.add(35);

    deque.addFirst(10);
    deque.addFirst(15);
    deque.addFirst(20);

    deque.add(45);
    deque.add(40);

    System.out.println(deque);
}

From source file:de.javagl.jgltf.model.io.JsonError.java

/**
 * Create a collection consisting of stream contexts, starting at the root
 * node and ending at the given stream context
 *  //ww w  .j a v a  2 s. c  o m
 * @param streamContext The stream context
 * @return The collection
 */
private static Collection<JsonStreamContext> expand(JsonStreamContext streamContext) {
    Deque<JsonStreamContext> collection = new LinkedList<JsonStreamContext>();
    JsonStreamContext current = streamContext;
    while (current != null) {
        collection.addFirst(current);
        current = current.getParent();
    }
    return collection;
}

From source file:com.frostwire.platform.DefaultFileSystem.java

public static void walkFiles(FileSystem fs, File file, FileFilter filter) {
    File[] arr = fs.listFiles(file, filter);
    if (arr == null) {
        return;//from   w w  w  .  ja  va2 s . c  om
    }
    Deque<File> q = new LinkedList<>(Arrays.asList(arr));

    while (!q.isEmpty()) {
        File child = q.pollFirst();
        filter.file(child);
        if (fs.isDirectory(child)) {
            arr = fs.listFiles(child, filter);
            if (arr != null) {
                for (int i = arr.length - 1; i >= 0; i--) {
                    q.addFirst(arr[i]);
                }
            }
        }
    }
}

From source file:com.epam.cme.storefront.history.impl.DefaultBrowseHistory.java

@Override
public void addBrowseHistoryEntry(final BrowseHistoryEntry browseHistoryEntry) {
    // Get the actual history entry list stored in the session
    final Deque<BrowseHistoryEntry> browseHistoryEntries = getBrowseHistoryEntries();

    if (browseHistoryEntries != null) {
        // Lock on the entries to ensure that we modify it atomically
        synchronized (browseHistoryEntries) {
            // Add the entry
            browseHistoryEntries.addFirst(browseHistoryEntry);

            // Remove any entries that are over capacity
            while (browseHistoryEntries.size() > getCapacity()) {
                browseHistoryEntries.removeLast();
            }/*from w w w  .  j  a  v a 2s  .c  o m*/
        }
    }
}

From source file:info.magnolia.ui.form.field.definition.FieldDefinitionKeyGenerator.java

@Override
protected void keysFor(List<String> list, FieldDefinition field, AnnotatedElement el) {
    Object parent = getParentViaCast(field);
    String fieldName = field.getName().replace(':', '-');
    if (parent != null && isChooseDialog(parent.getClass())) {
        // handle choose dialog
        final AppDescriptor app = (AppDescriptor) getRoot(field);
        addKey(list, app.getName(), "chooseDialog", "fields", fieldName, fieldOrGetterName(el));
    } else {/*  w  w  w .ja v  a 2  s  .c  o  m*/
        final Deque<String> parentNames = new LinkedList<String>();
        while (parent != null && !(parent instanceof TabDefinition)) {
            String parentName = getParentName(parent);
            if (parentName != null) {
                parentNames.addFirst(parentName);
            }
            parent = getParentViaCast(parent);
        }

        final String property = fieldOrGetterName(el);
        final String parentKeyPart = StringUtils.join(parentNames, '.').replace(':', '-');
        if (parent instanceof TabDefinition) {
            TabDefinition tab = (TabDefinition) parent;
            final String tabName = tab.getName();
            final FormDefinition formDef = getParentViaCast(tab);
            final String dialogID = getParentId(formDef);

            // in case of a field in field
            if (parentNames.size() > 0) {
                // <dialogId>.<tabName>.<parentFieldNames_separated_by_dots>.<fieldName>.<property>
                // <dialogId>.<tabName>.<parentFieldNames_separated_by_dots>.<fieldName> (in case of property==label)
                addKey(list, dialogID, tabName, parentKeyPart, fieldName, property);
            }
            // <dialogId>.<tabName>.<fieldName>.<property>
            // <dialogId>.<tabName>.<fieldName> (in case of property==label)
            addKey(list, dialogID, tabName, fieldName, property);
            // <tabName>.<fieldName> (in case of property==label)
            addKey(list, tabName, fieldName, property);
            // <dialogId>.<fieldName>.<property>
            // <dialogId>.<fieldName> (in case property==label)
            addKey(list, dialogID, fieldName, property);

            String[] parts = StringUtils.split(dialogID, ".");
            if (parts.length > 1) {
                String dialogIDNoModuleName = parts[parts.length - 1];
                addKey(list, dialogIDNoModuleName, fieldName, property);
                addKey(list, dialogIDNoModuleName, tabName, fieldName, property);
            }

        } else {
            // In case we didn't encounter parent tab definition - we simply generated a key based on dot-separated parent names
            addKey(list, parentKeyPart, fieldName, property);
        }
    }
}

From source file:com.reprezen.swaggerparser.test.BigParseTest.java

private Object getFromModelObject(JsonOverlay<?> modelObj, Deque<PathKey> path) {
    if (modelObj == null) {
        throw new NullPointerException("Attempt to get value of a null overlay");
    } else if (path.isEmpty()) {
        return modelObj.get();
    } else {//  www .j  a v a2 s.  co m
        PathKey key = path.remove();
        if (modelObj instanceof CollectionOverlay<?>) {
            JsonOverlay<?> item = getFromCollectionOverlay((CollectionOverlay<?>) modelObj, key);
            return getFromModelObject(item, path);
        } else if (modelObj instanceof ObjectOverlay<?>) {
            path.addFirst(key);
            ArrayDeque<PathKey> origPath = Queues.newArrayDeque(path);
            Optional<? extends JsonOverlay<?>> field;
            try {
                field = getField((ObjectOverlay<?>) modelObj, path);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                throw new IllegalStateException("Cannot access object overlay field value", e);
            }
            if (field.isPresent()) {
                return getFromModelObject(field.get(), path);
            } else {
                throw new IllegalArgumentException(
                        "Path does not identify an object overlay field: " + origPath);
            }

        } else {
            throw new UnsupportedOperationException("Attempt to get a component from a primitive overlay");
        }
    }
}

From source file:org.openregistry.core.domain.jpa.JpaPersonImpl.java

@Override
public Map<String, Deque<Identifier>> getIdentifiersByType() {
    final Map<String, Deque<Identifier>> identifiersByType = new HashMap<String, Deque<Identifier>>();

    for (final Identifier identifier : this.identifiers) {
        final String identifierType = identifier.getType().getName();
        Deque<Identifier> listIdentifiers = identifiersByType.get(identifierType);

        if (listIdentifiers == null) {
            listIdentifiers = new ArrayDeque<Identifier>();
            identifiersByType.put(identifierType, listIdentifiers);
        }//  w  w  w.  j  a v  a  2s . co m

        if (identifier.isPrimary()) {
            listIdentifiers.addFirst(identifier);
        } else {
            listIdentifiers.addLast(identifier);
        }
    }

    return identifiersByType;
}

From source file:com.jaspersoft.jasperserver.war.cascade.token.FilterCore.java

@Override
public LinkedHashSet<String> resolveCascadingOrder(Map<String, Set<String>> masterDependencies) {
    Deque<String> orderedNames = new LinkedList<String>();
    Queue<String> workingQueue = new LinkedList<String>(masterDependencies.keySet());
    int maxIterations = (masterDependencies.size() * (masterDependencies.size() + 1)) / 2 + 1;
    while (workingQueue.size() > 0 && maxIterations-- > 0) {
        String currentName = workingQueue.remove();

        Set<String> masterDependency = masterDependencies.get(currentName);
        if (masterDependency == null || masterDependency.isEmpty()) {
            orderedNames.addFirst(currentName);
        } else {/*from  www.ja va 2s.c om*/
            if (orderedNames.containsAll(masterDependency)) {
                orderedNames.addLast(currentName);
            } else {
                workingQueue.add(currentName);
            }
        }
    }
    if (maxIterations > 0) {
        return new LinkedHashSet<String>(orderedNames);
    } else {
        throw new JSException("Order cannot be resolved because of circular or non-existing dependencies.");
    }
}

From source file:edu.berkeley.compbio.phyloutils.HugenholtzTaxonomyService.java

@NotNull
private synchronized Integer getUniqueNodeForMultilevelName(String[] taxa) throws NoSuchNodeException {
    List<String> reverseTaxa = new ArrayList(Arrays.asList(taxa.clone()));
    Collections.reverse(reverseTaxa);

    //final String firstS = reverseTaxa.remove(0);
    //Collection<Integer> trav = null; // = nameToIdMap.get(firstS);

    /*while (trav.isEmpty())
       {//from w  w w.j  ava2  s . c o  m
       logger.warn("IGNORING Node " + s + " not found in " + DSStringUtils.join(taxa, "; "));
       continue;
       }
    */

    Set<Deque<Integer>> paths = null;
    HashMultimap<String, Integer> nameToIdsMap = (HashMultimap<String, Integer>) nameToIdsMapStub.get();
    HashMultimap<String, Integer> extraNameToIdsMap = (HashMultimap<String, Integer>) extraNameToIdsMapStub
            .get();

    for (String s : reverseTaxa) {
        Collection<Integer> matchingNodes = nameToIdsMap.get(s);
        if (matchingNodes.isEmpty()) {
            matchingNodes = extraNameToIdsMap.get(s);
        }
        if (matchingNodes.isEmpty()) {
            logger.debug("IGNORING Node " + s + " not found in " + DSStringUtils.join(taxa, "; "));
        } else {
            //   Set<Integer> nextTrav = new HashSet<Integer>();
            if (paths == null) {
                paths = new HashSet<Deque<Integer>>(matchingNodes.size());
                //nextTrav.addAll(matchingNodes);
                for (Integer node : matchingNodes) {
                    Deque<Integer> l = new LinkedList<Integer>();
                    l.add(node);
                    paths.add(l);
                }
            } else {
                BasicRootedPhylogeny<Integer> theIntegerTree = (BasicRootedPhylogeny<Integer>) theIntegerTreeStub
                        .get();

                Set<Deque<Integer>> okPaths = new HashSet<Deque<Integer>>();
                for (Deque<Integer> path : paths) {
                    Integer descendant = path.peek();
                    for (Integer ancestor : matchingNodes) {
                        if (theIntegerTree.isDescendant(ancestor, descendant)) {
                            path.addFirst(ancestor);
                            okPaths.add(path);
                        }
                    }
                }
                paths = okPaths; // ditch any paths that didn't have an ancestor added this round
            }

            if (paths.isEmpty()) {
                // we get here only if
                //  a) there was more than one live path on the last round
                //  b) none of those paths are descendants of the matches at the current level
                throw new NoSuchNodeException(
                        "Requested classification path does not match tree: " + DSStringUtils.join(taxa, "; "));
            }

            // if all the paths converge on exactly one node, call it a match, even if higher levels of the tree don't match.

            if (matchingNodes.size() == 1) {
                return commonAncestor(paths);
            }
        }
    }
    throw new NoSuchNodeException("Multiple distinct matching paths: " + DSStringUtils.join(taxa, "; "));
    //return commonAncestor(paths);
}

From source file:cc.kave.commons.pointsto.analysis.unification.UnificationAnalysisVisitorContext.java

private void updateUnificationWorklist(Deque<Pair<ReferenceLocation, ReferenceLocation>> worklist,
        int previousIdentifiersSize, ReferenceLocation location, ReferenceLocation unificationPartner) {
    int newIdentifiersSize = location.getIdentifiers().size();
    if (newIdentifiersSize != previousIdentifiersSize) {
        for (ReferenceLocation dependentLocation : pendingUnifications.get(location)) {
            if (dependentLocation != unificationPartner) {
                worklist.addFirst(ImmutablePair.of(location, dependentLocation));
            }/*w w w. j a  v a 2  s.c om*/
        }
    }
}