Example usage for com.google.common.collect Lists newArrayListWithExpectedSize

List of usage examples for com.google.common.collect Lists newArrayListWithExpectedSize

Introduction

In this page you can find the example usage for com.google.common.collect Lists newArrayListWithExpectedSize.

Prototype

@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) 

Source Link

Document

Creates an ArrayList instance to hold estimatedSize elements, plus an unspecified amount of padding; you almost certainly mean to call #newArrayListWithCapacity (see that method for further advice on usage).

Usage

From source file:org.apache.shindig.social.opensocial.service.RpcRequestItem.java

@Override
public List<String> getListParameter(String paramName) {
    try {//  ww w.java2  s.c o  m
        if (data.has(paramName)) {
            if (data.get(paramName) instanceof JSONArray) {
                JSONArray jsonArray = data.getJSONArray(paramName);
                List<String> returnVal = Lists.newArrayListWithExpectedSize(jsonArray.length());
                for (int i = 0; i < jsonArray.length(); i++) {
                    returnVal.add(jsonArray.getString(i));
                }
                return returnVal;
            } else {
                // Allow up-conversion of non-array to array params.
                return Lists.newArrayList(data.getString(paramName));
            }
        } else {
            return Collections.emptyList();
        }
    } catch (JSONException je) {
        throw new SocialSpiException(ResponseError.BAD_REQUEST, je.getMessage(), je);
    }
}

From source file:com.android.build.gradle.internal.model.DependenciesImpl.java

@NonNull
static DependenciesImpl cloneDependencies(@NonNull BaseVariantData variantData,
        @NonNull AndroidBuilder androidBuilder) {
    VariantDependencies variantDependencies = variantData.getVariantDependency();

    List<AndroidLibrary> libraries;
    List<JavaLibrary> javaLibraries;
    List<String> projects;

    List<LibraryDependencyImpl> libs = variantDependencies.getLibraries();
    libraries = Lists.newArrayListWithCapacity(libs.size());
    for (LibraryDependencyImpl libImpl : libs) {
        AndroidLibrary clonedLib = sCache.get(libImpl);
        if (clonedLib != null) {
            libraries.add(clonedLib);/*from ww w.j  a  v  a  2s  .c om*/
        }
    }

    List<JarDependency> jarDeps = variantDependencies.getJarDependencies();
    List<JarDependency> localDeps = variantDependencies.getLocalDependencies();

    javaLibraries = Lists.newArrayListWithExpectedSize(jarDeps.size() + localDeps.size());
    projects = Lists.newArrayList();

    for (JarDependency jarDep : jarDeps) {
        // don't include package-only dependencies
        if (jarDep.isCompiled()) {
            boolean customArtifact = jarDep.getResolvedCoordinates() != null
                    && jarDep.getResolvedCoordinates().getClassifier() != null;

            File jarFile = jarDep.getJarFile();
            if (!customArtifact && jarDep.getProjectPath() != null) {
                projects.add(jarDep.getProjectPath());
            } else {
                javaLibraries.add(new JavaLibraryImpl(jarFile, null, jarDep.getResolvedCoordinates()));
            }
        }
    }

    for (JarDependency jarDep : localDeps) {
        // don't include package-only dependencies
        if (jarDep.isCompiled()) {
            javaLibraries.add(new JavaLibraryImpl(jarDep.getJarFile(), null, jarDep.getResolvedCoordinates()));
        }
    }

    GradleVariantConfiguration variantConfig = variantData.getVariantConfiguration();

    if (variantConfig.getRenderscriptSupportModeEnabled()) {
        File supportJar = androidBuilder.getRenderScriptSupportJar();
        if (supportJar != null) {
            javaLibraries.add(new JavaLibraryImpl(supportJar, null, null));
        }
    }

    return new DependenciesImpl(libraries, javaLibraries, projects);
}

From source file:org.spongepowered.common.data.manipulator.tileentity.SpongeSignData.java

@Override
public DataContainer toContainer() {
    List<String> jsonLines = Lists.newArrayListWithExpectedSize(4);
    for (Text line : this.lines) {
        jsonLines.add(Texts.json().to(line));
    }// w  w  w .j  a  v  a  2  s  .c  o  m
    return new MemoryDataContainer().set(LINES, jsonLines);
}

From source file:com.android.tools.idea.navigator.nodes.AndroidResFolderNode.java

/**
 * Returns the children of the res folder. Rather than showing the existing directory hierarchy, this merges together
 * all the folders by their {@link com.android.resources.ResourceFolderType}.
 *//* w  ww.  j a v a  2  s . co m*/
@NotNull
@Override
public Collection<? extends AbstractTreeNode> getChildren() {
    // collect all res folders from all source providers
    List<PsiDirectory> resFolders = Lists.newArrayList();
    for (PsiDirectory directory : getSourceDirectories()) {
        resFolders.addAll(Lists.newArrayList(directory.getSubdirectories()));
    }

    // group all the res folders by their folder type
    HashMultimap<ResourceFolderType, PsiDirectory> foldersByResourceType = HashMultimap.create();
    for (PsiDirectory resFolder : resFolders) {
        ResourceFolderType type = ResourceFolderType.getFolderType(resFolder.getName());
        if (type == null) {
            // skip unknown folder types inside res
            continue;
        }
        foldersByResourceType.put(type, resFolder);
    }

    // create a node for each res folder type that actually has some resources
    AndroidProjectTreeBuilder treeBuilder = (AndroidProjectTreeBuilder) myProjectViewPane.getTreeBuilder();
    List<AbstractTreeNode> children = Lists.newArrayListWithExpectedSize(foldersByResourceType.size());
    for (ResourceFolderType type : foldersByResourceType.keySet()) {
        Set<PsiDirectory> folders = foldersByResourceType.get(type);
        final AndroidResFolderTypeNode androidResFolderTypeNode = new AndroidResFolderTypeNode(myProject,
                getValue(), Lists.newArrayList(folders), getSettings(), type, myProjectViewPane);
        children.add(androidResFolderTypeNode);

        // Inform the tree builder of the node that this particular virtual file maps to
        for (PsiDirectory folder : folders) {
            treeBuilder.createMapping(folder.getVirtualFile(), androidResFolderTypeNode);
        }
    }
    return children;

}

From source file:com.android.tools.lint.psi.EcjPsiPackage.java

@SuppressWarnings("SameParameterValue")
private PsiAnnotation[] findAnnotations(boolean includeSuper) {
    List<PsiAnnotation> all = Lists.newArrayListWithExpectedSize(4);
    ExternalAnnotationRepository manager = mManager.getAnnotationRepository();

    PackageBinding binding = this.mPackageBinding;
    AnnotationBinding[] annotations = binding.getAnnotations();
    int count = annotations.length;
    if (count > 0) {
        for (AnnotationBinding annotation : annotations) {
            if (annotation != null) {
                all.add(new EcjPsiBinaryAnnotation(mManager, this, annotation));
            }/*from  w  ww . jav  a 2s. co  m*/
        }
    }

    // Look for external annotations
    if (manager != null) {
        Collection<PsiAnnotation> external = manager.getAnnotations(binding);
        if (external != null) {
            all.addAll(external);
        }
    }

    return EcjPsiManager.ensureUnique(all);
}

From source file:org.apache.mahout.math.neighborhood.Searcher.java

public List<List<WeightedThing<Vector>>> search(Iterable<? extends Vector> queries, int limit) {
    List<List<WeightedThing<Vector>>> results = Lists.newArrayListWithExpectedSize(Iterables.size(queries));
    for (Vector query : queries) {
        results.add(search(query, limit));
    }/*ww  w  . j  a v  a 2s .  c  o  m*/
    return results;
}

From source file:io.druid.java.util.common.parsers.JSONToLowerParser.java

@Override
public Map<String, Object> parse(String input) {
    try {//from  w ww.j  av  a 2  s . c om
        Map<String, Object> map = new LinkedHashMap<>();
        JsonNode root = objectMapper.readTree(input);

        Iterator<String> keysIter = (fieldNames == null ? root.fieldNames() : fieldNames.iterator());

        while (keysIter.hasNext()) {
            String key = keysIter.next();

            if (exclude.contains(key.toLowerCase())) {
                continue;
            }

            JsonNode node = root.path(key);

            if (node.isArray()) {
                final List<Object> nodeValue = Lists.newArrayListWithExpectedSize(node.size());
                for (final JsonNode subnode : node) {
                    final Object subnodeValue = valueFunction.apply(subnode);
                    if (subnodeValue != null) {
                        nodeValue.add(subnodeValue);
                    }
                }
                map.put(key.toLowerCase(), nodeValue); // difference from JSONParser parse()
            } else {
                final Object nodeValue = valueFunction.apply(node);
                if (nodeValue != null) {
                    map.put(key.toLowerCase(), nodeValue); // difference from JSONParser parse()
                }
            }
        }
        return map;
    } catch (Exception e) {
        throw new ParseException(e, "Unable to parse row [%s]", input);
    }
}

From source file:de.learnlib.algorithms.baselinelstar.ObservationTable.java

@Nonnull
public List<Word<I>> getLongPrefixLabels() {
    List<Word<I>> labels = Lists.newArrayListWithExpectedSize(longPrefixRows.size());
    for (ObservationTableRow<I> row : longPrefixRows) {
        labels.add(row.getLabel());//from ww  w.j a  v a2 s  .  c  o  m
    }
    return labels;
}

From source file:com.google.cloud.datastore.BaseDatastoreBatchWriter.java

@SuppressWarnings("unchecked")
@Override//from www.java  2 s  .c o  m
public final List<Entity> add(FullEntity<?>... entities) {
    validateActive();
    List<IncompleteKey> incompleteKeys = Lists.newArrayListWithExpectedSize(entities.length);
    for (FullEntity<?> entity : entities) {
        IncompleteKey key = entity.getKey();
        Preconditions.checkArgument(key != null, "Entity must have a key");
        if (!(key instanceof Key)) {
            incompleteKeys.add(key);
        }
    }
    Iterator<Key> allocated;
    if (!incompleteKeys.isEmpty()) {
        IncompleteKey[] toAllocate = Iterables.toArray(incompleteKeys, IncompleteKey.class);
        allocated = getDatastore().allocateId(toAllocate).iterator();
    } else {
        allocated = Collections.emptyIterator();
    }
    List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length);
    for (FullEntity<?> entity : entities) {
        if (entity.getKey() instanceof Key) {
            addInternal((FullEntity<Key>) entity);
            answer.add(Entity.convert((FullEntity<Key>) entity));
        } else {
            Entity entityWithAllocatedId = Entity.newBuilder(allocated.next(), entity).build();
            addInternal(entityWithAllocatedId);
            answer.add(entityWithAllocatedId);
        }
    }
    return answer;
}

From source file:com.android.tools.klint.client.api.OtherFileVisitor.java

/** Analyze other files in the given project */
void scan(@NonNull LintDriver driver, @NonNull Project project, @Nullable Project main) {
    // Collect all project files
    File projectFolder = project.getDir();

    EnumSet<Scope> scopes = EnumSet.noneOf(Scope.class);
    for (Detector detector : mDetectors) {
        OtherFileScanner fileScanner = (OtherFileScanner) detector;
        EnumSet<Scope> applicable = fileScanner.getApplicableFiles();
        if (applicable.contains(Scope.OTHER)) {
            scopes = Scope.ALL;/*from ww w .jav a 2  s  .co m*/
            break;
        }
        scopes.addAll(applicable);
    }

    List<File> subset = project.getSubset();

    if (scopes.contains(Scope.RESOURCE_FILE)) {
        if (subset != null && !subset.isEmpty()) {
            List<File> files = new ArrayList<File>(subset.size());
            for (File file : subset) {
                if (SdkUtils.endsWith(file.getPath(), DOT_XML)
                        && !file.getName().equals(ANDROID_MANIFEST_XML)) {
                    files.add(file);
                }
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.RESOURCE_FILE, files);
            }
        } else {
            List<File> files = Lists.newArrayListWithExpectedSize(100);
            for (File res : project.getResourceFolders()) {
                collectFiles(files, res);
            }
            File assets = new File(projectFolder, FD_ASSETS);
            if (assets.exists()) {
                collectFiles(files, assets);
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.RESOURCE_FILE, files);
            }
        }
    }

    if (scopes.contains(Scope.SOURCE_FILE)) {
        if (subset != null && !subset.isEmpty()) {
            List<File> files = new ArrayList<File>(subset.size());
            for (File file : subset) {
                if (file.getPath().endsWith(DOT_JAVA)) {
                    files.add(file);
                }
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.SOURCE_FILE, files);
            }
        } else {
            List<File> files = Lists.newArrayListWithExpectedSize(100);
            for (File srcFolder : project.getJavaSourceFolders()) {
                collectFiles(files, srcFolder);
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.SOURCE_FILE, files);
            }
        }
    }

    if (scopes.contains(Scope.CLASS_FILE)) {
        if (subset != null && !subset.isEmpty()) {
            List<File> files = new ArrayList<File>(subset.size());
            for (File file : subset) {
                if (file.getPath().endsWith(DOT_CLASS)) {
                    files.add(file);
                }
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.CLASS_FILE, files);
            }
        } else {
            List<File> files = Lists.newArrayListWithExpectedSize(100);
            for (File classFolder : project.getJavaClassFolders()) {
                collectFiles(files, classFolder);
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.CLASS_FILE, files);
            }
        }
    }

    if (scopes.contains(Scope.MANIFEST)) {
        if (subset != null && !subset.isEmpty()) {
            List<File> files = new ArrayList<File>(subset.size());
            for (File file : subset) {
                if (file.getName().equals(ANDROID_MANIFEST_XML)) {
                    files.add(file);
                }
            }
            if (!files.isEmpty()) {
                mFiles.put(Scope.MANIFEST, files);
            }
        } else {
            List<File> manifestFiles = project.getManifestFiles();
            if (manifestFiles != null) {
                mFiles.put(Scope.MANIFEST, manifestFiles);
            }
        }
    }

    for (Map.Entry<Scope, List<File>> entry : mFiles.entrySet()) {
        Scope scope = entry.getKey();
        List<File> files = entry.getValue();
        List<Detector> applicable = new ArrayList<Detector>(mDetectors.size());
        for (Detector detector : mDetectors) {
            OtherFileScanner fileScanner = (OtherFileScanner) detector;
            EnumSet<Scope> appliesTo = fileScanner.getApplicableFiles();
            if (appliesTo.contains(Scope.OTHER) || appliesTo.contains(scope)) {
                applicable.add(detector);
            }
        }
        if (!applicable.isEmpty()) {
            for (File file : files) {
                Context context = new Context(driver, project, main, file);
                for (Detector detector : applicable) {
                    detector.beforeCheckFile(context);
                    detector.run(context);
                    detector.afterCheckFile(context);
                }
                if (driver.isCanceled()) {
                    return;
                }
            }
        }
    }
}