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:ei.ne.ke.cassandra.cql3.IdClassEntitySpecification.java

/**
 * {@inheritDoc}/*from w w w.jav a2 s.  c  o m*/
 */
@Override
@SuppressWarnings("unchecked")
public List<ID> getKey(List<T> entities) {
    List<ID> keys = Lists.newArrayListWithExpectedSize(entities.size());
    for (T entity : entities) {
        keys.add((ID) entity);
    }
    return keys;
}

From source file:org.apache.crunch.types.writable.WritableTableType.java

@Override
public ReadableSource<Pair<K, V>> createSourceTarget(Configuration conf, Path path,
        Iterable<Pair<K, V>> contents, int parallelism) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    outputFn.setConfiguration(conf);/*from ww  w  .ja v a 2s . c o m*/
    outputFn.initialize();
    fs.mkdirs(path);
    List<SequenceFile.Writer> writers = Lists.newArrayListWithExpectedSize(parallelism);
    for (int i = 0; i < parallelism; i++) {
        Path out = new Path(path, "out" + i);
        writers.add(SequenceFile.createWriter(fs, conf, out, keyType.getSerializationClass(),
                valueType.getSerializationClass()));
    }
    int target = 0;
    for (Pair<K, V> value : contents) {
        Pair writablePair = (Pair) outputFn.map(value);
        writers.get(target).append(writablePair.first(), writablePair.second());
        target = (target + 1) % parallelism;
    }
    for (SequenceFile.Writer writer : writers) {
        writer.close();
    }
    ReadableSource<Pair<K, V>> ret = new SeqFileTableSource<K, V>(path, this);
    ret.inputConf(RuntimeParameters.DISABLE_COMBINE_FILE, "true");
    return ret;
}

From source file:org.apache.phoenix.coprocessor.ScanRegionObserver.java

public static OrderedResultIterator deserializeFromScan(Scan scan, RegionScanner s) {
    byte[] topN = scan.getAttribute(BaseScannerRegionObserver.TOPN);
    if (topN == null) {
        return null;
    }//from  w ww . j  a  va 2s.co  m
    ByteArrayInputStream stream = new ByteArrayInputStream(topN); // TODO: size?
    try {
        DataInputStream input = new DataInputStream(stream);
        int thresholdBytes = WritableUtils.readVInt(input);
        int limit = WritableUtils.readVInt(input);
        int estimatedRowSize = WritableUtils.readVInt(input);
        int size = WritableUtils.readVInt(input);
        List<OrderByExpression> orderByExpressions = Lists.newArrayListWithExpectedSize(size);
        for (int i = 0; i < size; i++) {
            OrderByExpression orderByExpression = new OrderByExpression();
            orderByExpression.readFields(input);
            orderByExpressions.add(orderByExpression);
        }
        ResultIterator inner = new RegionScannerResultIterator(s);
        return new OrderedResultIterator(inner, orderByExpressions, thresholdBytes, limit >= 0 ? limit : null,
                estimatedRowSize);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:org.apache.kylin.metadata.expression.CaseTupleExpression.java

@Override
public void deserialize(IFilterCodeSystem<?> cs, ByteBuffer buffer) {
    int nWhenEntries = BytesUtil.readVInt(buffer);
    List<Pair<TupleFilter, TupleExpression>> whenList = Lists.newArrayListWithExpectedSize(nWhenEntries);
    for (int i = 0; i < nWhenEntries; i++) {
        TupleFilter tupleFilter = TupleFilterSerializer.deserialize(BytesUtil.readByteArray(buffer), cs);
        TupleExpression tupleExpression = TupleExpressionSerializer.deserialize(BytesUtil.readByteArray(buffer),
                cs);/*from   w ww.ja va2s.c o  m*/
        whenList.add(new Pair<>(tupleFilter, tupleExpression));
    }
    this.whenList = whenList;
    int flag = BytesUtil.readVInt(buffer);
    if (flag == 1) {
        this.elseExpr = TupleExpressionSerializer.deserialize(BytesUtil.readByteArray(buffer), cs);
    }
}

From source file:org.gradoop.flink.algorithms.fsm.dimspan.functions.mining.GrowFrequentPatterns.java

@Override
public void open(Configuration parameters) throws Exception {
    super.open(parameters);

    // broadcast reception

    patternFrequencies = getRuntimeContext().getBroadcastVariable(DIMSpanConstants.FREQUENT_PATTERNS);

    int patternCount = patternFrequencies.size();

    this.frequentPatterns = Lists.newArrayListWithExpectedSize(patternCount);

    for (WithCount<int[]> patternWithCount : patternFrequencies) {
        int[] pattern = patternWithCount.getObject();

        // uncompress
        if (uncompressFrequentPatterns) {
            pattern = Simple16Compressor.uncompress(pattern);
        }/* www.  jav a2s .c  o m*/
        frequentPatterns.add(pattern);
    }

    // sort
    frequentPatterns.sort(new DFSCodeComparator());

    // calculate rightmost paths
    this.rightmostPaths = Lists.newArrayListWithExpectedSize(patternCount);
    this.compressedFrequentPatterns = Lists.newArrayListWithExpectedSize(patternCount);

    for (int[] pattern : frequentPatterns) {
        rightmostPaths.add(gSpan.getRightmostPathTimes(pattern));

        // TODO: directly store compressed patterns at reception
        compressedFrequentPatterns.add(compressPatterns ? Simple16Compressor.compress(pattern) : pattern);
    }
}

From source file:com.textocat.textokit.postagger.opennlp.DictionaryBasedContextGenerator.java

public List<String> extract(Token focusToken, String prevTag) {
    if (!(focusToken instanceof W)) {
        return ImmutableList.of();
    }//from   ww w. j a v a  2 s .c o m
    String form = focusToken.getCoveredText();
    if (!WordUtils.isRussianWord(form)) {
        return ImmutableList.of("DL=NotRussian");
    }
    form = WordUtils.normalizeToDictionaryForm(form);
    List<Wordform> dictWfs = morphDict.getEntries(form);
    if (dictWfs == null || dictWfs.isEmpty()) {
        return ImmutableList.of("DL=Unknown");
    }
    List<BitSet> dictWfBitSets = Lists.transform(dictWfs, Wordform.allGramBitsFunction(morphDict));
    //
    Set<BitSet> tokenPossibleTags = Sets.newHashSetWithExpectedSize(dictWfBitSets.size());
    for (BitSet dictWfBits : dictWfBitSets) {
        BitSet tokenPossibleBits = (BitSet) dictWfBits.clone();
        tokenPossibleBits.and(targetCategoriesMask);
        tokenPossibleTags.add(tokenPossibleBits);
    }
    List<String> resultList = Lists.newArrayListWithExpectedSize(tokenPossibleTags.size());
    for (BitSet tokenPossibleBits : tokenPossibleTags) {
        String featValue;
        if (tokenPossibleBits.isEmpty()) {
            featValue = "NULL";
        } else {
            featValue = gramJoiner.join(gramModel.toGramSet(tokenPossibleBits));
        }
        resultList.add("DL=" + featValue);
    }
    if (prevTag != null && !PunctuationUtils.isPunctuationTag(prevTag)) {
        // add the name of a predicate if it yields true for any pair <prevTag, dictTag>, dictTag in tokenPossibleTags
        BitSet prevTagBits = toGramBits(gramModel, tagMapper.parseTag(prevTag, focusToken.getCoveredText()));
        for (Map.Entry<String, TwoTagPredicate> predEntry : namedPredicates.entrySet()) {
            for (BitSet dictTag : tokenPossibleTags) {
                if (predEntry.getValue().apply(prevTagBits, dictTag)) {
                    resultList.add(predEntry.getKey());
                    break;
                }
            }
        }
    }
    return resultList;
}

From source file:org.eclipse.xtext.generator.trace.TraceRegionMerger.java

private void doMergeLeafRegions(int listIdx, int exclusiveEndOffset, int inclusiveEndLine,
        List<ILocationData> locations) {
    int i = listIdx;
    List<AbstractTraceRegion> newRegions = null;
    AbstractTraceRegion prev = null;// w  w w .  j av  a2s. c  om
    while (i < leafRegions.size()) {
        AbstractTraceRegion next = leafRegions.get(i);
        if (next.getMyOffset() >= exclusiveEndOffset) {
            newRegions = addPendingRegions(prev, exclusiveEndOffset, inclusiveEndLine, locations, newRegions);
            partialSortRegions(listIdx, exclusiveEndOffset, i, newRegions);
            return;
        }
        if (prev != null) {
            int prevEnd = prev.getMyOffset() + prev.getMyLength();
            if (prevEnd < next.getMyOffset()) {
                if (newRegions == null)
                    newRegions = Lists.newArrayListWithExpectedSize(4);
                newRegions.add(new TraceRegion(prevEnd, next.getMyOffset() - prevEnd, prev.getMyEndLineNumber(),
                        next.getMyLineNumber(), true, locations, null));
            }
        }
        if (next.getMyOffset() + next.getMyLength() <= exclusiveEndOffset) {
            next = replaceMerged(i, next, locations);
        } else {
            int oldLength = next.getMyLength();
            int oldEndLine = next.getMyEndLineNumber();
            next = replaceTruncated(i, next, exclusiveEndOffset - next.getMyOffset(), inclusiveEndLine);
            if (newRegions == null)
                newRegions = Lists.newArrayListWithExpectedSize(4);
            newRegions.add(
                    new TraceRegion(next.getMyOffset() + next.getMyLength(), oldLength - next.getMyLength(),
                            inclusiveEndLine, oldEndLine, true, next.getAssociatedLocations(), null));
            next = replaceMerged(i, next, locations);
        }
        i++;
        prev = next;
    }
    newRegions = addPendingRegions(prev, exclusiveEndOffset, inclusiveEndLine, locations, newRegions);
    partialSortRegions(listIdx, exclusiveEndOffset, i, newRegions);
}

From source file:org.apache.bookkeeper.statelib.impl.mvcc.MVCCUtils.java

private static List<RequestOp> toRequestOpList(List<Op<byte[], byte[]>> ops) {
    if (ops == null) {
        return Collections.emptyList();
    }//from  w  ww  .  ja  v a 2s.c  om
    List<RequestOp> requestOps = Lists.newArrayListWithExpectedSize(ops.size());
    for (Op<byte[], byte[]> op : ops) {
        switch (op.type()) {
        case PUT:
            requestOps.add(
                    RequestOp.newBuilder().setRequestPut(toPutRequest((PutOp<byte[], byte[]>) op)).build());
            break;
        case DELETE:
            requestOps.add(RequestOp.newBuilder()
                    .setRequestDeleteRange(toDeleteRequest((DeleteOp<byte[], byte[]>) op)).build());
            break;
        case RANGE:
            requestOps.add(RequestOp.newBuilder().setRequestRange(toRangeRequest((RangeOp<byte[], byte[]>) op))
                    .build());
            break;
        default:
            throw new IllegalArgumentException("Unknown request " + op.type() + " found in a txn request");
        }
    }
    return requestOps;
}

From source file:com.android.tools.lint.ExternalAnnotationRepository.java

@NonNull
public static synchronized ExternalAnnotationRepository get(@NonNull LintClient client) {
    if (sSingleton == null) {
        HashSet<AndroidLibrary> seen = Sets.newHashSet();
        Collection<Project> projects = client.getKnownProjects();
        List<File> files = Lists.newArrayListWithExpectedSize(2);
        for (Project project : projects) {
            if (project.isGradleProject()) {
                Variant variant = project.getCurrentVariant();
                AndroidProject model = project.getGradleProjectModel();
                if (model != null && variant != null) {
                    Dependencies dependencies = variant.getMainArtifact().getDependencies();
                    for (AndroidLibrary library : dependencies.getLibraries()) {
                        addLibraries(files, library, seen);
                    }//from w w w  .  j a va2s.com
                }
            }
        }

        File sdkAnnotations = client.findResource(SDK_ANNOTATIONS_PATH);
        if (sdkAnnotations == null) {
            // Until the SDK annotations are bundled in platform tools, provide
            // a fallback for Gradle builds to point to a locally installed version
            String path = System.getenv("SDK_ANNOTATIONS");
            if (path != null) {
                sdkAnnotations = new File(path);
                if (!sdkAnnotations.exists()) {
                    sdkAnnotations = null;
                }
            }
        }
        if (sdkAnnotations != null) {
            files.add(sdkAnnotations);
        }

        sSingleton = create(client, files);
    }

    return sSingleton;
}

From source file:org.apache.phoenix.compile.ScanRanges.java

public static ScanRanges create(RowKeySchema schema, List<List<KeyRange>> ranges, int[] slotSpan,
        KeyRange minMaxRange, Integer nBuckets, boolean useSkipScan, int rowTimestampColIndex) {
    int offset = nBuckets == null ? 0 : SaltingUtil.NUM_SALTING_BYTES;
    int nSlots = ranges.size();
    if (nSlots == offset && minMaxRange == KeyRange.EVERYTHING_RANGE) {
        return EVERYTHING;
    } else if (minMaxRange == KeyRange.EMPTY_RANGE || (nSlots == 1 + offset && ranges.get(offset).size() == 1
            && ranges.get(offset).get(0) == KeyRange.EMPTY_RANGE)) {
        return NOTHING;
    }/*from   ww  w .j a  va2 s . c o m*/
    TimeRange rowTimestampRange = getRowTimestampColumnRange(ranges, schema, rowTimestampColIndex);
    boolean isPointLookup = isPointLookup(schema, ranges, slotSpan, useSkipScan);
    if (isPointLookup) {
        // TODO: consider keeping original to use for serialization as it would be smaller?
        List<byte[]> keys = ScanRanges.getPointKeys(ranges, slotSpan, schema, nBuckets);
        List<KeyRange> keyRanges = Lists.newArrayListWithExpectedSize(keys.size());
        KeyRange unsaltedMinMaxRange = minMaxRange;
        if (nBuckets != null && minMaxRange != KeyRange.EVERYTHING_RANGE) {
            unsaltedMinMaxRange = KeyRange.getKeyRange(stripPrefix(minMaxRange.getLowerRange(), offset),
                    minMaxRange.lowerUnbound(), stripPrefix(minMaxRange.getUpperRange(), offset),
                    minMaxRange.upperUnbound());
        }
        // We have full keys here, so use field from our varbinary schema
        BytesComparator comparator = ScanUtil.getComparator(SchemaUtil.VAR_BINARY_SCHEMA.getField(0));
        for (byte[] key : keys) {
            // Filter now based on unsalted minMaxRange and ignore the point key salt byte
            if (unsaltedMinMaxRange.compareLowerToUpperBound(key, offset, key.length - offset, true,
                    comparator) <= 0
                    && unsaltedMinMaxRange.compareUpperToLowerBound(key, offset, key.length - offset, true,
                            comparator) >= 0) {
                keyRanges.add(KeyRange.getKeyRange(key));
            }
        }
        ranges = Collections.singletonList(keyRanges);
        useSkipScan = keyRanges.size() > 1;
        // Treat as binary if descending because we've got a separator byte at the end
        // which is not part of the value.
        if (keys.size() > 1 || SchemaUtil.getSeparatorByte(schema.rowKeyOrderOptimizable(), false,
                schema.getField(0)) == QueryConstants.DESC_SEPARATOR_BYTE) {
            schema = SchemaUtil.VAR_BINARY_SCHEMA;
            slotSpan = ScanUtil.SINGLE_COLUMN_SLOT_SPAN;
        } else {
            // Keep original schema and don't use skip scan as it's not necessary
            // when there's a single key.
            slotSpan = new int[] { schema.getMaxFields() - 1 };
        }
    }
    List<List<KeyRange>> sortedRanges = Lists.newArrayListWithExpectedSize(ranges.size());
    for (int i = 0; i < ranges.size(); i++) {
        List<KeyRange> sorted = Lists.newArrayList(ranges.get(i));
        Collections.sort(sorted, KeyRange.COMPARATOR);
        sortedRanges.add(ImmutableList.copyOf(sorted));
    }

    // Don't set minMaxRange for point lookup because it causes issues during intersect
    // by going across region boundaries
    KeyRange scanRange = KeyRange.EVERYTHING_RANGE;
    // if (!isPointLookup && (nBuckets == null || !useSkipScanFilter)) {
    // if (! ( isPointLookup || (nBuckets != null && useSkipScanFilter) ) ) {
    // if (nBuckets == null || (nBuckets != null && (!isPointLookup || !useSkipScanFilter))) {
    if (nBuckets == null || !isPointLookup || !useSkipScan) {
        byte[] minKey = ScanUtil.getMinKey(schema, sortedRanges, slotSpan);
        byte[] maxKey = ScanUtil.getMaxKey(schema, sortedRanges, slotSpan);
        // If the maxKey has crossed the salt byte boundary, then we do not
        // have anything to filter at the upper end of the range
        if (ScanUtil.crossesPrefixBoundary(maxKey, ScanUtil.getPrefix(minKey, offset), offset)) {
            maxKey = KeyRange.UNBOUND;
        }
        // We won't filter anything at the low end of the range if we just have the salt byte
        if (minKey.length <= offset) {
            minKey = KeyRange.UNBOUND;
        }
        scanRange = KeyRange.getKeyRange(minKey, maxKey);
    }
    if (minMaxRange != KeyRange.EVERYTHING_RANGE) {
        minMaxRange = ScanUtil.convertToInclusiveExclusiveRange(minMaxRange, schema,
                new ImmutableBytesWritable());
        scanRange = scanRange.intersect(minMaxRange);
    }

    if (scanRange == KeyRange.EMPTY_RANGE) {
        return NOTHING;
    }
    return new ScanRanges(schema, slotSpan, sortedRanges, scanRange, minMaxRange, useSkipScan, isPointLookup,
            nBuckets, rowTimestampRange);
}