List of usage examples for com.google.common.base Functions forMap
public static <K, V> Function<K, V> forMap(Map<K, V> map)
From source file:RandomWalk.java
public static void main(String[] args) { int numNodes = 3; if (args.length > 0) numNodes = Integer.parseInt(args[0]); System.out.printf("Generating a random graph with %d nodes...\n", numNodes); DirectedGraph graph = TestGraphs.generateRandomGraph(numNodes, Math.min(10, numNodes)); System.out.printf("Generated a random directed graph with %s nodes and %s edges.\n", graph.nodeCount(), graph.edgeCount());/* ww w. ja v a2s . com*/ // Generate walk parameters long numSteps = 1000 * 1000; final scala.Option<Integer> wpNone = scala.Option.apply(null); final scala.Option<Integer> wpTwo = scala.Option.apply(2); RandomWalkParams walkParams = new RandomWalkParams(numSteps, 0.1, wpNone, wpTwo, wpNone, false, GraphDir.OutDir(), false, false); GraphUtils graphUtils = new GraphUtils(graph); // Do the walk and measure how long it took System.out.printf("Now doing a random walk of %s steps from Node 0...\n", numSteps); long startTime = System.nanoTime(); Tuple2<Int2IntMap, scala.Option<Int2ObjectMap<Object2IntMap<DirectedPath>>>> lm = graphUtils .calculatePersonalizedReputation(0, walkParams); long endTime = System.nanoTime(); Int2IntMap neighbors = lm._1; System.out.printf("Random walk visited %s nodes in %s ms:\n", neighbors.size(), (endTime - startTime) / 1000000); // Sort neighbors (or nodes) in descending number of visits and take the top 10 neighbors List<Integer> topNeighbors = Ordering.natural().onResultOf(Functions.forMap(neighbors)).reverse() .immutableSortedCopy(neighbors.keySet()); if (topNeighbors.size() > 10) topNeighbors = topNeighbors.subList(0, 10); // Print the top 10 neighbors (and paths) System.out.printf("%8s%10s\t%s\n", "NodeID", "#Visits", "Top 2 Paths with counts"); for (int id : topNeighbors) { int numVisits = neighbors.get(id); System.out.printf("%8s%10s\t", id, numVisits); if (lm._2.isDefined()) { // If Option is not None Object2IntMap<DirectedPath> paths = lm._2.get().get(id); int remaining = paths.size(); for (Map.Entry<DirectedPath, Integer> ef : paths.entrySet()) { // Print a directed path and #visits along that path int[] nodes = ef.getKey().nodes(); for (int i = 0; i < nodes.length; i++) { if (i != 0) System.out.printf("->%d", nodes[i]); else System.out.printf("%d", nodes[i]); } System.out.printf(" (%d)", ef.getValue()); if (remaining > 1) System.out.printf(" | "); remaining--; } } System.out.println(); } }
From source file:RandomWalkJava.java
public static void main(String[] args) { int numNodes = 3; if (args.length > 0) numNodes = Integer.parseInt(args[0]); System.out.printf("Generating a random graph with %d nodes...\n", numNodes); DirectedGraph graph = TestGraphs.generateRandomGraph(numNodes, TestGraphs.getProbEdgeRandomDirected(numNodes, Math.min(10, numNodes)), StoredGraphDir.BothInOut()); System.out.printf("Generated a random directed graph with %s nodes and %s edges.\n", graph.nodeCount(), graph.edgeCount());// www . j av a 2s . c om // Generate walk parameters long numSteps = 1000 * 1000; final scala.Option<Object> wpNone = scala.Option.apply(null); final scala.Option<Object> wpTwo = scala.Option.apply((Object) 2); RandomWalkParams walkParams = new RandomWalkParams(numSteps, 0.1, wpNone, wpTwo, wpNone, false, GraphDir.OutDir(), false, false); GraphUtils graphUtils = new GraphUtils(graph); // Do the walk and measure how long it took System.out.printf("Now doing a random walk of %s steps from Node 0...\n", numSteps); long startTime = System.nanoTime(); Tuple2<Int2IntMap, scala.Option<Int2ObjectMap<Object2IntMap<DirectedPath>>>> lm = graphUtils .calculatePersonalizedReputation(0, walkParams); long endTime = System.nanoTime(); Int2IntMap neighbors = lm._1; System.out.printf("Random walk visited %s nodes in %s ms:\n", neighbors.size(), (endTime - startTime) / 1000000); // Sort neighbors (or nodes) in descending number of visits and take the top 10 neighbors List<Integer> topNeighbors = Ordering.natural().onResultOf(Functions.forMap(neighbors)).reverse() .immutableSortedCopy(neighbors.keySet()); if (topNeighbors.size() > 10) topNeighbors = topNeighbors.subList(0, 10); // Print the top 10 neighbors (and paths) System.out.printf("%8s%10s\t%s\n", "NodeID", "#Visits", "Top 2 Paths with counts"); for (int id : topNeighbors) { int numVisits = neighbors.get(id); System.out.printf("%8s%10s\t", id, numVisits); if (lm._2.isDefined()) { // If Option is not None Object2IntMap<DirectedPath> paths = lm._2.get().get(id); int remaining = paths.size(); for (Map.Entry<DirectedPath, Integer> ef : paths.entrySet()) { // Print a directed path and #visits along that path int[] nodes = ef.getKey().nodes(); for (int i = 0; i < nodes.length; i++) { if (i != 0) System.out.printf("->%d", nodes[i]); else System.out.printf("%d", nodes[i]); } System.out.printf(" (%d)", ef.getValue()); if (remaining > 1) System.out.printf(" | "); remaining--; } } System.out.println(); } }
From source file:org.apache.whirr.util.Utils.java
/** * converts a map to a loading cache.// w ww . j ava2 s.c o m */ public static <K, V> LoadingCache<K, V> convertMapToLoadingCache(Map<K, V> in) { return CacheBuilder.newBuilder().build(CacheLoader.from(Functions.forMap(in))); }
From source file:com.evidon.areweprivateyet.ValueComparableMap.java
private ValueComparableMap(Ordering<? super V> partialValueOrdering, HashMap<K, V> valueMap) { super(partialValueOrdering //Apply the value ordering .onResultOf(Functions.forMap(valueMap)) //On the result of getting the value for the key from the map .compound(Ordering.natural())); //as well as ensuring that the keys don't get clobbered this.valueMap = valueMap; }
From source file:uk.ac.open.kmi.iserve.discovery.api.ranking.impl.ReverseRanker.java
@Override public SortedMap<URI, Double> rank(Map<URI, Double> map) { Ordering<URI> byScoreOrdering = Ordering.natural().onResultOf(Functions.forMap(map)) .compound(Ordering.usingToString()); return ImmutableSortedMap.copyOf(map, byScoreOrdering); }
From source file:uk.ac.open.kmi.iserve.discovery.api.ranking.impl.StandardRanker.java
@Override public SortedMap<URI, Double> rank(Map<URI, Double> map) { Ordering<URI> byScoreOrdering = Ordering.natural().reverse().onResultOf(Functions.forMap(map)) .compound(Ordering.usingToString()); return ImmutableSortedMap.copyOf(map, byScoreOrdering); }
From source file:com.insightml.data.features.stats.FeaturesImportance.java
@Override public String getText(final ISamples<?, Double> instances, final int labelIndex) { final FeatureStatistics stats = new FeatureStatistics(instances, labelIndex); final Map<String, Double> mi = new MutualInformation(0.1).run(stats); final Map<String, Double> chi = new ChiSquare(0.1).run(stats); final StringBuilder builder = new StringBuilder(1024); final SimpleFormatter formatter = new SimpleFormatter(5, true); for (final Entry<String, Double> feature : ImmutableSortedMap .copyOf(mi, Ordering.natural().reverse().onResultOf(Functions.forMap(mi))).entrySet()) { builder.append(UiUtils.fill(feature.getKey(), 25) + "\t"); builder.append("MutualInformation: " + UiUtils.fill(formatter.format(feature.getValue()), 12)); builder.append("ChiSquare: " + UiUtils.fill(formatter.format(chi.get(feature.getKey())), 12)); builder.append("\n"); }//from w w w . ja v a 2 s .com return builder.toString(); }
From source file:com.facebook.buck.util.FakeListeningProcessExecutor.java
public FakeListeningProcessExecutor(Multimap<ProcessExecutorParams, FakeListeningProcessState> processStates) { this(Functions.forMap(processStates.asMap()), new SettableFakeClock(0, 0)); }
From source file:org.jclouds.byon.config.CacheNodeStoreModule.java
public CacheNodeStoreModule(Map<String, Node> backing) { this(CacheBuilder.newBuilder().<String, Node>build(CacheLoader.from(Functions.forMap(backing)))); for (String node : backing.keySet()) this.backing.getUnchecked(node); }
From source file:org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestConfiguration.java
void setFields(Map<String, Field> fields) { Function<Field, String> getName = new Function<Field, String>() { public String apply(Field item) { return item.getName(); }// ww w . ja v a 2 s . c o m }; Function<String, String> comparatorFunction = Functions.compose(getName, Functions.forMap(fields)); Ordering<String> comparator = Ordering.natural().onResultOf(comparatorFunction); this.fields = ImmutableSortedMap.copyOf(fields, comparator); }