Example usage for java.util Spliterators spliterator

List of usage examples for java.util Spliterators spliterator

Introduction

In this page you can find the example usage for java.util Spliterators spliterator.

Prototype

public static <T> Spliterator<T> spliterator(Collection<? extends T> c, int characteristics) 

Source Link

Document

Creates a Spliterator using the given collection's java.util.Collection#iterator() as the source of elements, and reporting its java.util.Collection#size() as its initial size.

Usage

From source file:net.dv8tion.jda.core.utils.cache.impl.AbstractCacheView.java

@Override
public Spliterator<T> spliterator() {
    return Spliterators.spliterator(elements.values(), Spliterator.IMMUTABLE);
}

From source file:net.morimekta.providence.maven.plugin.BaseGenerateSourcesMojo.java

boolean executeInternal(File outputDir, IncludeExcludeFileSelector files, String defaultInputIncludes,
        boolean testCompile) throws MojoExecutionException, MojoFailureException {

    Set<File> inputs = ProvidenceInput.getInputFiles(project, files, defaultInputIncludes);
    if (inputs.isEmpty()) {
        return false;
    }/* w  w w.  j a  v  a  2s  . c om*/

    if (!outputDir.exists()) {
        if (!outputDir.mkdirs()) {
            throw new MojoExecutionException("Unable to create target directory " + outputDir);
        }
    }

    TreeSet<File> includes = new TreeSet<>();

    File workingDir = new File(buildDir, testCompile ? "providence-test" : "providence");
    File[] deleteFiles = workingDir.listFiles();
    if (!workingDir.exists()) {
        if (!workingDir.mkdirs()) {
            throw new MojoExecutionException("Unable to create working directory " + workingDir);
        }
    } else if (deleteFiles != null) {
        StreamSupport.<File>stream(
                Spliterators.spliterator(deleteFiles, Spliterator.DISTINCT | Spliterator.IMMUTABLE), false)
                .forEach(File::delete);
    }

    Set<Artifact> resolvedArtifacts = new HashSet<>();
    for (Dependency dep : dependencies) {
        dep.setType(ProvidenceAssemblyMojo.TYPE);
        if (dep.getClassifier() == null || dep.getClassifier().isEmpty()) {
            dep.setClassifier(ProvidenceAssemblyMojo.CLASSIFIER);
        }

        Artifact artifact = repositorySystem.createDependencyArtifact(dep);
        // Avoid resolving stuff we already have resolved.
        if (resolvedArtifacts.contains(artifact)) {
            continue;
        }

        ArtifactResolutionRequest request = new ArtifactResolutionRequest();
        request.setLocalRepository(localRepository);
        request.setRemoteRepositories(remoteRepositories);
        request.setResolveTransitively(false);
        request.setArtifact(artifact);

        ArtifactResolutionResult result = artifactResolver.resolve(request);

        boolean found = false;
        for (Artifact resolved : result.getArtifacts()) {
            if (artifact.equals(resolved)) {
                resolvedArtifacts.add(resolved);
                addDependencyInclude(workingDir, includes, resolved);
                found = true;
                break;
            }
        }
        if (!found) {
            throw new MojoFailureException("Unable to resolve providence dependency: " + artifact.getGroupId()
                    + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + ":"
                    + artifact.getClassifier());
        }
    }

    if (includeDirs != null) {
        DirectoryScanner includeScanner = new DirectoryScanner();
        includeScanner.setIncludes(includeDirs.getIncludes());
        if (includeDirs.getExcludes() != null) {
            includeScanner.setExcludes(includeDirs.getExcludes());
        }
        includeScanner.setBasedir(project.getBasedir());
        includeScanner.scan();
        for (String dir : includeScanner.getIncludedDirectories()) {
            includes.add(new File(project.getBasedir(), dir));
        }
        for (String dir : includeScanner.getExcludedDirectories()) {
            includes.remove(new File(project.getBasedir(), dir));
        }
    }
    inputs.stream().map(File::getParentFile).forEach(includes::add);

    FileManager fileManager = new FileManager(outputDir);
    DocumentParser parser = new ThriftDocumentParser();
    TypeLoader loader = new TypeLoader(includes, parser);

    LinkedList<CDocument> documents = new LinkedList<>();

    for (File in : inputs) {
        try {
            documents.add(loader.load(in));
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to read thrift file: " + in.getName(), e);
        } catch (ParseException e) {
            getLog().warn(e.getMessage());
            getLog().warn(".---------------------.");
            throw new MojoFailureException("Failed to parse thrift file: " + in.getName(), e);
        }
    }

    try {
        Generator generator;
        if (tiny) {
            TinyOptions options = new TinyOptions();
            options.jackson = jackson;
            if (android) {
                throw new MojoExecutionException("Android option not compatible with 'tiny_java' variant.");
            }
            generator = new TinyGenerator(fileManager, loader.getRegistry(), options);
        } else {
            JOptions options = new JOptions();
            options.android = android;
            if (jackson) {
                throw new MojoExecutionException("Jackson option not compatible with 'java' variant.");
            }
            generator = new JGenerator(fileManager, loader.getRegistry(), options);
        }

        for (CDocument doc : documents) {
            try {
                generator.generate(doc);
            } catch (IOException e) {
                throw new MojoExecutionException("Failed to write document: " + doc.getPackageName(), e);
            } catch (GeneratorException e) {
                getLog().warn(e.getMessage());
                throw new MojoFailureException("Failed to generate document: " + doc.getPackageName(), e);
            }
        }
    } catch (GeneratorException e) {
        getLog().warn(e.getMessage());
        throw new MojoFailureException("Failed to generate file: " + e.getMessage(), e);
    }

    return compileOutput;
}