abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1201574 - in /abdera/abdera2/common/src/main/java/org/apache/abdera2/common: protocol/ templates/
Date Mon, 14 Nov 2011 02:15:37 GMT
Author: jmsnell
Date: Mon Nov 14 02:15:37 2011
New Revision: 1201574

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

Modified:
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/TemplateManagerTargetBuilder.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/DefaultingContext.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/MultiContext.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/ObjectContext.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/QueryContext.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java
    abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/TemplateManager.java

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/TemplateManagerTargetBuilder.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/TemplateManagerTargetBuilder.java?rev=1201574&r1=1201573&r2=1201574&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/TemplateManagerTargetBuilder.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/protocol/TemplateManagerTargetBuilder.java
Mon Nov 14 02:15:37 2011
@@ -28,6 +28,8 @@ import org.apache.abdera2.common.templat
 import org.apache.abdera2.common.templates.Template;
 import org.apache.abdera2.common.templates.TemplateManager;
 
+import com.google.common.collect.ImmutableMap;
+
 public class TemplateManagerTargetBuilder<T> 
   extends TemplateManager<T>
   implements TargetBuilder<T> {
@@ -46,7 +48,7 @@ public class TemplateManagerTargetBuilde
   public static class Builder<T> extends TemplateManager.Builder<T> {
     public TemplateManager<T> get() {
       return new TemplateManagerTargetBuilder<T>(
-        templates,isiri,base,defaultContexts.get());
+        templates.build(),isiri,base,defaultContexts.get());
     }
     public TemplateManagerTargetBuilder<T> getTargetBuilder() {
       return (TemplateManagerTargetBuilder<T>) get();
@@ -54,7 +56,7 @@ public class TemplateManagerTargetBuilde
   }
   
   TemplateManagerTargetBuilder(
-      Map<T,Template> templates, 
+      ImmutableMap<T,Template> templates, 
       boolean isiri, 
       IRI base, 
       Context contextDefaults) {

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/DefaultingContext.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/DefaultingContext.java?rev=1201574&r1=1201573&r2=1201574&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/DefaultingContext.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/DefaultingContext.java
Mon Nov 14 02:15:37 2011
@@ -17,12 +17,12 @@
  */
 package org.apache.abdera2.common.templates;
 import static com.google.common.base.Preconditions.*;
-import java.util.HashSet;
 import java.util.Iterator;
-import java.util.Set;
 
 import org.apache.abdera2.common.misc.MoreFunctions;
 
+import com.google.common.collect.ImmutableSet;
+
 @SuppressWarnings("unchecked")
 public class DefaultingContext 
   extends DelegatingContext {
@@ -42,12 +42,12 @@ public class DefaultingContext 
   }
 
   public Iterator<String> iterator() {
-    Set<String> set = new HashSet<String>();
+    ImmutableSet.Builder<String> set = ImmutableSet.builder();
     for (String name : subcontext)
       set.add(name);
     for (String name : defaults)
       set.add(name);
-    return set.iterator();
+    return set.build().iterator();
   }
   
   public boolean contains(String var) {

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/MultiContext.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/MultiContext.java?rev=1201574&r1=1201573&r2=1201574&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/MultiContext.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/MultiContext.java
Mon Nov 14 02:15:37 2011
@@ -17,7 +17,6 @@
  */
 package org.apache.abdera2.common.templates;
 
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
@@ -84,11 +83,11 @@ public final class MultiContext 
     return false;
   }
   public Iterator<String> iterator() {
-    Set<String> names = new HashSet<String>();
+    ImmutableSet.Builder<String> names = ImmutableSet.builder();
     for (Context context : contexts) 
       for (String name : context)
         names.add(name);
-    return names.iterator();
+    return names.build().iterator();
   }
   protected <T> T resolveActual(String var) {
     for (Context context : contexts)

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/ObjectContext.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/ObjectContext.java?rev=1201574&r1=1201573&r2=1201574&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/ObjectContext.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/ObjectContext.java
Mon Nov 14 02:15:37 2011
@@ -22,9 +22,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
-import java.util.HashMap;
 import java.util.Iterator;
-import java.util.Map;
 
 import static com.google.common.base.Preconditions.*;
 import static org.apache.abdera2.common.misc.MorePreconditions.*;
@@ -34,13 +32,14 @@ import org.apache.abdera2.common.templat
 import org.apache.abdera2.common.templates.ObjectContext;
 import org.apache.abdera2.common.anno.Name;
 
+import com.google.common.collect.ImmutableMap;
+
 @SuppressWarnings("unchecked")
 public final class ObjectContext extends CachingContext {
 
     private static final long serialVersionUID = -1387599933658718221L;
     private final Object target;
-    private final Map<String, AccessibleObject> accessors = 
-      new HashMap<String, AccessibleObject>();
+    private final ImmutableMap<String, AccessibleObject> accessors;
 
     public ObjectContext(Object object) {
       this(object, false);
@@ -50,49 +49,49 @@ public final class ObjectContext extends
       super(isiri);
       checkNotNull(object);
       this.target = object;
-      initMethods();
+      this.accessors = initMethods();
     }
     
-    private void initMethods() {
-        Class<?> _class = target.getClass();
-        checkArguments(!_class.isAnnotation(),
-                       !_class.isArray(),
-                       !_class.isEnum(),
-                       !_class.isPrimitive());
-        if (!_class.isInterface()) {
-            Field[] fields = _class.getFields();
-            for (Field field : fields) {
-                if (!Modifier.isPrivate(field.getModifiers())) {
-                    accessors.put(getName(field), field);
-                }
-            }
-        }
-        Method[] methods = _class.getMethods();
-        for (Method method : methods) {
-            String name = method.getName();
-            if (!Modifier.isPrivate(method.getModifiers()) && method.getParameterTypes().length
== 0
-                && !method.getReturnType().equals(Void.class)
-                && !isReserved(name)) {
-                accessors.put(getName(method), method);
-            }
-        }
+    private ImmutableMap<String,AccessibleObject> initMethods() {
+      ImmutableMap.Builder<String, AccessibleObject> accessors = 
+        ImmutableMap.builder();
+      Class<?> _class = target.getClass();
+      checkArguments(!_class.isAnnotation(),
+                     !_class.isArray(),
+                     !_class.isEnum(),
+                     !_class.isPrimitive());
+      if (!_class.isInterface()) {
+        Field[] fields = _class.getFields();
+        for (Field field : fields)
+          if (!Modifier.isPrivate(field.getModifiers()))
+            accessors.put(getName(field), field);
+      }
+      Method[] methods = _class.getMethods();
+      for (Method method : methods) {
+        String name = method.getName();
+        if (!Modifier.isPrivate(method.getModifiers()) && method.getParameterTypes().length
== 0
+            && !method.getReturnType().equals(Void.class)
+            && !isReserved(name))
+          accessors.put(getName(method), method);
+      }
+      return accessors.build();
     }
 
     private String getName(AccessibleObject object) {
-        String name = null;
-        Name varName = object.getAnnotation(Name.class);
-        if (varName != null)
-            return varName.value();
-        if (object instanceof Field) {
-            name = ((Field)object).getName().toLowerCase();
-        } else if (object instanceof Method) {
-            name = ((Method)object).getName().toLowerCase();
-            if (name.startsWith("get"))
-                name = name.substring(3);
-            else if (name.startsWith("is"))
-                name = name.substring(2);
-        }
-        return name;
+      String name = null;
+      Name varName = object.getAnnotation(Name.class);
+      if (varName != null)
+          return varName.value();
+      if (object instanceof Field)
+          name = ((Field)object).getName().toLowerCase();
+      else if (object instanceof Method) {
+        name = ((Method)object).getName().toLowerCase();
+        if (name.startsWith("get"))
+          name = name.substring(3);
+        else if (name.startsWith("is"))
+          name = name.substring(2);
+      }
+      return name;
     }
 
     private boolean isReserved(String name) {

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/QueryContext.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/QueryContext.java?rev=1201574&r1=1201573&r2=1201574&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/QueryContext.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/QueryContext.java
Mon Nov 14 02:15:37 2011
@@ -25,6 +25,8 @@ import java.util.Map;
 import org.apache.abdera2.common.iri.IRI;
 import org.apache.abdera2.common.misc.Pair;
 
+import com.google.common.collect.ImmutableMap;
+
 import static java.util.Collections.addAll;
 import static org.apache.abdera2.common.text.CharUtils.*;
 import static com.google.common.collect.Lists.newArrayList;
@@ -90,7 +92,7 @@ public class QueryContext extends MapCon
     if (query != null)
       for (Pair<String,String> pair : Pair.from(query,"&"))
         setval(map,pair.first(),pair.second());
-    return map;
+    return ImmutableMap.copyOf(map);
   }
   
   @SuppressWarnings({ "rawtypes", "unchecked" })

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java?rev=1201574&r1=1201573&r2=1201574&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/Template.java
Mon Nov 14 02:15:37 2011
@@ -18,9 +18,7 @@
 package org.apache.abdera2.common.templates;
 
 import java.io.Serializable;
-import java.util.ArrayList;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -36,8 +34,8 @@ import org.apache.abdera2.common.templat
 
 import com.google.common.base.Function;
 import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
 
 import static com.google.common.base.Preconditions.*;
 
@@ -109,7 +107,8 @@ public final class Template 
      * Return the array of template variables
      */
     private Iterable<Expression> initExpressions() {
-      List<Expression> expressions = new ArrayList<Expression>();
+      ImmutableList.Builder<Expression> expressions = 
+        ImmutableList.builder();
       Matcher matcher = EXPRESSION.matcher(pattern);
       while (matcher.find()) {
         String token = matcher.group();
@@ -117,14 +116,14 @@ public final class Template 
         Expression exp = new Expression(token);
         expressions.add(exp);
       }
-      return Iterables.unmodifiableIterable(expressions);
+      return expressions.build();
     }
 
     /**
      * Return the array of template variables
      */
     public Iterable<String> getVariables() {
-        return variables;
+      return variables;
     }
 
     /**
@@ -134,14 +133,14 @@ public final class Template 
      * @return An expanded URI
      */
     public String expand(Context context) {
-        String pattern = this.pattern;
-        for (Expression exp : this)
-            pattern = 
-              replace(
-                pattern, 
-                exp, 
-                exp.evaluate(context));
-        return pattern;
+      String pattern = this.pattern;
+      for (Expression exp : this)
+          pattern = 
+            replace(
+              pattern, 
+              exp, 
+              exp.evaluate(context));
+      return pattern;
     }
 
     /**

Modified: abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/TemplateManager.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/TemplateManager.java?rev=1201574&r1=1201573&r2=1201574&view=diff
==============================================================================
--- abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/TemplateManager.java
(original)
+++ abdera/abdera2/common/src/main/java/org/apache/abdera2/common/templates/TemplateManager.java
Mon Nov 14 02:15:37 2011
@@ -17,7 +17,6 @@
  */
 package org.apache.abdera2.common.templates;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -25,6 +24,8 @@ import org.apache.abdera2.common.iri.IRI
 import org.apache.abdera2.common.misc.MoreFunctions;
 
 import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableMap;
+
 import static com.google.common.base.Preconditions.*;
 import static org.apache.abdera2.common.misc.MorePreconditions.*;
 @SuppressWarnings("unchecked")
@@ -37,8 +38,8 @@ public class TemplateManager<T>
   
   public static class Builder<T> {
     
-    protected final Map<T,Template> templates = 
-      new HashMap<T,Template>();
+    protected final ImmutableMap.Builder<T,Template> templates = 
+      ImmutableMap.builder();
     protected boolean isiri;
     protected IRI base;
     protected final MultiContext.Builder defaultContexts = 
@@ -47,24 +48,18 @@ public class TemplateManager<T>
     public Builder() {}
     
     public <M extends Builder<T>>M add(T key, Template template) {
-      checkNotNull(key);
-      checkNotNull(template);
-      this.templates.put(key,template);
+      this.templates.put(checkNotNull(key),checkNotNull(template));
       return (M)this;
     }
     
     public <M extends Builder<T>>M add(T key, String template) {
-      checkNotNull(key);
-      checkNotNull(template);
-      return (M)add(key, new Template(template));
+      return (M)add(checkNotNull(key), new Template(checkNotNull(template)));
     }
     
     public <M extends Builder<T>>M add(T key, Object template) {
-      checkNotNull(template);
-      checkNotNull(key);
-      checkArgumentTypes(template,Map.class,Collection.class);
+      checkArgumentTypes(checkNotNull(template),Map.class,Collection.class);
       if (template instanceof Supplier)
-        return add(key,((Supplier<?>)template).get());
+        return add(checkNotNull(key),((Supplier<?>)template).get());
       Template temp = 
         template instanceof Template ?
           (Template)template :
@@ -73,8 +68,7 @@ public class TemplateManager<T>
     }
     
     public <M extends Builder<T>>M add(Map<T,Object> templates) {
-      checkNotNull(templates);
-      for (Map.Entry<T,Object> entry : templates.entrySet())
+      for (Map.Entry<T,Object> entry : checkNotNull(templates).entrySet())
         add(entry.getKey(),entry.getValue());
       return (M)this;
     }
@@ -85,58 +79,51 @@ public class TemplateManager<T>
     }
     
     public <M extends Builder<T>>M withDefaults(Context context) {
-      checkNotNull(context);
-      this.defaultContexts.with(context);
+      this.defaultContexts.with(checkNotNull(context));
       return (M)this;
     }
     
     public <M extends Builder<T>>M withDefaults(MapContext context) {
-      checkNotNull(context);
-      this.defaultContexts.with(context);
+      this.defaultContexts.with(checkNotNull(context));
       return (M)this;
     }
     
     public <M extends Builder<T>>M withDefaults(Map<String,Object> map)
{
-      checkNotNull(map);
-      this.defaultContexts.with(new MapContext(map));
+      this.defaultContexts.with(new MapContext(checkNotNull(map)));
       return (M)this;
     }
     
     public <M extends Builder<T>>M withDefaults(Object context) {
-      checkNotNull(context);
-      this.defaultContexts.with(new ObjectContext(context));
+      this.defaultContexts.with(new ObjectContext(checkNotNull(context)));
       return (M)this;
     }
     
-    public <M extends Builder<T>>M withBase(IRI iri) {
-      checkNotNull(iri);
-      this.base = iri;
+    public <M extends Builder<T>>M withBase(IRI iri) {;
+      this.base = checkNotNull(iri);
       return (M)this;
     }
     
     public <M extends Builder<T>>M withBase(String iri) {
-      checkNotNull(iri);
-      return (M)withBase(new IRI(iri));
+      return (M)withBase(new IRI(checkNotNull(iri)));
     }
     
     public TemplateManager<T> get() {
       return new TemplateManager<T>(
-        templates,isiri,base,defaultContexts.get());
+        templates.build(),isiri,base,defaultContexts.get());
     }
   }
   
-  private final Map<T,Template> templates = 
-    new HashMap<T,Template>();
+  private final ImmutableMap<T,Template> templates;
   private final boolean isiri;
   private final IRI base;
   private final Context contextDefaults;
 
   protected TemplateManager(
-    Map<T,Template> templates, 
+    ImmutableMap<T,Template> templates, 
     boolean isiri, 
     IRI base, 
     Context contextDefaults) {
-      this.templates.putAll(templates);
+      this.templates = templates;
       this.isiri = isiri;
       this.base = base;
       this.contextDefaults = contextDefaults;



Mime
View raw message