Example usage for com.google.common.base Functions forMap

List of usage examples for com.google.common.base Functions forMap

Introduction

In this page you can find the example usage for com.google.common.base Functions forMap.

Prototype

public static <K, V> Function<K, V> forMap(Map<K, ? extends V> map, @Nullable V defaultValue) 

Source Link

Document

Returns a function which performs a map lookup with a default value.

Usage

From source file:com.github.mike10004.demo.infinitescroll.ItemServlet.java

static Function<String, String> newParamMapFunction(HttpServletRequest request) {
    return Functions.compose(new Function<String[], String>() {
        @Override//from  w w  w .j  a va 2s .c  o  m
        public String apply(String[] input) {
            return input != null && input.length > 0 ? input[0] : null;
        }
    }, Functions.forMap(request.getParameterMap(), null));
}

From source file:de.comci.bitmap.SortDirection.java

<K, V> Comparator order(Map<K, ? extends V> map) {
    return order.onResultOf(Functions.forMap(map, null));
}

From source file:org.jclouds.openstack.nova.domain.Resource.java

public URI getURI() {
    for (Map<String, String> linkProperties : links) {
        try {/* w  w  w  .  j  a v a  2s . co  m*/
            if (!Functions.forMap(linkProperties, "").apply("rel").equals("bookmark"))
                continue;
            if (!Functions.forMap(linkProperties, "").apply("type").contains("json"))
                continue;

            return new URI(linkProperties.get("href"));
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    throw new IllegalStateException("URI is not available");
}

From source file:org.jdag.example.wc.CountWords.java

/**
 * {@inheritDoc}//  www . java 2  s  .  c o m
 */
@Override
public void process(Input<String> input, Output<Map<String, Integer>> output) {
    Map<String, Integer> wordcountMap = new HashMap<String, Integer>();
    com.google.common.base.Function<String, Integer> lookupFunction = Functions.forMap(wordcountMap, 0);
    for (String line : new IteratorWrapper<String>(input.getIterator())) {
        String[] words = line.split(" ");
        for (String word : words) {
            int count = lookupFunction.apply(word);
            wordcountMap.put(word, ++count);
        }
    }
    output.write(wordcountMap);
    output.done();
}

From source file:org.jdag.example.wc.WordCountMerger.java

/**
 * {@inheritDoc}//from   w w w.j  a  v  a 2 s.c o  m
 */
@Override
public void merge(List<Input<Map<String, Integer>>> shards, Output<Map<String, Integer>> output) {
    Map<String, Integer> resultMap = new HashMap<String, Integer>();
    com.google.common.base.Function<String, Integer> lookupFunction = Functions.forMap(resultMap, 0);

    for (Input<Map<String, Integer>> shard : shards) {
        Map<String, Integer> splitWcMap = shard.getIterator().next();
        for (Map.Entry<String, Integer> entry : splitWcMap.entrySet()) {
            resultMap.put(entry.getKey(), lookupFunction.apply(entry.getKey()) + entry.getValue());
        }
    }
    output.write(resultMap);
    output.done();
}

From source file:com.twitter.common.net.UrlResolverUtil.java

UrlResolverUtil(Map<String, String> hostToUserAgent) {
    this(Functions.compose(Functions.forMap(checkNotBlank(hostToUserAgent), DEFAULT_USER_AGENT),
            new Function<URL, String>() {
                @Override/*from   w w w . j a  v a2 s.co m*/
                public String apply(URL url) {
                    return url.getHost();
                }
            }));
}

From source file:com.github.jonross.seq4j.SeqMisc.java

/**
 * Wraps {@link Functions#forMap(Map, Object)}; returns a function that looks up its
 * argument in a map, using the given default value if the map does not contain that key.
 *///from w w  w.j ava 2  s  .  com

public static <K, V> Function<K, V> lookup(Map<K, ? extends V> map, V defaultValue) {
    return Functions.forMap(map, defaultValue);
}

From source file:org.sonar.plugins.pmd.PmdTemplate.java

private static String normalize(String version) {
    return Functions.forMap(JAVA_VERSIONS, version).apply(version);
}

From source file:eu.thebluemountain.customers.dctm.brownbag.badcontentslister.db.DCReader.java

/**
 * The method returns the function which, given a format's name,
 * returns the format's extension.//  w w  w  .j  a v  a  2s . c o  m
 * @param jdbc provides access to the source
 * @return the function which, given a format name, returns the matching
 * extension if any.
 */
private static Function<String, String> makeExtensions(JDBCConnection jdbc) {
    String sql = "SELECT name, dos_extension " + "FROM dm_format_s WHERE "
            + "(dos_extension IS NOT NULL AND dos_extension <> ' ')";
    try {
        try (Statement stmt = jdbc.connection.createStatement(ResultSet.TYPE_FORWARD_ONLY,
                ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(sql)) {
            ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
            while (rs.next()) {
                String name = rs.getString(1);
                String ext = "." + rs.getString(2);
                builder.put(name, ext);
            }
            return Functions.forMap(builder.build(), null);
        }
    } catch (SQLException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.apache.tez.analyzer.plugins.VertexLevelCriticalPathAnalyzer.java

private static Map<String, Long> sortByValues(Map<String, Long> result) {
    //Sort result by time in reverse order
    final Ordering<String> reversValueOrdering = Ordering.natural().reverse().nullsLast()
            .onResultOf(Functions.forMap(result, null));
    Map<String, Long> orderedMap = ImmutableSortedMap.copyOf(result, reversValueOrdering);
    return orderedMap;
}