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

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

Introduction

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

Prototype

public static <K, V> HashMap<K, V> newHashMap() 

Source Link

Document

Creates a mutable, empty HashMap instance.

Usage

From source file:com.zaradai.kunzite.optimizer.data.matrix.SparseMatrix.java

protected Map<Pair, T> createMap() {
    return Maps.newHashMap();
}

From source file:org.apache.shindig.gadgets.rewrite.image.ImageUtils.java

/**
 * Convert an image to a palletized one. Will not create a palette above a fixed
 * number of entries/*from   w  w w . j  a  va 2 s.  c om*/
 */
public static BufferedImage palettize(BufferedImage img, int maxEntries) {
    // Just because an image has a palette doesnt mean it has a good one
    // so we re-index even if its an IndexColorModel
    int addedCount = 0;
    Map<Integer, Integer> added = Maps.newHashMap();
    for (int y = 0; y < img.getHeight(); y++) {
        for (int x = 0; x < img.getWidth(); x++) {
            if (!added.containsKey(img.getRGB(x, y))) {
                added.put(img.getRGB(x, y), addedCount++);
            }
            if (added.size() > maxEntries) {
                // Bail if palette becomes too large
                return null;
            }
        }
    }
    int[] cmap = new int[added.size()];
    for (int c : added.keySet()) {
        cmap[added.get(c)] = c;
    }

    int bitCount = 1;
    while (added.size() >> bitCount != 0) {
        bitCount *= 2;
    }

    IndexColorModel icm = new IndexColorModel(bitCount, added.size(), cmap, 0, DataBuffer.TYPE_BYTE, null);

    // Check if generated palette matched original
    if (img.getColorModel() instanceof IndexColorModel) {
        IndexColorModel originalModel = (IndexColorModel) img.getColorModel();
        if (originalModel.getPixelSize() == icm.getPixelSize()
                && originalModel.getMapSize() == icm.getMapSize()) {
            // Old model already had efficient palette
            return null;
        }
    }

    // Be careful to assign correctly assign byte packing method based on pixel size
    BufferedImage dst = new BufferedImage(img.getWidth(), img.getHeight(),
            icm.getPixelSize() < 8 ? BufferedImage.TYPE_BYTE_BINARY : BufferedImage.TYPE_BYTE_INDEXED, icm);

    WritableRaster wr = dst.getRaster();
    for (int y = 0; y < dst.getHeight(); y++) {
        for (int x = 0; x < dst.getWidth(); x++) {
            wr.setSample(x, y, 0, added.get(img.getRGB(x, y)));
        }
    }
    return dst;
}

From source file:org.atlasapi.output.rdf.BeanIntrospector.java

public static Map<String, PropertyDescriptor> getRelations(Class<?> beanType) throws IntrospectionException {
    Map<String, PropertyDescriptor> result = Maps.newHashMap();
    Map<String, PropertyDescriptor> properties = BeanIntrospector.getPropertyDescriptors(beanType);

    for (Map.Entry<String, PropertyDescriptor> propertyEntry : properties.entrySet()) {
        Class<?> propertyType = propertyEntry.getValue().getPropertyType();

        if (Collection.class.isAssignableFrom(propertyType)) {
            Method readMethod = propertyEntry.getValue().getReadMethod();
            propertyType = GenericCollectionTypeResolver.getCollectionReturnType(readMethod);
        }/* www .  j  a v  a 2 s  . c o  m*/

        if (propertyType != null && isEntity(propertyType)) {
            result.put(propertyEntry.getKey(), propertyEntry.getValue());
        }
    }

    return result;
}

From source file:org.apache.blur.slur.BlurQueryHelper.java

private static void maybeAddSelector(BlurQuery blurQuery, SolrParams p) {
    String fieldString = p.get(CommonParams.FL);
    Selector selector = new Selector();
    selector.setRecordOnly(true);/*w ww.j  a  v  a2  s  . c o m*/

    if (fieldString != null) {
        Map<String, Set<String>> famCols = Maps.newHashMap();
        String[] fields = fieldString.split(",");

        for (String field : fields) {
            String[] famCol = field.split("\\.");

            if (famCol.length != 2) {
                throw new IllegalArgumentException("Fields must be in a family.column format[" + field + "]");
            }
            if (!famCols.containsKey(famCol[0])) {
                famCols.put(famCol[0], new HashSet<String>());
            }
            Set<String> cols = famCols.get(famCol[0]);
            cols.add(famCol[1]);
        }
        selector.setColumnsToFetch(famCols);

    }
    blurQuery.setSelector(selector);
}

From source file:org.glowroot.common.util.SystemProperties.java

public static Map<String, String> maskSystemProperties(Map<String, String> systemProperties,
        List<String> maskSystemProperties) {
    if (maskSystemProperties.isEmpty()) {
        return systemProperties;
    }/*  www. j  a  v a  2 s  .  c  o  m*/
    List<Pattern> maskPatterns = buildPatternList(maskSystemProperties);
    Map<String, String> maskedSystemProperties = Maps.newHashMap();
    for (Map.Entry<String, String> entry : systemProperties.entrySet()) {
        String name = entry.getKey();
        if (matchesAny(name, maskPatterns)) {
            maskedSystemProperties.put(name, "****");
        } else {
            maskedSystemProperties.put(name, entry.getValue());
        }
    }
    return maskedSystemProperties;
}

From source file:net.diogobohm.timed.api.db.serializer.DBTaskTagSerializer.java

@Override
public Map<String, Object> serialize(DBTaskTag object) {
    Map<String, Object> valueMap = Maps.newHashMap();

    valueMap.put("task_id", object.getTaskId());
    valueMap.put("tag_id", object.getTagId());

    return valueMap;
}

From source file:com.google.testing.junit.runner.junit4.JUnit4Options.java

/**
 * Parses the given array of arguments and returns a JUnit4Options
 * object representing the parsed arguments.
 *//*from  w w  w  .  j  a  v  a 2s.  c om*/
static JUnit4Options parse(Map<String, String> envVars, List<String> args) {
    ImmutableList.Builder<String> unparsedArgsBuilder = ImmutableList.builder();
    Map<String, String> optionsMap = Maps.newHashMap();

    optionsMap.put(TEST_INCLUDE_FILTER_OPTION, null);
    optionsMap.put(TEST_EXCLUDE_FILTER_OPTION, null);

    for (Iterator<String> it = args.iterator(); it.hasNext();) {
        String arg = it.next();
        int indexOfEquals = arg.indexOf("=");

        if (indexOfEquals > 0) {
            String optionName = arg.substring(0, indexOfEquals);
            if (optionsMap.containsKey(optionName)) {
                optionsMap.put(optionName, arg.substring(indexOfEquals + 1));
                continue;
            }
        } else if (optionsMap.containsKey(arg)) {
            // next argument is the regexp
            if (!it.hasNext()) {
                throw new RuntimeException("No filter expression specified after " + arg);
            }
            optionsMap.put(arg, it.next());
            continue;
        }
        unparsedArgsBuilder.add(arg);
    }
    // If TESTBRIDGE_TEST_ONLY is set in the environment, forward it to the
    // --test_filter flag.
    String testFilter = envVars.get(TESTBRIDGE_TEST_ONLY);
    if (testFilter != null && optionsMap.get(TEST_INCLUDE_FILTER_OPTION) == null) {
        optionsMap.put(TEST_INCLUDE_FILTER_OPTION, testFilter);
    }

    ImmutableList<String> unparsedArgs = unparsedArgsBuilder.build();
    return new JUnit4Options(optionsMap.get(TEST_INCLUDE_FILTER_OPTION),
            optionsMap.get(TEST_EXCLUDE_FILTER_OPTION), unparsedArgs.toArray(new String[unparsedArgs.size()]));
}

From source file:qa.qcri.qnoise.model.HistogramModel.java

/**
 * {@inheritDoc}/*from   w w w.java  2  s .  c om*/
 */
@Override
public int nextIndex(int start, int end) {
    HashMap<String, Integer> count = Maps.newHashMap();
    HashMap<String, List<Integer>> index = Maps.newHashMap();
    int length = end - start;
    for (int i = start; i < end; i++) {
        String data = profile.getCell(i, columnIndex);
        if (count.containsKey(data)) {
            Integer v = count.get(data);
            v++;
            count.put(data, v);
            List<Integer> list = index.get(data);
            list.add(i);
        } else {
            count.put(data, 1);
            List<Integer> list = Lists.newArrayList();
            list.add(i);
            index.put(data, list);
        }
    }

    HashMap<String, Double> hist = Maps.newHashMap();
    // normalize the ratio
    double pre = 0.0;
    for (String key : count.keySet()) {
        Integer v = count.get(key);
        double ratio = (double) v / (double) length + pre;
        hist.put(key, ratio);
        pre = ratio;
    }

    double random = Math.random();
    String selectedBin = null;
    for (String key : hist.keySet()) {
        double bound = hist.get(key);
        if (random < bound) {
            selectedBin = key;
            break;
        }
    }

    List<Integer> binIndex = index.get(selectedBin);

    return binIndex.get(0);
}

From source file:org.apache.flume.event.EventBuilder.java

public static Event withBody(long body) {
    Map<String, String> headers = Maps.newHashMap();
    headers.put(Event.bodyType, "long");
    return withBody(Longs.toByteArray(body), headers);
}

From source file:controllers.AdminDashboard.java

@Restrictions({ @Restrict("SYSTEM_MONITOR"), @Restrict("SYSTEM_ADMIN"), @Restrict("RESTRICTED_SYSTEM_ADMIN") })
public static void health() {
    Map<String, Promise<?>> promises = Maps.newHashMap();
    promises.put("nodeHealthList", AdminDashboardUtils.nodeHealthList());
    promises.put("clusterInfo", AdminDashboardUtils.clusterInfo());
    trySetRenderArgs(promises);//from   ww w. ja  va2  s  .  c om
    // Add lastUpdated render args after promises are redeemed
    renderArgs.put("nodeHealthListLastUpdated", AdminDashboardUtils.getNodeHealthListLastUpdated());
    render();
}