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

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

Introduction

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

Prototype

@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayListWithCapacity(int initialArraySize) 

Source Link

Document

Creates an ArrayList instance backed by an array with the specified initial size; simply delegates to ArrayList#ArrayList(int) .

Usage

From source file:com.android.contacts.list.GroupMemberTileAdapter.java

@Override
public ArrayList<ContactEntry> getItem(int position) {
    final ArrayList<ContactEntry> resultList = Lists.newArrayListWithCapacity(mColumnCount);
    int contactIndex = position * mColumnCount;

    for (int columnCounter = 0; columnCounter < mColumnCount; columnCounter++) {
        resultList.add(createContactEntryFromCursor(mContactCursor, contactIndex));
        contactIndex++;/*from  w  w  w . ja v  a2 s .co  m*/
    }
    return resultList;
}

From source file:org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows.java

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();//w w w.  ja  v a  2s. c  om
    this.result = Lists.newArrayListWithCapacity((int) (size / slide));
}

From source file:org.sonatype.nexus.jmx.MBeanBuilder.java

public MBean build() {
    // build attribute-info
    List<MBeanAttributeInfo> ainfos = Lists.newArrayListWithCapacity(attributes.size());
    for (MBeanAttribute attribute : attributes) {
        ainfos.add(attribute.getInfo());
    }//from  w w w.  jav a2 s .  c o m

    // build operation-info
    List<MBeanOperationInfo> oinfos = Lists.newArrayListWithCapacity(operations.size());
    for (MBeanOperation operation : operations) {
        oinfos.add(operation.getInfo());
    }

    // TODO: Sort out if we want to support ctor or notification muck
    MBeanConstructorInfo[] cinfos = {};
    MBeanNotificationInfo[] ninfos = {};

    MBeanInfo info = new MBeanInfo(className, description,
            ainfos.toArray(new MBeanAttributeInfo[ainfos.size()]), cinfos,
            oinfos.toArray(new MBeanOperationInfo[oinfos.size()]), ninfos, descriptor);

    log.trace("Building mbean with info: {}", info);
    return new MBean(info, attributes, operations);
}

From source file:com.google.gerrit.lifecycle.LifecycleManager.java

private static <T> List<T> newList() {
    return Lists.newArrayListWithCapacity(4);
}

From source file:org.eclipse.emf.eson.xtextbackpatch.FasterFlatResourceSetBasedAllContainersState.java

@Override
public Collection<URI> getContainedURIs(String containerHandle) {
    if (!HANDLE.equals(containerHandle))
        return Collections.emptySet();
    if (resourceSet instanceof XtextResourceSet) {
        XtextResourceSet xtextResourceSet = (XtextResourceSet) resourceSet;
        ResourceDescriptionsData descriptionsData = ResourceDescriptionsData.ResourceSetAdapter
                .findResourceDescriptionsData(resourceSet);
        if (descriptionsData != null) {
            return descriptionsData.getAllURIs();
        }/* w w  w . j  av a 2  s  .c o  m*/
        return newArrayList(xtextResourceSet.getNormalizationMap().values());
    }
    List<URI> uris = Lists.newArrayListWithCapacity(resourceSet.getResources().size());
    URIConverter uriConverter = resourceSet.getURIConverter();
    for (Resource r : resourceSet.getResources())
        uris.add(uriConverter.normalize(r.getURI()));
    return uris;
}

From source file:com.lastcalc.parsers.web.Select.java

@Override
public ParseResult parse(final TokenList tokens, final int templatePos, final ParserContext context) {
    final String selectStatement = ((QuotedString) tokens.get(templatePos + 1)).value;
    final Object docOrElement = tokens.get(templatePos + 3);
    final Object cached = ObjectCache.getFast(Long.MAX_VALUE, "select", selectStatement, docOrElement);
    if (cached != null)
        return ParseResult
                .success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), cached));
    Element el;/*from w ww .  j a  v a  2  s . c om*/
    if (docOrElement instanceof DocumentWrapper) {
        el = ((DocumentWrapper) docOrElement).doc;
    } else {
        el = ((ElementWrapper) docOrElement).el;
    }
    final Elements elements = el.select(selectStatement);

    if (elements.size() == 1)
        return ParseResult.success(tokens.replaceWithTokens(templatePos, templatePos + template.size(),
                new ElementWrapper(elements.first())));
    else {
        final List<Object> elementList = Lists.newArrayListWithCapacity(elements.size());
        for (final Element e : elements) {
            elementList.add(new ElementWrapper(e));
        }
        return ParseResult
                .success(tokens.replaceWithTokens(templatePos, templatePos + template.size(), elementList));
    }
}

From source file:org.terasology.entitySystem.metadata.extension.AssetTypeHandler.java

@Override
public List<T> deserializeList(EntityData.Value value) {
    List<T> result = Lists.newArrayListWithCapacity(value.getStringCount());
    for (String item : value.getStringList()) {
        AssetUri uri = new AssetUri(type, item);
        if (uri.isValid()) {
            Asset asset = Assets.get(uri);
            if (asset != null && assetClass.isAssignableFrom(asset.getClass())) {
                result.add(assetClass.cast(asset));
            }//from w  w w. ja v a  2 s .  co  m
        }
    }
    return result;
}

From source file:com.google.idea.blaze.base.sync.aspects.IdeInfoFromProtobuf.java

@Nullable
public static TargetIdeInfo makeTargetIdeInfo(WorkspaceLanguageSettings workspaceLanguageSettings,
        IntellijIdeInfo.TargetIdeInfo message) {
    Kind kind = getKind(message);/*  ww  w .j  a  va2 s.c  o  m*/
    if (kind == null) {
        return null;
    }
    if (!workspaceLanguageSettings.isLanguageActive(kind.getLanguageClass())) {
        return null;
    }

    final TargetKey key;
    if (message.hasKey()) {
        key = makeTargetKey(message.getKey());
    } else {
        key = TargetKey.forPlainTarget(new Label(message.getLabel()));
    }

    ArtifactLocation buildFile = getBuildFile(message);

    final Collection<Dependency> dependencies;
    if (message.getDepsCount() > 0) {
        dependencies = message.getDepsList().stream().map(IdeInfoFromProtobuf::makeDependency)
                .collect(toList());
    } else {
        dependencies = Lists
                .newArrayListWithCapacity(message.getDependenciesCount() + message.getRuntimeDepsCount());
        dependencies.addAll(
                makeDependencyListFromLabelList(message.getDependenciesList(), DependencyType.COMPILE_TIME));
        dependencies
                .addAll(makeDependencyListFromLabelList(message.getRuntimeDepsList(), DependencyType.RUNTIME));
    }

    Collection<String> tags = ImmutableList.copyOf(message.getTagsList());

    Collection<ArtifactLocation> sources = Lists.newArrayList();
    CIdeInfo cIdeInfo = null;
    if (message.hasCIdeInfo()) {
        cIdeInfo = makeCIdeInfo(message.getCIdeInfo());
        sources.addAll(cIdeInfo.sources);
    }
    CToolchainIdeInfo cToolchainIdeInfo = null;
    if (message.hasCToolchainIdeInfo()) {
        cToolchainIdeInfo = makeCToolchainIdeInfo(message.getCToolchainIdeInfo());
    }
    JavaIdeInfo javaIdeInfo = null;
    if (message.hasJavaIdeInfo()) {
        javaIdeInfo = makeJavaIdeInfo(message.getJavaIdeInfo());
        Collection<ArtifactLocation> javaSources = makeArtifactLocationList(
                message.getJavaIdeInfo().getSourcesList());
        sources.addAll(javaSources);
    }
    AndroidIdeInfo androidIdeInfo = null;
    if (message.hasAndroidIdeInfo()) {
        androidIdeInfo = makeAndroidIdeInfo(message.getAndroidIdeInfo());
    }
    PyIdeInfo pyIdeInfo = null;
    if (message.hasPyIdeInfo()) {
        pyIdeInfo = makePyIdeInfo(message.getPyIdeInfo());
        sources.addAll(pyIdeInfo.sources);
    }
    TestIdeInfo testIdeInfo = null;
    if (message.hasTestInfo()) {
        testIdeInfo = makeTestIdeInfo(message.getTestInfo());
    }
    ProtoLibraryLegacyInfo protoLibraryLegacyInfo = null;
    if (message.hasProtoLibraryLegacyJavaIdeInfo()) {
        protoLibraryLegacyInfo = makeProtoLibraryLegacyInfo(message.getProtoLibraryLegacyJavaIdeInfo());
    }
    JavaToolchainIdeInfo javaToolchainIdeInfo = null;
    if (message.hasJavaToolchainIdeInfo()) {
        javaToolchainIdeInfo = makeJavaToolchainIdeInfo(message.getJavaToolchainIdeInfo());
    }

    return new TargetIdeInfo(key, kind, buildFile, dependencies, tags, sources, cIdeInfo, cToolchainIdeInfo,
            javaIdeInfo, androidIdeInfo, pyIdeInfo, testIdeInfo, protoLibraryLegacyInfo, javaToolchainIdeInfo);
}

From source file:com.facebook.buck.json.RawParser.java

/**
 * @return One of: String, Boolean, Long, Number, List<Object>, Map<String, Object>.
 *///  w w  w . j  a  v a 2  s  .co  m
@Nullable
@VisibleForTesting
static Object toRawTypes(JsonElement json) {
    // Cases are ordered from most common to least common.
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            return interner.intern(primitive.getAsString());
        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isNumber()) {
            Number number = primitive.getAsNumber();
            // Number is likely an instance of class com.google.gson.internal.LazilyParsedNumber.
            if (number.longValue() == number.doubleValue()) {
                return number.longValue();
            } else {
                return number;
            }
        } else {
            throw new IllegalStateException("Unknown primitive type: " + primitive);
        }
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        List<Object> out = Lists.newArrayListWithCapacity(array.size());
        for (JsonElement item : array) {
            out.add(toRawTypes(item));
        }
        return out;
    } else if (json.isJsonObject()) {
        Map<String, Object> out = new LinkedHashMap<>(json.getAsJsonObject().entrySet().size());
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            // On a large project, without invoking intern(), we have seen `buck targets` OOM. When this
            // happened, according to the .hprof file generated using -XX:+HeapDumpOnOutOfMemoryError,
            // 39.6% of the memory was spent on char[] objects while 14.5% was spent on Strings.
            // (Another 10.5% was spent on java.util.HashMap$Entry.) Introducing intern() stopped the
            // OOM from happening.
            out.put(interner.intern(entry.getKey()), toRawTypes(entry.getValue()));
        }
        return out;
    } else if (json.isJsonNull()) {
        return null;
    } else {
        throw new IllegalStateException("Unknown type: " + json);
    }
}

From source file:com.netflix.aegisthus.io.writable.RowWritable.java

@Override
public void readFields(DataInput in) throws IOException {
    deletedAt = in.readLong();/*from   www  .jav  a2s. co m*/
    int columnCount = in.readInt();
    columns = Lists.newArrayListWithCapacity(columnCount);
    for (int i = 0; i < columnCount; i++) {
        OnDiskAtom atom = serializer.deserializeFromSSTable(in, ColumnSerializer.Flag.PRESERVE_SIZE,
                Integer.MIN_VALUE, version);
        columns.add(atom);
    }
}