commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1766123 - in /commons/sandbox/flatfile/trunk: pom.xml src/main/java/org/apache/commons/flatfile/EntityMap.java src/main/java/org/apache/commons/flatfile/dsl/ParserEntityFactory.java
Date Fri, 21 Oct 2016 20:40:45 GMT
Author: mbenson
Date: Fri Oct 21 20:40:45 2016
New Revision: 1766123

URL: http://svn.apache.org/viewvc?rev=1766123&view=rev
Log:
upgrade to commons-collections4

Modified:
    commons/sandbox/flatfile/trunk/pom.xml
    commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntityMap.java
    commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/dsl/ParserEntityFactory.java

Modified: commons/sandbox/flatfile/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/flatfile/trunk/pom.xml?rev=1766123&r1=1766122&r2=1766123&view=diff
==============================================================================
--- commons/sandbox/flatfile/trunk/pom.xml (original)
+++ commons/sandbox/flatfile/trunk/pom.xml Fri Oct 21 20:40:45 2016
@@ -82,16 +82,16 @@
       <artifactId>commons-lang3</artifactId>
       <version>3.1</version>
     </dependency>
-    <dependency>
-      <groupId>commons-collections</groupId>
-      <artifactId>commons-collections</artifactId>
-      <version>3.2.2</version>
-    </dependency>
-    <dependency>
+  	<dependency>
+  		<groupId>org.apache.commons</groupId>
+  		<artifactId>commons-collections4</artifactId>
+  		<version>4.0</version>
+  	</dependency>
+  	<dependency>
       <groupId>commons-pool</groupId>
       <artifactId>commons-pool</artifactId>
       <version>1.6</version>
-      <optional>true</optional> 
+      <optional>true</optional>
     </dependency>
     <dependency>
       <groupId>net.sf.morph</groupId>

Modified: commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntityMap.java
URL: http://svn.apache.org/viewvc/commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntityMap.java?rev=1766123&r1=1766122&r2=1766123&view=diff
==============================================================================
--- commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntityMap.java
(original)
+++ commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/EntityMap.java
Fri Oct 21 20:40:45 2016
@@ -17,6 +17,7 @@
 package org.apache.commons.flatfile;
 
 import java.io.Serializable;
+import java.util.AbstractList;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -25,10 +26,8 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.commons.collections.IteratorUtils;
-import org.apache.commons.collections.Transformer;
-import org.apache.commons.collections.list.AbstractSerializableListDecorator;
-
+import org.apache.commons.collections4.IteratorUtils;
+import org.apache.commons.collections4.Transformer;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.Validate;
 
@@ -45,8 +44,8 @@ public class EntityMap extends EntityCol
         /** Serialization version */
         private static final long serialVersionUID = 2347729609002856564L;
 
-        private String name;
-        private Entity entity;
+        private final String name;
+        private final Entity entity;
 
         /**
          * Create a new Child instance.
@@ -64,38 +63,46 @@ public class EntityMap extends EntityCol
     }
 
     /**
-     * Expose the Child list as List<Entity>.
+     * Expose the Child list as List<Entity>. This is necessary because we permit nameless
children,
+     * which we want to account for in a layout but have no need of referencing later.
      */
-    // TODO switch to functor for full generics
-    @SuppressWarnings("unchecked")
-    private class ChildrenList extends AbstractSerializableListDecorator {
-        private static final long serialVersionUID = 3241482030276238909L;
-
-        private Transformer t = new Transformer() {
-            public Object transform(Object o) {
-                return ((Child) o).entity;
+    private static class ChildrenList extends AbstractList<Entity> implements Serializable
{
+
+        private static final long serialVersionUID = -954784669450571284L;
+
+        private static final Transformer<Child, Entity> CHILD_TO_ENTITY = new Transformer<Child,
Entity>() {
+
+            public Entity transform(Child input) {
+                return input.entity;
             }
         };
 
-        /**
-         * Create our ChildrenList instance.
-         */
-        private ChildrenList() {
-            super(children);
+        private final List<Child> wrapped;
+
+        private ChildrenList(List<Child> wrapped) {
+            super();
+            this.wrapped = wrapped;
         }
 
         /**
          * {@inheritDoc}
          */
+        @Override
         public Entity get(int index) {
-            return (Entity) t.transform(super.get(index));
+            return CHILD_TO_ENTITY.transform(wrapped.get(index));
         }
 
         /**
          * {@inheritDoc}
          */
+        @Override
         public Iterator<Entity> iterator() {
-            return IteratorUtils.transformedIterator(super.iterator(), t);
+            return IteratorUtils.transformedIterator(wrapped.iterator(), CHILD_TO_ENTITY);
+        }
+
+        @Override
+        public int size() {
+            return wrapped.size();
         }
 
     }
@@ -113,13 +120,12 @@ public class EntityMap extends EntityCol
      * @param name if {@code null} filler element
      * @param child non-null child Entity
      */
-    @SuppressWarnings("unchecked")
     public synchronized void add(String name, Entity child) {
         Validate.notNull(child, "child entity is null");
 
         if (children == null) {
             children = new ArrayList<Child>();
-            exposeChildrenList = new ChildrenList();
+            exposeChildrenList = new ChildrenList(children);
             childMap = new LinkedHashMap<String, Entity>();
         }
         if (childMap.containsKey(name)) {
@@ -136,16 +142,15 @@ public class EntityMap extends EntityCol
      * @return Map<String, Entity>
      */
     public synchronized Map<String, Entity> getChildMap() {
-        return childMap == null ? Collections.<String, Entity> emptyMap() : Collections
-                .unmodifiableMap(childMap);
+        return childMap == null ? Collections.<String, Entity> emptyMap() : Collections.unmodifiableMap(childMap);
     }
 
     /**
      * {@inheritDoc}
      */
     public synchronized String[] getChildNames() {
-        return childMap == null ? ArrayUtils.EMPTY_STRING_ARRAY : childMap.keySet().toArray(
-                new String[childMap.size()]);
+        return childMap == null ? ArrayUtils.EMPTY_STRING_ARRAY
+            : childMap.keySet().toArray(new String[childMap.size()]);
     }
 
     /**
@@ -166,13 +171,13 @@ public class EntityMap extends EntityCol
      * {@inheritDoc}
      */
     public synchronized Collection<Entity> getChildren() {
-        return children == null ? Collections.<Entity> emptyList() : Collections
-                .unmodifiableList(exposeChildrenList);
+        return children == null ? Collections.<Entity> emptyList() : Collections.unmodifiableList(exposeChildrenList);
     }
 
     /**
      * {@inheritDoc}
      */
+    @Override
     public synchronized EntityMap clone() {
         EntityMap result = (EntityMap) super.clone();
         result.children = null;

Modified: commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/dsl/ParserEntityFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/dsl/ParserEntityFactory.java?rev=1766123&r1=1766122&r2=1766123&view=diff
==============================================================================
--- commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/dsl/ParserEntityFactory.java
(original)
+++ commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/dsl/ParserEntityFactory.java
Fri Oct 21 20:40:45 2016
@@ -21,6 +21,12 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.commons.collections4.Transformer;
+import org.apache.commons.flatfile.DynamicField;
+import org.apache.commons.flatfile.Entity;
+import org.apache.commons.flatfile.EntityFactory;
+import org.apache.commons.flatfile.Field;
+import org.apache.commons.flatfile.util.ApplyOptions;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -31,18 +37,12 @@ import org.apache.commons.pool.impl.Stac
 import antlr.TokenBuffer;
 import antlr.collections.AST;
 
-import org.apache.commons.collections.Transformer;
-import org.apache.commons.flatfile.DynamicField;
-import org.apache.commons.flatfile.Entity;
-import org.apache.commons.flatfile.EntityFactory;
-import org.apache.commons.flatfile.Field;
-import org.apache.commons.flatfile.util.ApplyOptions;
-
 /**
  * Entity factory; provides access to named entities parsed from one or more description
files.
  * @version $Revision$ $Date$
  */
 public class ParserEntityFactory implements EntityFactory {
+
     /** Field option constant */
     public static final String OPTION_FIELD = "field";
     /** DynamicField option constant */
@@ -52,7 +52,8 @@ public class ParserEntityFactory impleme
     private static final EntityNameStrategy DEFAULT_NAME_STRATEGY = new DefaultEntityNameStrategy();
 
     private class EntityHolder {
-        private EntityDefinition definition;
+
+        private final EntityDefinition definition;
         private Entity prototype;
 
         /**
@@ -70,10 +71,10 @@ public class ParserEntityFactory impleme
         Entity getPrototype() {
             synchronized (this) {
                 if (prototype == null) {
-                    prototype = (Entity) doWithPooledTreeParser(new Transformer() {
+                    prototype = doWithPooledTreeParser(new Transformer<EntityTreeParser,
Entity>() {
 
-                        public Object transform(Object arg0) {
-                            return ((EntityTreeParser) arg0).createEntity(definition);
+                        public Entity transform(EntityTreeParser input) {
+                            return input.createEntity(definition);
                         }
                     });
                 }
@@ -84,22 +85,21 @@ public class ParserEntityFactory impleme
 
     private EntityNameStrategy entityNameStrategy;
     private Map<String, EntityHolder> entityMap;
-    private Map<String, Map<String, ? extends Object>> defaultOptionMaps = new
HashMap<String, Map<String, ? extends Object>>();
+    private final Map<String, Map<String, ?>> defaultOptionMaps = new HashMap<String,
Map<String, ?>>();
     private boolean checked;
     private InputStream[] sources;
     private EntityFactory parent;
 
     private final ObjectPool<EntityTreeParser> treeParserPool =
-        new StackObjectPool<EntityTreeParser>(
-            new BasePoolableObjectFactory<EntityTreeParser>() {
+        new StackObjectPool<EntityTreeParser>(new BasePoolableObjectFactory<EntityTreeParser>()
{
 
-                @Override
-                public EntityTreeParser makeObject() throws Exception {
-                    EntityTreeParser result = new EntityTreeParser();
-                    result.setEntityFactory(ParserEntityFactory.this);
-                    return result;
-                }
-            }, 1);
+            @Override
+            public EntityTreeParser makeObject() throws Exception {
+                EntityTreeParser result = new EntityTreeParser();
+                result.setEntityFactory(ParserEntityFactory.this);
+                return result;
+            }
+        }, 1);
 
     /**
      * Create a new ParserEntityFactory.
@@ -281,8 +281,8 @@ public class ParserEntityFactory impleme
                 EntityParser p = null;
                 final ArrayList<AST> trees = new ArrayList<AST>();
                 InputStream[] is = getSources();
-                for (int i = 0; i < is.length; i++) {
-                    TokenBuffer tb = new TokenBuffer(new EntityLexer(is[i]));
+                for (InputStream element : is) {
+                    TokenBuffer tb = new TokenBuffer(new EntityLexer(element));
                     if (p == null) {
                         p = new EntityParser(tb);
                     } else {
@@ -291,36 +291,34 @@ public class ParserEntityFactory impleme
                     p.parse();
                     trees.add(p.getAST());
                 }
-                doWithPooledTreeParser(new Transformer() {
+                doWithPooledTreeParser(new Transformer<EntityTreeParser, Void>() {
 
-                    public Object transform(Object arg0) {
+                    public Void transform(EntityTreeParser input) {
                         for (AST ast : trees) {
                             try {
-                                ((EntityTreeParser) arg0).load(ast);
+                                input.load(ast);
                             } catch (Exception e) {
-                                throw e instanceof RuntimeException ? (RuntimeException)
e
-                                        : new RuntimeException(e);
+                                throw e instanceof RuntimeException ? (RuntimeException)
e : new RuntimeException(e);
                             }
                         }
                         return null;
                     }
                 });
             } catch (Exception e) {
-                throw e instanceof RuntimeException ? (RuntimeException) e
-                        : new RuntimeException(e);
+                throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
             }
         }
         return entityMap;
     }
 
-    private Object doWithPooledTreeParser(Transformer t) {
+    private <T> T doWithPooledTreeParser(Transformer<EntityTreeParser, T> xform)
{
         EntityTreeParser entityTreeParser;
         try {
             entityTreeParser = treeParserPool.borrowObject();
         } catch (Exception e) {
             throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
         }
-        Object result = t.transform(entityTreeParser);
+        T result = xform.transform(entityTreeParser);
         try {
             treeParserPool.returnObject(entityTreeParser);
         } catch (Exception e) {



Mime
View raw message