Example usage for com.google.common.collect Maps newHashMapWithExpectedSize

List of usage examples for com.google.common.collect Maps newHashMapWithExpectedSize

Introduction

In this page you can find the example usage for com.google.common.collect Maps newHashMapWithExpectedSize.

Prototype

public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) 

Source Link

Document

Creates a HashMap instance, with a high enough "initial capacity" that it should hold expectedSize elements without growth.

Usage

From source file:com.opengamma.language.convert.MapConverter.java

@Override
public void convertValue(final ValueConversionContext conversionContext, final Object value,
        final JavaTypeInfo<?> type) {
    if ((value == null) && type.isAllowNull()) {
        conversionContext.setResult(null);
        return;//w w w .j  a  v  a 2 s  .  c  om
    }
    if (type.getRawClass() == Map.class) {
        // Converting from Values[][] to Map
        final Value[][] values = (Value[][]) value;
        final JavaTypeInfo<?> keyType = type.getParameterizedType(0);
        final JavaTypeInfo<?> valueType = type.getParameterizedType(1);
        final Map<Object, Object> result = Maps.newHashMapWithExpectedSize(values.length);
        for (Value[] entry : values) {
            if (entry.length != 2) {
                conversionContext.setFail();
                return;
            }
            conversionContext.convertValue(entry[0], keyType);
            if (conversionContext.isFailed()) {
                conversionContext.setFail();
                return;
            }
            final Object key = conversionContext.getResult();
            conversionContext.convertValue(entry[1], valueType);
            if (conversionContext.isFailed()) {
                conversionContext.setFail();
                return;
            }
            result.put(key, conversionContext.getResult());
        }
        conversionContext.setResult(result);
    } else {
        // Converting from Map to Values[][]
        final Map<?, ?> map = (Map<?, ?>) value;
        final Value[][] result = new Value[map.size()][2];
        int i = 0;
        for (Map.Entry<?, ?> entry : map.entrySet()) {
            if (entry.getKey() == null) {
                result[i][0] = new Value();
            } else {
                conversionContext.convertValue(entry.getKey(), VALUE);
                if (conversionContext.isFailed()) {
                    conversionContext.setFail();
                    return;
                }
                result[i][0] = conversionContext.getResult();
            }
            if (entry.getValue() == null) {
                result[i++][1] = new Value();
            } else {
                conversionContext.convertValue(entry.getValue(), VALUE);
                if (conversionContext.isFailed()) {
                    conversionContext.setFail();
                    return;
                }
                result[i++][1] = conversionContext.getResult();
            }
        }
        // Don't be tempted to sort the results; the map instance may already have done that
        conversionContext.setResult(result);
    }
}

From source file:io.druid.query.topn.TopNLexicographicResultBuilder.java

@Override
public TopNResultBuilder addEntry(String dimName, Object dimValIndex, Object[] metricVals) {
    final Map<String, Object> metricValues = Maps.newHashMapWithExpectedSize(metricVals.length + 1);

    if (shouldAdd(dimName)) {
        metricValues.put(dimSpec.getOutputName(), dimName);
        final int extra = metricVals.length % LOOP_UNROLL_COUNT;
        switch (extra) {
        case 7://w  w  w  . j  a va2s. c  om
            metricValues.put(aggFactoryNames[6], metricVals[6]);
        case 6:
            metricValues.put(aggFactoryNames[5], metricVals[5]);
        case 5:
            metricValues.put(aggFactoryNames[4], metricVals[4]);
        case 4:
            metricValues.put(aggFactoryNames[3], metricVals[3]);
        case 3:
            metricValues.put(aggFactoryNames[2], metricVals[2]);
        case 2:
            metricValues.put(aggFactoryNames[1], metricVals[1]);
        case 1:
            metricValues.put(aggFactoryNames[0], metricVals[0]);
        }
        for (int i = extra; i < metricVals.length; i += LOOP_UNROLL_COUNT) {
            metricValues.put(aggFactoryNames[i + 0], metricVals[i + 0]);
            metricValues.put(aggFactoryNames[i + 1], metricVals[i + 1]);
            metricValues.put(aggFactoryNames[i + 2], metricVals[i + 2]);
            metricValues.put(aggFactoryNames[i + 3], metricVals[i + 3]);
            metricValues.put(aggFactoryNames[i + 4], metricVals[i + 4]);
            metricValues.put(aggFactoryNames[i + 5], metricVals[i + 5]);
            metricValues.put(aggFactoryNames[i + 6], metricVals[i + 6]);
            metricValues.put(aggFactoryNames[i + 7], metricVals[i + 7]);
        }

        pQueue.add(new DimValHolder.Builder().withDimName(dimName).withMetricValues(metricValues).build());
        if (pQueue.size() > threshold) {
            pQueue.poll();
        }
    }

    return this;
}

From source file:org.dishevelled.venn.layout.VennLayoutImpl.java

/**
 * Create a new venn layout with the specified list of shapes and bounding rectangle.
 *
 * @param shapes list of shapes, must not be null
 * @param boundingRectangle bounding rectangle, must not be null
 *///from  w ww  .ja  v a2 s  .c  o m
public VennLayoutImpl(final List<? extends Shape> shapes, final Rectangle2D boundingRectangle) {
    if (shapes == null) {
        throw new IllegalArgumentException("shapes must not be null");
    }
    if (boundingRectangle == null) {
        throw new IllegalArgumentException("boundingRectangle must not be null");
    }
    this.shapes = ImmutableList.copyOf(shapes);
    this.boundingRectangle = boundingRectangle;

    luneCenters = Maps.newHashMapWithExpectedSize((int) Math.pow(2, this.shapes.size()) - 1);
}

From source file:org.auraframework.impl.adapter.BrowserValueProvider.java

protected Map<String, Object> parse() {
    AuraContext context = Aura.getContextService().getCurrentContext();
    Map<String, Object> m = Maps.newHashMapWithExpectedSize(32);
    String ua = context != null ? context.getClient().getUserAgent() : null;
    BrowserInfo b = new BrowserInfo(ua);
    m.put(IS_TABLET, b.isTablet());//from w  w  w.  ja  v  a  2s  .com
    m.put(IS_PHONE, b.isPhone());
    m.put(IS_ANDROID, b.isAndroid());
    m.put(FORM_FACTOR, b.getFormFactor());
    m.put(IS_IPHONE, b.isIPhone());
    m.put(IS_IPAD, b.isIPad());
    m.put(IS_IOS, b.isIOS());
    m.put(IS_IE6, b.isIE6());
    m.put(IS_IE7, b.isIE7());
    m.put(IS_IE8, b.isIE8());
    m.put(IS_IE9, b.isIE9());
    m.put(IS_IE10, b.isIE10());
    m.put(IS_IE11, b.isIE11());
    m.put(IS_WEBKIT, b.isWebkit());
    m.put(IS_FIREFOX, b.isFirefox());
    m.put(IS_WINDOWS_PHONE, b.isWindowsPhone());
    m.put(IS_WINDOWS_TABLET, b.isWindowsTablet());

    return m;
}

From source file:com.mengge.internal.MobileElementToJsonConverter.java

/**
 * Converts {@link RemoteWebElement} objects, which may be
 * {@link WrapsElement wrapped}, into their JSON representation as defined by
 * the WebDriver wire protocol.//  w  ww .  j  a v a2 s. c  o  m
 *
 * @param arg is the argument
 * @return the result
 */
public Object apply(Object arg) {
    if (arg == null || arg instanceof String || arg instanceof Boolean || arg instanceof Number) {
        return arg;
    }

    while (arg instanceof WrapsElement) {
        arg = ((WrapsElement) arg).getWrappedElement();
    }

    if (arg instanceof MobileElement) {
        return ImmutableMap.of("ELEMENT", ((MobileElement) arg).getId());
    }

    if (arg.getClass().isArray()) {
        arg = Lists.newArrayList((Object[]) arg);
    }

    if (arg instanceof Collection<?>) {
        Collection<?> args = (Collection<?>) arg;
        return Collections2.transform(args, this);
    }

    if (arg instanceof Map<?, ?>) {
        Map<?, ?> args = (Map<?, ?>) arg;
        Map<String, Object> converted = Maps.newHashMapWithExpectedSize(args.size());
        for (Map.Entry<?, ?> entry : args.entrySet()) {
            Object key = entry.getKey();
            if (!(key instanceof String)) {
                throw new IllegalArgumentException(
                        "All keys in Map script arguments must be strings: " + key.getClass().getName());
            }
            converted.put((String) key, apply(entry.getValue()));
        }
        return converted;
    }

    throw new IllegalArgumentException("Argument is of an illegal type: " + arg.getClass().getName());
}

From source file:org.opendaylight.controller.config.facade.xml.rpc.RuntimeRpcElementResolved.java

public ObjectName getObjectName(ModuleRpcs rpcMapping) {
    Map<String, String> additionalAttributesJavaNames = Maps
            .newHashMapWithExpectedSize(additionalAttributes.size());
    for (String attributeYangName : additionalAttributes.keySet()) {
        String attributeJavaName = rpcMapping.getRbeJavaName(attributeYangName);
        Preconditions.checkState(attributeJavaName != null,
                "Cannot find java name for runtime bean wtih yang name %s", attributeYangName);
        additionalAttributesJavaNames.put(attributeJavaName, additionalAttributes.get(attributeYangName));
    }/*from  w  w w .j  av  a 2s  .  c om*/
    return ObjectNameUtil.createRuntimeBeanName(moduleName, instanceName, additionalAttributesJavaNames);
}

From source file:com.newisys.apps.pktviz.model.xform.AdjSeqTimeTransform.java

public void reset(Iterator<PacketInfo> packetIterator) {
    final SortedMap<Long, AnalysisTickInfo> analysisMap = Maps.newTreeMap();
    while (packetIterator.hasNext()) {
        final PacketInfo packet = packetIterator.next();
        final long fromTime = packet.getFromTimeActual();
        final AnalysisTickInfo fromInfo = getAnalysisTickInfo(analysisMap, fromTime);
        final int fromAlloc = mapIncrement(fromInfo.nodeAllocMap, packet.getFromNode(), 1);
        ++fromInfo.fromPackets;/*from   w  w  w. ja  va 2  s  . co  m*/

        final long toTime = packet.getToTimeActual();
        final AnalysisTickInfo toInfo = getAnalysisTickInfo(analysisMap, toTime);
        mapIncrement(toInfo.nodeAllocMap, packet.getToNode(), fromTime == toTime ? fromAlloc : 1);
        ++toInfo.toPackets;
    }

    tickInfoMap = Maps.newHashMapWithExpectedSize(analysisMap.size());
    int spanCount = 0;
    long seqTime = 0;
    for (final Map.Entry<Long, AnalysisTickInfo> entry : analysisMap.entrySet()) {
        final Long realTime = entry.getKey();
        final AnalysisTickInfo info = entry.getValue();
        tickInfoMap.put(realTime, new FinalTickInfo(seqTime, info.nodeAllocMap.size()));
        spanCount += info.fromPackets - info.toPackets;
        seqTime += mapMax(info.nodeAllocMap, 1) + 1;
    }
}

From source file:com.palantir.atlasdb.keyvalue.jdbc.impl.MultiTimestampPutBatch.java

@Override
@Nullable//from www.  j  a  v  a  2 s  .c om
public PutBatch getNextBatch(Result<? extends Record> existingRecords) {
    Map<CellTimestamp, byte[]> existing = Maps.newHashMapWithExpectedSize(existingRecords.size());
    for (Record record : existingRecords) {
        existing.put(
                new CellTimestamp(record.getValue(JdbcConstants.A_ROW_NAME),
                        record.getValue(JdbcConstants.A_COL_NAME), record.getValue(JdbcConstants.A_TIMESTAMP)),
                record.getValue(JdbcConstants.A_VALUE));
    }
    Multimap<Cell, Value> nextBatch = ArrayListMultimap.create();
    for (Entry<Cell, Value> entry : data.entries()) {
        Cell cell = entry.getKey();
        Value newValue = entry.getValue();
        byte[] oldValue = existing
                .get(new CellTimestamp(cell.getRowName(), cell.getColumnName(), newValue.getTimestamp()));
        if (oldValue == null) {
            nextBatch.put(cell, newValue);
        } else if (!Arrays.equals(oldValue, newValue.getContents())) {
            return null;
        }
    }
    return new MultiTimestampPutBatch(nextBatch);
}

From source file:net.derquinse.bocas.BucketGuavaCachingBocas.java

Map<BucketKey, MemoryByteSource> toInternalEntryMap(Map<ByteString, MemoryByteSource> entries) {
    final Map<BucketKey, MemoryByteSource> map = Maps.newHashMapWithExpectedSize(entries.size());
    for (Entry<ByteString, MemoryByteSource> entry : entries.entrySet()) {
        map.put(new BucketKey(this, entry.getKey()), entry.getValue());
    }//from  w  ww  .j a v a 2  s  .co m
    return map;
}

From source file:org.opendaylight.mdsal.binding.dom.adapter.BindingDOMDataTreeListenerAdapter.java

private Map<DataTreeIdentifier<?>, DataObject> toBinding(
        final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> domSubtrees) {
    // FIXME: Introduce lazy translating map
    final Map<DataTreeIdentifier<?>, DataObject> ret = Maps.newHashMapWithExpectedSize(domSubtrees.size());
    for (final Entry<DOMDataTreeIdentifier, NormalizedNode<?, ?>> domEntry : domSubtrees.entrySet()) {
        final Entry<InstanceIdentifier<?>, DataObject> bindingEntry = codec
                .fromNormalizedNode(domEntry.getKey().getRootIdentifier(), domEntry.getValue());
        ret.put(DataTreeIdentifier.create(store, bindingEntry.getKey()), bindingEntry.getValue());
    }//from w w  w.ja va  2s.  c o  m
    return ret;
}