abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1199462 - in /abdera/abdera2: common/src/main/java/org/apache/abdera2/common/lang/ common/src/main/java/org/apache/abdera2/common/misc/ examples/src/main/java/org/apache/abdera2/examples/activities/
Date Tue, 08 Nov 2011 21:15:55 GMT
Author: jmsnell
Date: Tue Nov  8 21:15:55 2011
New Revision: 1199462

URL: http://svn.apache.org/viewvc?rev=1199462&view=rev
Log: (empty)

Modified:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/SubtagSet.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MapRed.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreFunctions.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Pair.java
    abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/MapRedExample.java

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/SubtagSet.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/SubtagSet.java?rev=1199462&r1=1199461&r2=1199462&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/SubtagSet.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/lang/SubtagSet.java Tue
Nov  8 21:15:55 2011
@@ -24,7 +24,6 @@ import java.util.LinkedList;
 import java.util.List;
 
 import org.apache.abdera2.common.lang.Subtag.Type;
-import org.apache.abdera2.common.misc.MoreFunctions;
 
 public abstract class SubtagSet 
   implements Serializable, 

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MapRed.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MapRed.java?rev=1199462&r1=1199461&r2=1199462&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MapRed.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MapRed.java Tue Nov
 8 21:15:55 2011
@@ -16,7 +16,6 @@ import com.google.common.base.Equivalenc
 import com.google.common.base.Function;
 import com.google.common.base.Functions;
 import com.google.common.base.Predicate;
-import com.google.common.base.Supplier;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Iterators;
 
@@ -40,84 +39,42 @@ import com.google.common.collect.Iterato
  * an application.
  * 
  * A number of common basic Mapper and Reducer objects are also provided.
+ * 
+ * <pre>
+ * Function&lt;Iterable&lt;Pair&lt;Void,String>>,Iterable&lt;Pair&lt;String,Iterable&lt;Integer>>>>
f1 = ...
+ * Iterable&lt;Pair&lt;String,Iterable&lt;Integer>>> res = f1.apply(gen.get());
+ * </pre>
+ * 
+ * By default, the Function, and the wrapped MapReduce operation, executes 
+ * within the current thread and blocks until completion. If you need async
+ * operation.. first, you should consider whether you need a full MapReduce 
+ * implementation like Hadoop, or, you can use wrap the Function in a 
+ * MoreFunctions.FutureFunction... e.g. 
+ * 
+ * <pre>
+ * ExecutorService exec = 
+ *   MoreExecutors.getExitingExecutorService(
+ *     (ThreadPoolExecutor) Executors.newCachedThreadPool());
+ *
+ * Function&lt;
+ *   Iterable&lt;MapRed.Pair&lt;Void, Activity>>, 
+ *   Future&lt;Iterable&lt;MapRed.Pair&lt;Integer,Iterable&lt;String>>>>>
ff = 
+ *   MoreFunctions.&lt;
+ *     Iterable&lt;MapRed.Pair&lt;Void, Activity>>, 
+ *     Iterable&lt;MapRed.Pair&lt;Integer,Iterable&lt;String>>>>futureFunction(f3,exec);
+ * </pre>
+ * 
+ * Note, again, the tricky use of generics but defining things in this way
+ * ensures type-safety throughout the operation. The FutureFunction.apply() 
+ * will submit the operation to the provided Executor and return a Future 
+ * whose value will be set once the operation completes. For greater control 
+ * over the execution, you can call MoreFunctions.functionTask(...) to get
+ * a FutureTask instance that you can submit the ExecutorService yourself.
  */
 public final class MapRed {
   
   private MapRed() {}
   
-  /**
-   * Begin constructing the input to a MapReduce Function using a 
-   * fluent factory API.. e.g.
-   * <pre>
-   * MapRed.PairBuilder&lt;Void,Activity> gen = 
-   *   MapRed
-   *    .&lt;Void,String>make()
-   *    .pair(null, "a")
-   *    .pair(null, "b")
-   *    .pair(null, "c");
-   * </pre>
-   * 
-   * The PairBuilder.get() method return an Iterable of MapRed.Pair
-   * objects. That Iterable is used as the input to the MapReduce Function.. 
-   * e.g.
-   * 
-   * <pre>
-   * Function&lt;Iterable&lt;Pair&lt;Void,String>>,Iterable&lt;Pair&lt;String,Iterable&lt;Integer>>>>
f1 = ...
-   * Iterable&lt;Pair&lt;String,Iterable&lt;Integer>>> res = f1.apply(gen.get());
-   * </pre>
-   * 
-   * By default, the Function, and the wrapped MapReduce operation, executes 
-   * within the current thread and blocks until completion. If you need async
-   * operation.. first, you should consider whether you need a full MapReduce 
-   * implementation like Hadoop, or, you can use wrap the Function in a 
-   * MoreFunctions.FutureFunction... e.g. 
-   * 
-   * <pre>
-   * ExecutorService exec = 
-   *   MoreExecutors.getExitingExecutorService(
-   *     (ThreadPoolExecutor) Executors.newCachedThreadPool());
-   *
-   * Function&lt;
-   *   Iterable&lt;MapRed.Pair&lt;Void, Activity>>, 
-   *   Future&lt;Iterable&lt;MapRed.Pair&lt;Integer,Iterable&lt;String>>>>>
ff = 
-   *   MoreFunctions.&lt;
-   *     Iterable&lt;MapRed.Pair&lt;Void, Activity>>, 
-   *     Iterable&lt;MapRed.Pair&lt;Integer,Iterable&lt;String>>>>futureFunction(f3,exec);
-   * </pre>
-   * 
-   * Note, again, the tricky use of generics but defining things in this way
-   * ensures type-safety throughout the operation. The FutureFunction.apply() 
-   * will submit the operation to the provided Executor and return a Future 
-   * whose value will be set once the operation completes. For greater control 
-   * over the execution, you can call MoreFunctions.functionTask(...) to get
-   * a FutureTask instance that you can submit the ExecutorService yourself.
-   */
-  public static <K,V>PairBuilder<K,V> make() {
-    return new PairBuilder<K,V>();
-  }
-  
-  public static class PairBuilder<K,V> implements Supplier<Iterable<Pair<K,V>>>,
PairReader<K,V> {
-    private final List<Pair<K,V>> list = 
-      new ArrayList<Pair<K,V>>();
-    public PairBuilder<K,V> pair(K k, V v) {
-      list.add(Pair.<K,V>of(k,v));
-      return this;
-    }
-    public Iterable<Pair<K, V>> get() {
-      return list;
-    }
-    public Iterator<Pair<K, V>> iterator() {
-      return get().iterator();
-    }
-  }
-  
-  public static <K,V>Iterable<Pair<K,V>> pairs(Map<K,V> map) {
-    List<Pair<K,V>> list = new ArrayList<Pair<K,V>>();
-    for (Map.Entry<K,V> entry : map.entrySet())
-      list.add(Pair.of(entry.getKey(),entry.getValue()));
-    return list;
-  }
-  
   public static interface Reducer<K2,V2,K3,V3> {
     void reduce(
       K2 key, 
@@ -133,8 +90,6 @@ public final class MapRed {
     void collect(K key, V val);
   }
   
-  public static interface PairReader<K,V> extends Iterable<Pair<K,V>> {}
-  
   public static interface MapperFunction<K1,V1,K2,V2>
     extends Function<Iterable<Pair<K1,V1>>,Iterable<Pair<K2,Iterable<V2>>>>
{}
 

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreFunctions.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreFunctions.java?rev=1199462&r1=1199461&r2=1199462&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreFunctions.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MoreFunctions.java
Tue Nov  8 21:15:55 2011
@@ -24,6 +24,14 @@ import com.google.common.util.concurrent
 
 public class MoreFunctions {
   
+  public static <K,V>Function<K,V> alwaysNull() {
+    return new Function<K,V>() {
+      public V apply(K input) {
+        return null;
+      }
+    };
+  }
+  
   public static <T,S>Set<S> immutableSetOf(T[] items, Function<T,S> transform,
Class<S> _class) {
     S[] set = each(items, transform, _class);
     return ImmutableSet.<S>copyOf(set);

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Pair.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Pair.java?rev=1199462&r1=1199461&r2=1199462&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Pair.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Pair.java Tue Nov 
8 21:15:55 2011
@@ -1,9 +1,13 @@
 package org.apache.abdera2.common.misc;
 
-import java.util.List;
+import java.util.Iterator;
+import java.util.Map;
 
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Lists;
+import com.google.common.base.Function;
+import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableSet;
+
+import static com.google.common.base.Preconditions.checkNotNull;
 
 public class Pair<K,V> {
 
@@ -22,7 +26,7 @@ public class Pair<K,V> {
   public V second() {
     return v;
   }
-
+  
   public static <K,V>Pair<K,V> of(K k, V v) {
     return new Pair<K,V>(k,v);
   }
@@ -32,11 +36,18 @@ public class Pair<K,V> {
     return new Pair<String,String>(split[0],split.length>1?split[1]:null);
   }
   
+  public static <K,V>Iterable<Pair<K,V>> from(Map<K,V> map) {
+    PairBuilder<K,V> pb = make();
+    for (Map.Entry<K,V> entry : map.entrySet())
+      pb.pair(entry);
+    return pb.get();
+  }
+  
   public static Iterable<Pair<String,String>> from(String[] pairs) {
-    List<Pair<String,String>> list = Lists.newArrayList();
+    PairBuilder<String,String> pb = make();
     for (String pair : pairs)
-      list.add(from(pair));
-    return Iterables.unmodifiableIterable(list);
+      pb.pair(from(pair));
+    return pb.get();
   }
   
   public static Iterable<Pair<String,String>> from(String pairs, String delim)
{
@@ -71,4 +82,48 @@ public class Pair<K,V> {
     return true;
   }
   
+  public static <K,V>PairBuilder<K,V> make() {
+    return new PairBuilder<K,V>();
+  }
+  
+  public static class PairBuilder<K,V> 
+    implements Supplier<Iterable<Pair<K,V>>>, PairReader<K,V> {
+    private final ImmutableSet.Builder<Pair<K,V>> builder =
+      ImmutableSet.builder();
+    public PairBuilder<K,V> index(
+      Function<V,K> keyFunction,
+      V... vals) {
+      checkNotNull(keyFunction);
+      for (V v : vals)
+        pair(keyFunction.apply(v),v);
+      return this;      
+    }
+    public PairBuilder<K,V> index(
+      Function<V,K> keyFunction, 
+      Iterable<V> vals) {
+      checkNotNull(keyFunction);
+      for (V v : checkNotNull(vals))
+        pair(keyFunction.apply(v),v);
+      return this;
+    }
+    public PairBuilder<K,V> pair(K k, V v) {
+      builder.add(Pair.<K,V>of(k,v));
+      return this;
+    }
+    public PairBuilder<K,V> pair(Pair<K,V> pair) {
+      builder.add(pair);
+      return this;
+    }
+    public PairBuilder<K,V> pair(Map.Entry<K,V> entry) {
+      return pair(entry.getKey(),entry.getValue());
+    }
+    public Iterable<Pair<K, V>> get() {
+      return builder.build();
+    }
+    public Iterator<Pair<K, V>> iterator() {
+      return get().iterator();
+    }
+  }
+  
+  public static interface PairReader<K,V> extends Iterable<Pair<K,V>> {}
 }

Modified: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/MapRedExample.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/MapRedExample.java?rev=1199462&r1=1199461&r2=1199462&view=diff
==============================================================================
--- abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/MapRedExample.java
(original)
+++ abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/activities/MapRedExample.java
Tue Nov  8 21:15:55 2011
@@ -23,7 +23,7 @@ import org.apache.abdera2.common.misc.Mo
 import org.apache.abdera2.common.misc.MapRed.Collector;
 import org.apache.abdera2.common.misc.MapRed.Mapper;
 import org.apache.abdera2.common.misc.Pair;
-import org.apache.abdera2.common.misc.MapRed.PairBuilder;
+import org.apache.abdera2.common.misc.Pair.PairBuilder;
 import org.apache.abdera2.common.misc.MapRed.ReducerFunction;
 import org.apache.abdera2.model.Document;
 import org.apache.abdera2.model.Feed;
@@ -103,10 +103,8 @@ public class MapRedExample {
     
     // Prepare the input data
     PairBuilder<Void,Activity> gen = 
-      MapRed
-      .<Void,Activity>make();
-    for (Activity activity : col.getItems())
-      gen.pair(null, activity);
+      Pair.<Void,Activity>make()
+        .index(MoreFunctions.<Activity,Void>alwaysNull(), col.getItems());
         
     // The Function ff is asynchronous... we apply it, then call get on
     // the returned Future to wait the result. The mapreduce operation



Mime
View raw message