Example usage for java.util Queue add

List of usage examples for java.util Queue add

Introduction

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

Prototype

boolean add(E e);

Source Link

Document

Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.

Usage

From source file:com.googlesource.gerrit.plugins.supermanifest.JiriManifestParser.java

public static JiriProjects getProjects(GerritRemoteReader reader, String repoKey, String ref, String manifest)
        throws ConfigInvalidException, IOException {

    try (RepoMap<String, Repository> repoMap = new RepoMap<>()) {
        repoMap.put(repoKey, reader.openRepository(repoKey));
        Queue<ManifestItem> q = new LinkedList<>();
        q.add(new ManifestItem(repoKey, manifest, ref, "", false));
        HashMap<String, HashSet<String>> processedRepoFiles = new HashMap<>();
        HashMap<String, JiriProjects.Project> projectMap = new HashMap<>();

        while (q.size() != 0) {
            ManifestItem mi = q.remove();
            Repository repo = repoMap.get(mi.repoKey);
            if (repo == null) {
                repo = reader.openRepository(mi.repoKey);
                repoMap.put(mi.repoKey, repo);
            }/*  w  ww. ja  v a  2 s .c om*/
            HashSet<String> processedFiles = processedRepoFiles.get(mi.repoKey);
            if (processedFiles == null) {
                processedFiles = new HashSet<String>();
                processedRepoFiles.put(mi.repoKey, processedFiles);
            }
            if (processedFiles.contains(mi.manifest)) {
                continue;
            }
            processedFiles.add(mi.manifest);
            JiriManifest m;
            try {
                m = parseManifest(repo, mi.ref, mi.manifest);
            } catch (JAXBException | XMLStreamException e) {
                throw new ConfigInvalidException("XML parse error", e);
            }

            for (JiriProjects.Project project : m.projects.getProjects()) {
                project.fillDefault();
                if (mi.revisionPinned && project.Key().equals(mi.projectKey)) {
                    project.setRevision(mi.ref);
                }
                if (projectMap.containsKey(project.Key())) {
                    if (!projectMap.get(project.Key()).equals(project))
                        throw new ConfigInvalidException(String.format(
                                "Duplicate conflicting project %s in manifest %s\n%s\n%s", project.Key(),
                                mi.manifest, project.toString(), projectMap.get(project.Key()).toString()));
                } else {
                    projectMap.put(project.Key(), project);
                }
            }

            URI parentURI;
            try {
                parentURI = new URI(mi.manifest);
            } catch (URISyntaxException e) {
                throw new ConfigInvalidException("Invalid parent URI", e);
            }
            for (JiriManifest.LocalImport l : m.imports.getLocalImports()) {
                ManifestItem tw = new ManifestItem(mi.repoKey, parentURI.resolve(l.getFile()).getPath(), mi.ref,
                        mi.projectKey, mi.revisionPinned);
                q.add(tw);
            }

            for (JiriManifest.Import i : m.imports.getImports()) {
                i.fillDefault();
                URI uri;
                try {
                    uri = new URI(i.getRemote());
                } catch (URISyntaxException e) {
                    throw new ConfigInvalidException("Invalid URI", e);
                }
                String iRepoKey = new Project.NameKey(StringUtils.strip(uri.getPath(), "/")).toString();
                String iRef = i.getRevision();
                boolean revisionPinned = true;
                if (iRef.isEmpty()) {
                    iRef = REFS_HEADS + i.getRemotebranch();
                    revisionPinned = false;
                }

                ManifestItem tmi = new ManifestItem(iRepoKey, i.getManifest(), iRef, i.Key(), revisionPinned);
                q.add(tmi);
            }
        }
        return new JiriProjects(projectMap.values().toArray(new JiriProjects.Project[0]));
    }
}

From source file:org.jbpm.services.task.jaxb.ComparePair.java

public static void compareOrig(Object origObj, Object newObj, Class objClass) {
    ComparePair compare = new ComparePair(origObj, newObj, objClass);
    Queue<ComparePair> compares = new LinkedList<ComparePair>();
    compares.add(compare);
    while (!compares.isEmpty()) {
        compares.addAll(compares.poll().compare());
    }//  www .j  a  va2s .  c  o  m
}

From source file:playground.johannes.statistics.SampledGraphStatistics.java

public static SortedSet<Collection<SampledVertex>> getDisconnectedComponents(SampledGraph g) {
    TreeSet<Collection<SampledVertex>> clusters = new TreeSet<Collection<SampledVertex>>(new SizeComparator());
    CentralityGraphDecorator graphDecorator = new CentralityGraphDecorator(g);
    UnweightedDijkstra dijkstra = new UnweightedDijkstra((CentralityGraph) graphDecorator.getSparseGraph());
    Queue<CentralityVertex> vertices = new LinkedList<CentralityVertex>();
    for (SparseVertex v : graphDecorator.getSparseGraph().getVertices())
        vertices.add((CentralityVertex) v);

    CentralityVertex source;//  w  ww  . ja  v  a 2  s  .co  m
    while ((source = vertices.poll()) != null) {
        List<CentralityVertex> reached = dijkstra.run(source);
        reached.add(source);
        List<SampledVertex> reached2 = new LinkedList<SampledVertex>();
        for (CentralityVertex cv : reached)
            reached2.add((SampledVertex) graphDecorator.getVertex(cv));
        clusters.add(reached2);
        vertices.removeAll(reached);
    }

    return clusters;
}

From source file:au.org.ala.delta.editor.EditorPreferences.java

/**
 * Adds the supplied filename to the top of the most recently used files.
 * /*from   ww  w  . java 2 s . co m*/
 * @param filename
 */
public static void addFileToMRU(String filename) {

    Queue<String> q = new LinkedList<String>();

    q.add(filename);

    String[] existingFiles = getPreviouslyUsedFiles();
    if (existingFiles != null) {
        for (String existingFile : existingFiles) {
            if (!q.contains(existingFile)) {
                q.add(existingFile);
            }
        }
    }

    StringBuilder b = new StringBuilder();
    for (int i = 0; i < MAX_SIZE_MRU && q.size() > 0; ++i) {
        if (i > 0) {
            b.append(MRU_SEPARATOR);
        }
        b.append(q.poll());
    }

    Preferences prefs = Preferences.userNodeForPackage(DeltaEditor.class);
    prefs.put(MRU_PREF_KEY, b.toString());
    try {
        prefs.sync();
    } catch (BackingStoreException e) {
        throw new RuntimeException(e);
    }
}

From source file:playground.johannes.statistics.GraphStatistics.java

public static SortedSet<Collection<Vertex>> getDisconnectedComponents(Graph g) {
    TreeSet<Collection<Vertex>> clusters = new TreeSet<Collection<Vertex>>(new SizeComparator());
    CentralityGraphDecorator graphDecorator = new CentralityGraphDecorator(g);
    UnweightedDijkstra dijkstra = new UnweightedDijkstra((CentralityGraph) graphDecorator.getSparseGraph());
    Queue<CentralityVertex> vertices = new LinkedList<CentralityVertex>();
    for (SparseVertex v : graphDecorator.getSparseGraph().getVertices())
        vertices.add((CentralityVertex) v);

    CentralityVertex source;// www.java 2 s.  com
    while ((source = vertices.poll()) != null) {
        List<CentralityVertex> reached = dijkstra.run(source);
        reached.add(source);
        List<Vertex> reached2 = new LinkedList<Vertex>();
        for (CentralityVertex cv : reached)
            reached2.add(graphDecorator.getVertex(cv));
        clusters.add(reached2);
        vertices.removeAll(reached);
    }

    return clusters;
}

From source file:org.janusgraph.util.system.ConfigurationFileFilter.java

public static int filter(String inputContextDirPath, String outputContextDirPath) throws IOException {

    // Read args[0] as a dirname and iterate recursively over its file contents
    File inputContextDir = new File(inputContextDirPath);
    File outputContextDir = new File(outputContextDirPath);

    log.info("Input context dir:  {}", inputContextDir);
    log.info("Output context dir: {}", outputContextDir);
    Preconditions.checkArgument(inputContextDir.isDirectory(), "Input context dir %s is not a directory",
            inputContextDir);//from  w  w w.  jav  a2 s  . c om
    Preconditions.checkArgument(inputContextDir.canRead(), "Input context dir %s is not readable",
            inputContextDir);

    if (!outputContextDir.exists()) {
        outputContextDir.mkdirs(); // may fail if path exists as a file
    }

    Queue<InputRecord> dirQueue = new LinkedList<InputRecord>();
    dirQueue.add(new InputRecord(inputContextDir, File.separator));
    int parseErrors = 0;
    int visitedDirs = 0;
    int processedFiles = 0;
    InputRecord rec;
    while (null != (rec = dirQueue.poll())) {
        File curDir = rec.getDirectory();
        String contextPath = rec.getContextPath();

        Preconditions.checkState(curDir.exists());
        Preconditions.checkState(curDir.isDirectory());
        Preconditions.checkState(curDir.canRead());

        visitedDirs++;

        for (File f : curDir.listFiles()) {
            if (f.isDirectory()) {
                if (!f.canRead()) {
                    log.warn("Skipping unreadable directory {} in input basedir", f);
                    continue;
                }

                dirQueue.add(new InputRecord(f, contextPath + f.getName() + File.separator));
            } else {
                if (!f.canRead()) {
                    log.warn("Skipping unreadable file {} in input basedir", f);
                    continue;
                }

                File outputDir = new File(outputContextDir.getPath() + contextPath);

                if (!outputDir.exists()) {
                    outputDir.mkdirs();
                }

                parseErrors += processFile(f, new File(outputContextDir.getPath() + contextPath + f.getName()));

                processedFiles++;
            }
        }
    }

    String summaryTemplate = "Summary: visited {} dir(s) and processed {} file(s) with {} parse error(s).";

    if (0 == parseErrors) {
        log.info(summaryTemplate, visitedDirs, processedFiles, parseErrors);
    } else {
        log.error(summaryTemplate, visitedDirs, processedFiles, parseErrors);
    }

    return parseErrors;
}

From source file:main.java.whiteSocket.Area.java

public static boolean[][] floodBorder(Area area, boolean[][] floodArea, int x, int y) {
    /**//from   w w w .j  a  va 2s .  co  m
     * paint bucket-like algorithm to fill binary map border this filled
     * area becomes the area to be filtered through the stretch() area
     * pixels to be turned Color.WHITE (unless notified otherwise by user
     * dictation)
     *
     */

    if (!floodArea[y][x]) {

        Queue<Point> queue = new LinkedList<Point>();
        queue.add(new Point(x, y));

        while (!queue.isEmpty()) {

            Point p = queue.remove();

            if (!floodArea[p.y][p.x]) {

                floodArea[p.y][p.x] = true;

                //                  System.out.println("totalErase = " + totalErase);
                if (totalErase != null)
                    totalErase[p.y][p.x] = true;

                queue.add(new Point(p.x + 1, p.y));
                queue.add(new Point(p.x - 1, p.y));
                queue.add(new Point(p.x, p.y + 1));
                queue.add(new Point(p.x, p.y - 1));

            }
        }
    }

    return floodArea;
}

From source file:com.insightml.utils.Collections.java

public static <I> Queue<I> newConcurrentList(final Iterable<I> elements) {
    final Queue<I> queue = newConcurrentList();
    for (final I element : elements) {
        queue.add(element);
    }/*ww  w.ja  v  a  2  s  .  c o m*/
    return queue;
}

From source file:com.mohawk.webcrawler.ScriptCompiler.java

/**
 * Parses out each token in the program, and then calls addScope.
 *
 * @param programText//w  w  w  .j  a  v  a  2 s.  co  m
 * @return
 * @throws CompilationException
 */
private static ScriptExecutable compile0(String programText) throws CompilationException {

    Pattern p = Pattern.compile("\"[^\"]+\"|\\([^\\)]+\\)|[^ \r\n]+");
    Matcher m = p.matcher(programText);

    ArrayList<String> tokensList = new ArrayList<>();
    while (m.find())
        tokensList.add(m.group(0).trim());

    Queue<String> tokensQueue = new LinkedList<>();
    for (String token : tokensList) {
        if (token.trim().length() > 0)
            tokensQueue.add(token);
    }

    // root scope, wil contain both String and BaseVerb objects
    LinkedList<BaseToken> rootScope = new LinkedList<>();
    addScope(tokensQueue, rootScope);

    return new ScriptExecutable(rootScope);
}

From source file:interactivespaces.activity.ActivityStateTransitioner.java

/**
 * Create the queue of transitions in a syntactically pleasing way.
 *
 * @param transitions//  ww  w. jav a  2  s .  co m
 *          the transitions to be queued
 *
 * @return the queue of transitions
 */
public static Queue<ActivityStateTransition> transitions(ActivityStateTransition... transitions) {
    Queue<ActivityStateTransition> result = Lists.newLinkedList();

    if (transitions != null) {
        for (ActivityStateTransition transition : transitions) {
            result.add(transition);
        }
    }

    return result;
}