abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1195721 - in /abdera/abdera2: activities/src/main/java/org/apache/abdera2/activities/extra/ common/src/main/java/org/apache/abdera2/common/misc/ common/src/main/java/org/apache/abdera2/common/text/
Date Mon, 31 Oct 2011 23:44:43 GMT
Author: jmsnell
Date: Mon Oct 31 23:43:57 2011
New Revision: 1195721

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

Modified:
    abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/Comparisons.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java

Modified: abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java?rev=1195721&r1=1195720&r2=1195721&view=diff
==============================================================================
--- abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
(original)
+++ abdera/abdera2/activities/src/main/java/org/apache/abdera2/activities/extra/Extra.java
Mon Oct 31 23:43:57 2011
@@ -1,19 +1,28 @@
 package org.apache.abdera2.activities.extra;
 
+import java.lang.reflect.Method;
 import java.util.Comparator;
 
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
+
 import org.apache.abdera2.activities.model.ASObject;
 import org.apache.abdera2.activities.model.Activity;
 import org.apache.abdera2.activities.model.Activity.Audience;
 import org.apache.abdera2.activities.model.IO;
 import org.apache.abdera2.activities.model.Verb;
+import org.apache.abdera2.common.anno.AnnoUtil;
+import org.apache.abdera2.common.anno.Name;
 import org.apache.abdera2.common.date.DateTimes;
+import org.apache.abdera2.common.misc.MoreFunctions;
 import org.apache.abdera2.common.selector.AbstractSelector;
 import org.apache.abdera2.common.selector.PropertySelector;
 import org.apache.abdera2.common.selector.Selector;
 import org.apache.abdera2.common.selector.MultiSelector;
 import org.joda.time.DateTime;
 
+import com.google.common.base.CaseFormat;
 import com.google.common.base.Equivalence;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
@@ -764,4 +773,104 @@ public class Extra {
         return innerCompare(d1,d2);
       }
   }
+  
+  
+  private static String get_name(Method obj) {   
+    String name = null;
+    if (obj.isAnnotationPresent(Name.class))
+      name = obj.getAnnotation(Name.class).value();
+    else {
+      name = obj.getName();
+      if (name.startsWith("get") || 
+        name.startsWith("set"))
+      name = name.substring(3);
+      name = CaseFormat.UPPER_CAMEL.to(
+          CaseFormat.LOWER_CAMEL, name);
+    }
+    return name;
+  }
+ 
+  /**
+   * Uses cglib to create an extension of the base ASObject type
+   * that implements the given interface. All setter/getter methods
+   * on the supplied interface will be mapped to properties on the
+   * underlying ASObject.. for instance, getFoo() and setFoo(..) will
+   * be mapped to a "foo" property
+   */
+  public static <T>T extend(
+    Class<T> type, 
+    Class<? extends ASObject> base) {
+    checkNotNull(type);
+    Enhancer e = new Enhancer();
+    if (type.isInterface()) {
+      e.setSuperclass(base);
+      e.setInterfaces(MoreFunctions.array(type));
+    } else if (ASObject.class.isAssignableFrom(type)) {
+      e.setSuperclass(type);
+    }
+    e.setCallback(new ExtensionObjectProxy(type,base));
+    ASObject obj = (ASObject) e.create();
+    obj.setObjectType(AnnoUtil.getName(type));
+    return type.cast(obj);
+  }
+  
+  /**
+   * Uses cglib to create an extension of the base ASObject type
+   * that implements the given interface. All setter/getter methods
+   * on the supplied interface will be mapped to properties on the
+   * underlying ASObject.. for instance, getFoo() and setFoo(..) will
+   * be mapped to a "foo" property
+   */
+  public static <T>T extend(Class<T> type) {
+    checkNotNull(type);
+    Enhancer e = new Enhancer();
+    if (type.isInterface()) {
+      e.setSuperclass(ASObject.class);
+      e.setInterfaces(MoreFunctions.array(type));
+    } else if (ASObject.class.isAssignableFrom(type)) {
+      e.setSuperclass(type);
+    }
+    e.setCallback(new ExtensionObjectProxy(type));
+    ASObject obj = (ASObject) e.create();
+    obj.setObjectType(type.getSimpleName().toLowerCase());
+    return type.cast(obj);
+  }
+  
+  private static class ExtensionObjectProxy 
+    implements MethodInterceptor {
+    private final Class<?> type;
+    private final Class<?> base;
+    ExtensionObjectProxy(Class<?> type) {
+      this.type = type;
+      this.base = type;
+    }
+    ExtensionObjectProxy(Class<?> type, Class<?> base) {
+      this.type = type;
+      this.base = base;
+    }
+    public Object intercept(
+      Object obj, 
+      Method method, 
+      Object[] args,
+      MethodProxy proxy) 
+        throws Throwable {
+      ASObject as = (ASObject) obj;
+      if (method.getDeclaringClass().equals(type) ||
+          method.getDeclaringClass().equals(base)) {
+        boolean setter = 
+          method.getName().matches("[Ss]et.+") || 
+          (void.class.isAssignableFrom(method.getReturnType()) && 
+          method.getParameterTypes().length == 1);
+        String name = get_name(method);
+        if (setter) {
+          if (args.length != 1)
+            throw new IllegalArgumentException();
+          as.setProperty(name,args[0]);
+          return null;
+        } else {
+          return method.getReturnType().cast(as.getProperty(name));
+        }
+      } else return proxy.invokeSuper(obj, args);
+    }    
+  }
 }

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=1195721&r1=1195720&r2=1195721&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 Mon
Oct 31 23:43:57 2011
@@ -78,6 +78,10 @@ public class Comparisons {
     return onlySecondApplies(predicate).apply(t1, t2);
   }
   
+  public static <T>boolean testBoth(T t1, T t2, Predicate<T> p1, Predicate<T>
p2) {
+    return testBoth(p1,p2).apply(t1,t2);
+  }
+  
   public static final Comparison<Object> bothAreNull = 
     bothAreNull();
   
@@ -113,6 +117,17 @@ public class Comparisons {
   }
   
   /**
+   * Returns true if predicate1.apply(r1) && predicate2.apply(r2)
+   */
+  public static <T>Comparison<T> testBoth(final Predicate<T> predicate1,
final Predicate<T> predicate2) {
+    return new Comparison<T>() {
+      public boolean apply(T r1, T r2) {
+        return predicate1.apply(r1) && predicate2.apply(r2);
+      }
+    };
+  }
+  
+  /**
    * Returns a Comparison that checks both inputs against a Predicate
    */
   public static <T>Comparison<T> bothApply(final Predicate<T> predicate)
{

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java?rev=1195721&r1=1195720&r2=1195721&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/misc/MorePredicates.java
Mon Oct 31 23:43:57 2011
@@ -4,10 +4,11 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.regex.Pattern;
 
+import org.apache.abdera2.common.selector.AbstractSelector;
 import org.apache.abdera2.common.selector.PropertySelector;
+import org.apache.abdera2.common.selector.Selector;
 import org.apache.abdera2.common.text.CodepointMatcher;
 
-import com.google.common.base.Predicate; 
 import com.google.common.base.Predicates;
 
 import static com.google.common.base.Preconditions.*;
@@ -16,20 +17,22 @@ public final class MorePredicates {
 
   private MorePredicates() {}
   
-  public static Predicate<String> equalsIgnoreCase(final String val) {
-    return new Predicate<String>() {
-      public boolean apply(String input) {
+  public static Selector<String> equalsIgnoreCase(final String val) {
+    return new AbstractSelector<String>() {
+      public boolean select(Object input) {
         checkNotNull(input);
         checkNotNull(val);
-        return input.equalsIgnoreCase(val);
+        return input.toString().equalsIgnoreCase(val);
       }
     };
   }
   
-  public static Predicate<String> notNullOrEmpty() {
-    return new Predicate<String>() {
-      public boolean apply(String input) {
-        return input != null && input.length() > 0;
+  public static Selector<String> notNullOrEmpty() {
+    return new AbstractSelector<String>() {
+      public boolean select(Object input) {
+        checkArgument(input instanceof String);
+        String val = input.toString();
+        return val != null && val.length() > 0;
       }
     };
   }
@@ -40,7 +43,7 @@ public final class MorePredicates {
    * and MUST NOT require any input parameters. The method name is case
    * sensitive. 
    */
-  public static <T>Predicate<T> isNull(Class<T> _class, String method)
{
+  public static <T>Selector<T> isNull(Class<T> _class, String method) {
     return PropertySelector.<T>create(_class, method, Predicates.isNull());
   }
   
@@ -50,7 +53,7 @@ public final class MorePredicates {
    * and MUST NOT require any input parameters. The method name is case
    * sensitive. 
    */
-  public static <T>Predicate<T> isNotNull(Class<T> _class, String method)
{
+  public static <T>Selector<T> isNotNull(Class<T> _class, String method)
{
     return PropertySelector.<T>create(_class, method, Predicates.not(Predicates.isNull()));
   }
   
@@ -58,36 +61,66 @@ public final class MorePredicates {
    * Returns a Predicate that checks if the value of a named property of 
    * instances of the specified class is an instance of the given test class 
    */
-  public static <T>Predicate<T> instanceOf(Class<T> _class, String method,
Class<?> _test) {
+  public static <T>Selector<T> instanceOf(Class<T> _class, String method,
Class<?> _test) {
     return PropertySelector.<T>create(_class, method, Predicates.instanceOf(_test));
   }
   
-  public static <T>Predicate<T> assignableFrom(Class<T> _class, String
method, Class<?> _test) {
+  public static <T>Selector<T> assignableFrom(Class<T> _class, String method,
Class<?> _test) {
     return PropertySelector.<T>create(_class, method, Predicates.assignableFrom(_test));
   }
   
-  public static <T>Predicate<T> containsPattern(Class<T> _class, String
method, Pattern pattern) {
+  public static <T>Selector<T> containsPattern(Class<T> _class, String
method, Pattern pattern) {
     return PropertySelector.<T>create(_class, method, Predicates.contains(pattern));
   }
   
-  public static <T>Predicate<T> containsPattern(Class<T> _class, String
method, String pattern) {
+  public static <T>Selector<T> containsPattern(Class<T> _class, String
method, String pattern) {
     return containsPattern(_class,method,Pattern.compile(pattern));
   }
   
-  public static <T>Predicate<T> matches(Class<T> _class, String method,
CodepointMatcher matcher) {
+  public static <T>Selector<T> matches(Class<T> _class, String method,
CodepointMatcher matcher) {
     return PropertySelector.<T>create(_class, method, matcher);
   }
   
-  public static <T>Predicate<T> equalTo(Class<T> _class, String method,
Object obj) {
+  public static <T>Selector<T> equalTo(Class<T> _class, String method,
Object obj) {
     return PropertySelector.<T>create(_class, method, Predicates.equalTo(obj));
   }
   
-  public static <T>Predicate<T> in(Class<T> _class, String method, Collection<T>
items) {
+  public static <T>Selector<T> in(Class<T> _class, String method, Collection<T>
items) {
     return PropertySelector.<T>create(_class, method, Predicates.in(items));
   }
   
-  public static <T>Predicate<T> in(Class<T> _class, String method, T...
items) {
+  public static <T>Selector<T> in(Class<T> _class, String method, T...
items) {
     return in(_class,method,Arrays.asList(items));
   }
   
+  public static Selector<Long> longNotNegativeOrNull = 
+    new AbstractSelector<Long>() {
+      public boolean select(Object item) {
+        if (item == null) return false;
+        checkArgument(item instanceof Long);
+        Long i = (Long)item;
+        return i >= 0;
+      }
+  };
+  
+  public static Selector<Integer> intNotNegativeOrNull = 
+    new AbstractSelector<Integer>() {
+      public boolean select(Object item) {
+        if (item == null) return false;
+        checkArgument(item instanceof Integer);
+        Integer i = (Integer)item;
+        return i >= 0;
+      }
+  };
+  
+  public static Selector<Short> shortNotNegativeOrNull = 
+    new AbstractSelector<Short>() {
+      public boolean select(Object item) {
+        if (item == null) return false;
+        checkArgument(item instanceof Short);
+        Short i = (Short)item;
+        return i >= 0;
+      }
+  };
+  
 }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java?rev=1195721&r1=1195720&r2=1195721&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/Codec.java Mon Oct
31 23:43:57 2011
@@ -206,6 +206,7 @@ public enum Codec {
   private static class DecodeFunction
     implements Function<String,String> {
     public String apply(String input) {
+      if (input == null) return null;
       return decode(input);
     }
   }

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java?rev=1195721&r1=1195720&r2=1195721&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java (original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/text/UrlEncoding.java Mon
Oct 31 23:43:57 2011
@@ -577,6 +577,7 @@ public final class UrlEncoding {
       return new Function<String,String>() {
         public String apply(String input) {
           try {
+            if (input == null) return null;
             return decode(input,charset);
           } catch (Throwable t) {
             throw ExceptionHelper.propogate(t);



Mime
View raw message