abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1221623 - in /abdera/abdera2: common/src/main/java/org/apache/abdera2/common/misc/ test/src/main/java/org/apache/abdera2/test/activities/ test/src/main/java/org/apache/abdera2/test/common/misc/
Date Wed, 21 Dec 2011 06:08:34 GMT
Author: jmsnell
Date: Wed Dec 21 06:08:33 2011
New Revision: 1221623

URL: http://svn.apache.org/viewvc?rev=1221623&view=rev
Log:
bug fixes and test cases...

Modified:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparison.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparisons.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java
    abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/misc/MiscTest.java

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparison.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparison.java?rev=1221623&r1=1221622&r2=1221623&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparison.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparison.java Wed
Dec 21 06:08:33 2011
@@ -17,6 +17,11 @@
  */
 package org.apache.abdera2.common.misc;
 
+import org.apache.abdera2.common.selector.AbstractSelector;
+import org.apache.abdera2.common.selector.Selector;
+
+import com.google.common.base.Predicate;
+
 /**
  * A Comparison is similiar to Comparable in that it compares
  * the equivalence of two Objects based on some specific condition,
@@ -36,7 +41,7 @@ public abstract class Comparison<R> {
 
   public abstract boolean apply(R r1, R r2);
   
-  public Comparison<R> negate() {
+  public final Comparison<R> negate() {
     final Comparison<R> _this = this;
     return new Comparison<R>() {
       public boolean apply(R r1, R r2) {
@@ -45,7 +50,7 @@ public abstract class Comparison<R> {
     };
   }
   
-  public Comparison<R> and(final Comparison<R> other) {
+  public final Comparison<R> and(final Comparison<R> other) {
     final Comparison<R> _this = this;
     return new Comparison<R>() {
       public boolean apply(R r1, R r2) {
@@ -54,7 +59,7 @@ public abstract class Comparison<R> {
     };
   }
   
-  public Comparison<R> or(final Comparison<R> other) {
+  public final Comparison<R> or(final Comparison<R> other) {
     final Comparison<R> _this = this;
     return new Comparison<R>() {
       public boolean apply(R r1, R r2) {
@@ -63,7 +68,7 @@ public abstract class Comparison<R> {
     };
   }
   
-  public Comparison<R> not(final Comparison<R> other) {
+  public final Comparison<R> not(final Comparison<R> other) {
     final Comparison<R> _this = this;
     return new Comparison<R>() {
       public boolean apply(R r1, R r2) {
@@ -72,4 +77,22 @@ public abstract class Comparison<R> {
     };
   } 
 
+  public final Predicate<R> predicateFor(final R first) {
+    final Comparison<R> comp = this;
+    return new Predicate<R>() {
+      public boolean apply(R input) {
+        return comp.apply(first, input);
+      }
+    };
+  }
+  
+  public final Selector<R> selectorFor(final R first) {
+    final Comparison<R> comp = this;
+    return new AbstractSelector<R>() {
+      @SuppressWarnings("unchecked")
+      public boolean select(Object item) {
+        return comp.apply(first,(R)item);
+      }
+    };
+  }
 }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparisons.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparisons.java?rev=1221623&r1=1221622&r2=1221623&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparisons.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparisons.java Wed
Dec 21 06:08:33 2011
@@ -269,4 +269,31 @@ public class Comparisons {
     };
   }
   
+  public static <T>Comparison<T> equals() {
+    return Comparisons.<T>neitherIsNull()
+      .and(new Comparison<T>() {
+        public boolean apply(T r1, T r2) {
+          return r1.equals(r2);
+        }
+      });
+  }
+  
+  public static <T>Comparison<T> identity() {
+    return Comparisons.<T>neitherIsNull()
+      .and(new Comparison<T>() {
+        public boolean apply(T r1, T r2) {
+          return r1 == r2;
+        }
+      });
+  }
+  
+  public static Comparison<String> equalsIgnoreCase() {
+    return Comparisons.<String>neitherIsNull()
+      .and(new Comparison<String>() {
+        public boolean apply(String r1, String r2) {
+          return r1.equalsIgnoreCase(r2);
+        }
+      });
+  }
+  
 }

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java?rev=1221623&r1=1221622&r2=1221623&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java
(original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/activities/ActivitiesTest.java
Wed Dec 21 06:08:33 2011
@@ -25,6 +25,7 @@ import org.apache.abdera2.activities.mod
 import org.apache.abdera2.activities.model.ASObject;
 import org.apache.abdera2.activities.model.Activity;
 import org.apache.abdera2.activities.model.Collection;
+import org.apache.abdera2.activities.model.CollectionWriter;
 import org.apache.abdera2.activities.model.Verb;
 import org.apache.abdera2.activities.model.ASBase.ASBuilder;
 import org.apache.abdera2.activities.model.IO;
@@ -53,6 +54,7 @@ import org.junit.Test;
 
 import com.google.common.base.Function;
 import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Multimap;
 
 public class ActivitiesTest {
@@ -266,6 +268,26 @@ public class ActivitiesTest {
   }
   
   @Test
+  public void testCollectionWriter() {
+    IO io = IO.get();
+    StringWriter sw = new StringWriter();
+    CollectionWriter cw = io.getCollectionWriter(sw);
+    cw.writeHeader(
+      ASBase.make()
+      .set("a","b")
+      .get());
+    cw.writeObject(
+      Activity.makeActivity().verb(Verb.POST).get());
+    cw.writeObjects(
+      Activity.makeActivity().verb(Verb.POST).get(),
+      Activity.makeActivity().verb(Verb.POST).get());
+    cw.complete();
+    Collection<Activity> col = io.read(sw.toString());
+    assertEquals("b",col.getProperty("a"));
+    assertEquals(3, Iterables.size(col.getItems()));
+  }
+  
+  @Test
   public void testMediaLink() {
     MediaLink ml = 
       MediaLink.makeMediaLink()

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/misc/MiscTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/misc/MiscTest.java?rev=1221623&r1=1221622&r2=1221623&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/misc/MiscTest.java (original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/common/misc/MiscTest.java Wed
Dec 21 06:08:33 2011
@@ -1,15 +1,47 @@
 package org.apache.abdera2.test.common.misc;
 
+import static org.apache.abdera2.common.misc.MapRed.asFunction;
+import static org.apache.abdera2.common.misc.MapRed.compose;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertThat;
+import static org.junit.matchers.JUnitMatchers.*;
 
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
+import java.util.Locale;
+import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.atomic.AtomicInteger;
 
+import org.apache.abdera2.activities.model.ASObject;
+import org.apache.abdera2.activities.model.Activity;
+import org.apache.abdera2.activities.model.Collection;
+import org.apache.abdera2.activities.model.objects.PersonObject;
+import org.apache.abdera2.common.misc.ArrayBuilder;
+import org.apache.abdera2.common.misc.Chain;
+import org.apache.abdera2.common.misc.Comparison;
+import org.apache.abdera2.common.misc.Comparisons;
+import org.apache.abdera2.common.misc.MapRed;
+import org.apache.abdera2.common.misc.MoreExecutors2;
+import org.apache.abdera2.common.misc.MoreFunctions;
+import org.apache.abdera2.common.misc.Pair;
+import org.apache.abdera2.common.misc.Task;
+import org.apache.abdera2.common.misc.MapRed.Collector;
+import org.apache.abdera2.common.misc.MapRed.Mapper;
+import org.apache.abdera2.common.misc.MapRed.ReducerFunction;
 import org.junit.Test;
 
+import com.google.common.base.Function;
+import com.google.common.base.Functions;
+import com.google.common.base.Predicates;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Iterators;
 
 public class MiscTest {
@@ -36,4 +68,285 @@ public class MiscTest {
     assertFalse(mi.hasNext());
   }
   
+  @Test
+  public void arrayBuilderTest() {
+    //Set array builder (no duplicates)
+    String[] array = 
+      ArrayBuilder.set(String.class)
+        .add("a")
+        .add("a")
+        .add("b")
+        .add("c")
+        .build();
+    assertEquals(3, array.length);
+    assertEquals("a", array[0]);
+    assertEquals("b", array[1]);
+    assertEquals("c", array[2]);
+    
+    //List array (duplicates)
+    array = 
+      ArrayBuilder.list(String.class)
+        .add("a")
+        .add("a")
+        .add("b")
+        .add("c")
+        .build();
+    assertEquals(4, array.length);
+    assertEquals("a", array[0]);
+    assertEquals("a", array[1]);
+    assertEquals("b", array[2]);
+    assertEquals("c", array[3]);
+    
+  }
+  
+  @Test
+  public void chainTest() {
+    final AtomicInteger i = new AtomicInteger(0);
+    ArrayBuilder<Task<String,String>> tasks = 
+      ArrayBuilder.list(Task.class);
+    for (int n = 0; n < 10; n++) 
+      tasks.add(
+        new Task<String,String>() {
+          public String apply(String input, Chain<String,String> flow) {
+            i.getAndIncrement();
+            return flow.next(input);
+          }
+        });
+    Chain<String,String> chain = 
+      Chain.<String,String>make()
+        .via(tasks.build())
+        .to(new Function<String,String>() {
+          public String apply(String input) {
+            i.getAndIncrement();
+            return input.toUpperCase(Locale.US);
+          }
+        })
+        .get();
+    String v = chain.apply("a");
+    assertEquals("A",v);
+    assertEquals(11, i.get());
+  }
+  
+  @Test
+  public void comparisonTest() {
+    assertTrue(Comparisons.<String>bothApply(Predicates.<String>notNull()).apply("A",
"B"));
+    assertFalse(Comparisons.<String>bothApply(Predicates.<String>notNull()).apply("A",
null));
+    assertFalse(Comparisons.<String>bothApply(Predicates.<String>notNull()).apply(null,
"B"));
+    
+    assertTrue(Comparisons.<String>bothAreNull().apply(null, null));
+    assertFalse(Comparisons.<String>bothAreNull().apply("A", null));
+    assertFalse(Comparisons.<String>bothAreNull().apply(null, "B"));
+    
+    assertTrue(Comparisons.<String>onlyFirstIsNull().apply(null, "A"));
+    assertFalse(Comparisons.<String>onlyFirstIsNull().apply(null, null));
+    assertFalse(Comparisons.<String>onlyFirstIsNull().apply("A", null));
+    assertFalse(Comparisons.<String>onlyFirstIsNull().apply("A", "B"));
+    
+    assertTrue(Comparisons.<String>onlySecondIsNull().apply("A", null));
+    assertFalse(Comparisons.<String>onlySecondIsNull().apply(null, null));
+    assertFalse(Comparisons.<String>onlySecondIsNull().apply(null, "B"));
+    assertFalse(Comparisons.<String>onlySecondIsNull().apply("A", "B"));
+    
+    assertTrue(Comparisons.<String>onlyOneIsNull().apply("A", null));
+    assertFalse(Comparisons.<String>onlyOneIsNull().apply(null, null));
+    assertTrue(Comparisons.<String>onlyOneIsNull().apply(null, "B"));
+    assertFalse(Comparisons.<String>onlyOneIsNull().apply("A", "B"));
+    
+    assertFalse(Comparisons.<String>neitherIsNull().apply("A", null));
+    assertFalse(Comparisons.<String>neitherIsNull().apply(null, null));
+    assertFalse(Comparisons.<String>neitherIsNull().apply(null, "B"));
+    assertTrue(Comparisons.<String>neitherIsNull().apply("A", "B"));
+    
+    assertFalse(Comparisons.eitherApply(Predicates.isNull()).apply("A", "B"));
+    assertTrue(Comparisons.eitherApply(Predicates.isNull()).apply(null, "B"));
+    assertTrue(Comparisons.eitherApply(Predicates.isNull()).apply("A", null));
+    assertTrue(Comparisons.eitherApply(Predicates.isNull()).apply(null, null));
+    
+    Comparison<String> equalsIgnoreCase = 
+      new Comparison<String>() {
+        public boolean apply(String r1, String r2) {
+          return r1.equalsIgnoreCase(r2);
+        }
+    };
+    assertTrue(equalsIgnoreCase.apply("A", "a"));
+    
+    Comparison<String> c = 
+      Comparisons.<String>neitherIsNull().and(equalsIgnoreCase);
+    assertTrue(c.apply("A", "a"));
+    assertFalse(c.apply(null, "a"));
+    assertFalse(c.apply("a", null));
+    
+  }
+  
+  @Test
+  public void testMoreFunctions() {
+    // Up first... Choice
+    Function<String,Integer> choice = 
+      MoreFunctions.<String,Integer>choice()
+      .of(Comparisons.equalsIgnoreCase().predicateFor("A"),1)
+      .of(Comparisons.equalsIgnoreCase().predicateFor("B"),2)
+      .otherwise(3)
+      .get();
+    assertEquals(Integer.valueOf(1),choice.apply("a"));
+    assertEquals(Integer.valueOf(2),choice.apply("b"));
+    assertEquals(Integer.valueOf(3),choice.apply("c"));
+    
+    // Test createInstance
+    Foo foo = MoreFunctions.createInstance(Foo.class).apply(null);
+    assertNotNull(foo);
+    
+    foo = MoreFunctions.createInstance(Foo.class,String.class).apply(MoreFunctions.array("A"));
+    assertNotNull(foo);
+    assertEquals("A",foo.a());
+    
+    // Test each
+    String[] array = MoreFunctions.array("a","b","c","d");
+    array = MoreFunctions
+      .eachArray(new Function<String,String>() {
+        public String apply(String input) {
+          return input.toUpperCase();
+        }
+      },String.class).apply(array);
+    assertEquals("A",array[0]);
+    assertEquals("B",array[1]);
+    assertEquals("C",array[2]);
+    assertEquals("D",array[3]);
+    
+    // Test firstNonNull...
+    assertEquals("A",MoreFunctions.firstNonNull(null,null,null,"A"));
+    
+    // Test futureFunction... executes the function in a separate 
+    // thread using the passed in ExecutorService
+    Future<String> future = 
+      MoreFunctions.<String,String>futureFunction(
+        new Function<String,String>() {
+          public String apply(String input) {
+            System.out.println("Thread sleeping....");
+            try {
+              Thread.sleep(10*1000);
+            } catch (Throwable t) {}
+            return input;
+          }
+        }, 
+        MoreExecutors2.getExitingExecutor()).apply("A");
+    try {
+      System.out.println("Waiting for return...");
+      assertEquals("A",future.get());
+    } catch (Throwable t) {
+      throw new RuntimeException(t);
+    }
+    
+  }
+  
+  @Test 
+  public void testPair() {
+    Pair<String,String> pair = Pair.of("A", "B");
+    assertEquals("A",pair.first());
+    assertEquals("B",pair.second());
+    
+    Map<String,String> map = new HashMap<String,String>();
+    map.put("A","B");
+    map.put("B", "C");
+    Iterable<Pair<String,String>> pairs = Pair.from(map);
+    assertEquals(2, Iterables.size(pairs));
+    assertEquals("A", Iterables.get(pairs,0).first());
+    assertEquals("B", Iterables.get(pairs,0).second());
+    assertEquals("B", Iterables.get(pairs,1).first());
+    assertEquals("C", Iterables.get(pairs,1).second());
+  }
+  
+  private final static Function<
+    Iterable<Pair<Void, Activity>>, 
+    Iterable<Pair<String, Iterable<Integer>>>> f1 =
+      compose(
+        new MyMapper(),
+        MapRed.<String,ASObject>countingReducer()
+      );
+
+  private final static ReducerFunction<String,Integer,Integer,String> f2 =
+      asFunction(MapRed.<String,Integer>invertReducer(), 
+      Collections.<Integer>reverseOrder()); 
+  
+  private final static Function<
+    Iterable<Pair<Void, Activity>>, 
+    Iterable<Pair<Integer,Iterable<String>>>> f3 = 
+      Functions.compose(f2,f1);
+   
+  private final static ExecutorService exec = 
+    MoreExecutors2.getExitingExecutor();
+  
+  private static final Function<Collection<Activity>,Iterable<Pair<Void, Activity>>>
transform = 
+    new Function<Collection<Activity>,Iterable<Pair<Void, Activity>>>()
{
+      public Iterable<Pair<Void, Activity>> apply(Collection<Activity>
input) {
+        return 
+          Pair.<Void,Activity>make()
+            .index(MoreFunctions.<Activity>alwaysVoid(), input.getItems());
+      }
+  };
+  
+  private final static Function<
+    Collection<Activity>, 
+    Future<Iterable<Pair<Integer,Iterable<String>>>>> ff = 
+    Functions.compose(
+    MoreFunctions.<
+      Iterable<Pair<Void, Activity>>, 
+      Iterable<Pair<Integer,Iterable<String>>>>futureFunction(f3,exec),
+    transform);
+
+  private Activity getActivity(String name,int n) {
+    return Activity.makeActivity()
+      .actor(PersonObject.makePerson(name))
+      .id(String.format("urn:%s:%s",name,n))
+      .get();
+  }
+  
+  @Test
+  public void testMapRed() throws Exception {
+    Collection<Activity> col = 
+      Collection.<Activity>makeCollection()
+       .item(getActivity("Joe",1))
+       .item(getActivity("Joe",2))
+       .item(getActivity("Mark",3))
+       .item(getActivity("Mark",4))
+       .item(getActivity("Sally",5))
+       .get();
+    
+    // This is basically MapReduce contained within a Function,
+    // Runs asynch using exiting executorservice... call to 
+    // ff.apply(gen).get() hides all the magic...
+    // this particular function looks at the activity stream
+    // and counts the number of activities per actor
+    
+    Iterable<Pair<Integer,Iterable<String>>> ret = ff.apply(col).get();
+    Pair<Integer,Iterable<String>> first = Iterables.get(ret,0);
+    Pair<Integer,Iterable<String>> second = Iterables.get(ret,1);
+    assertEquals(Integer.valueOf(2),first.first());
+    assertThat(first.second(),hasItems("Joe","Mark"));
+    assertEquals(Integer.valueOf(1),second.first());
+    assertThat(second.second(),hasItems("Sally"));
+  }
+
+  static class MyMapper 
+    implements Mapper<Void,Activity,String,ASObject> {
+    public void map(
+      Void key, 
+      Activity val, 
+      Collector<String,ASObject> context) {
+        String ot = val.getActor().getDisplayName();
+        context.collect(ot!=null?ot:"", val.getActor());
+    }    
+  }
+  
+  public static class Foo {
+    private final String a;
+    public Foo() {
+      this.a = null;
+    }
+    public Foo(String a) {
+      this.a = a;
+    }
+    public String a() {
+      return a;
+    }
+  }
 }



Mime
View raw message