commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r758061 - in /commons/sandbox/flatfile/trunk/src/main: antlr/EntityTreeParser.g java/org/apache/commons/flatfile/dsl/ParserEntityFactory.java
Date Tue, 24 Mar 2009 22:17:18 GMT
Author: mbenson
Date: Tue Mar 24 22:17:18 2009
New Revision: 758061

URL: http://svn.apache.org/viewvc?rev=758061&view=rev
Log:
address some TODOs

Modified:
    commons/sandbox/flatfile/trunk/src/main/antlr/EntityTreeParser.g
    commons/sandbox/flatfile/trunk/src/main/java/org/apache/commons/flatfile/dsl/ParserEntityFactory.java

Modified: commons/sandbox/flatfile/trunk/src/main/antlr/EntityTreeParser.g
URL: http://svn.apache.org/viewvc/commons/sandbox/flatfile/trunk/src/main/antlr/EntityTreeParser.g?rev=758061&r1=758060&r2=758061&view=diff
==============================================================================
--- commons/sandbox/flatfile/trunk/src/main/antlr/EntityTreeParser.g (original)
+++ commons/sandbox/flatfile/trunk/src/main/antlr/EntityTreeParser.g Tue Mar 24 22:17:18 2009
@@ -82,7 +82,6 @@
 		}
 	}
 
-//TODO support other encodings
 	private byte[] getBytes(String s) {
 		try {
 			return s.getBytes("UTF-8");

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=758061&r1=758060&r2=758061&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
Tue Mar 24 22:17:18 2009
@@ -46,6 +46,33 @@
 
     private static final Log LOG = LogFactory.getLog(ParserEntityFactory.class);
     private static final EntityNameStrategy DEFAULT_NAME_STRATEGY = new DefaultEntityNameStrategy();
+
+    private class EntityHolder {
+        private EntityDefinition definition;
+        private Entity prototype;
+
+        /**
+         * Create a new EntityHolder instance.
+         * @param definition base
+         */
+        EntityHolder(EntityDefinition definition) {
+            this.definition = definition;
+        }
+
+        /**
+         * Get the prototypical entity from this {@link EntityHolder}.
+         * @return Entity
+         */
+        Entity getPrototype() {
+            synchronized (this) {
+                if (prototype == null) {
+                    prototype = getTreeParser().createEntity(definition);
+                }
+            }
+            return prototype;
+        }
+    }
+
     private EntityNameStrategy entityNameStrategy;
 
     private ThreadLocal<EntityTreeParser> treeParser = new ThreadLocal<EntityTreeParser>()
{
@@ -56,9 +83,8 @@
         }
     };
 
-    private Map<String, Object> entityMap;
-    private Map<String, Map<String, ? extends Object>> defaultOptionMaps
-            = new HashMap<String, Map<String, ? extends Object>>();
+    private Map<String, EntityHolder> entityMap;
+    private Map<String, Map<String, ? extends Object>> defaultOptionMaps = new
HashMap<String, Map<String, ? extends Object>>();
     private boolean checked;
     private InputStream[] sources;
     private EntityFactory parent;
@@ -89,26 +115,11 @@
      * {@inheritDoc}
      */
     public final Entity getEntity(Object cue) {
-        if (parent != null) {
-            Entity e = parent.getEntity(cue);
-            if (e != null) {
-                return e;
-            }
+        EntityHolder holder = getEntityMap().get(getEntityNameStrategy().getEntityName(cue));
+        if (holder != null) {
+            return holder.getPrototype().clone();
         }
-        Object o = getEntityMap().get(getEntityNameStrategy().getEntityName(cue));
-        if (o == null) {
-            return null;
-        }
-        Entity prototype = null;
-        if (o instanceof Entity) {
-            prototype = (Entity) o;
-        } else {
-            EntityDefinition def = (EntityDefinition) o;
-            EntityTreeParser tp = getTreeParser();
-            prototype = tp.createEntity(def);
-            entityMap.put(def.getName(), prototype);
-        }
-        return prototype.clone();
+        return getParent() == null ? null : getParent().getEntity(cue);
     }
 
     /**
@@ -185,7 +196,7 @@
      * @param def to add
      */
     void add(String name, EntityDefinition def) {
-        entityMap.put(name, def);
+        entityMap.put(name, new EntityHolder(def));
     }
 
     /**
@@ -209,9 +220,6 @@
         return defaultOptionMaps.get(type);
     }
 
-    // TODO make field creation pluggable
-    // TODO tie default options to class rather than mnemonic
-
     /**
      * Create a Field of a particular length, applying default options.
      * @param length of field
@@ -258,14 +266,13 @@
         return treeParser.get();
     }
 
-    // TODO come up with a single wrapper to get the Entity for the value.
     /**
      * Return our entity map, initializing if necessary.
-     * @return Map<String, Object> - value may be an EntityDefinition or an Entity.
+     * @return Map<String, EntityProxy> - value may be an EntityDefinition or an Entity.
      */
-    private synchronized Map<String, Object> getEntityMap() {
+    private synchronized Map<String, EntityHolder> getEntityMap() {
         if (entityMap == null) {
-            entityMap = new HashMap<String, Object>();
+            entityMap = new HashMap<String, EntityHolder>();
             try {
                 EntityParser p = null;
                 ArrayList<AST> trees = new ArrayList<AST>();



Mime
View raw message