freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject incubator-freemarker git commit: (Some code cleanup)
Date Sat, 21 Oct 2017 19:42:48 GMT
Repository: incubator-freemarker
Updated Branches:
  refs/heads/2.3-gae cf2ae45d1 -> d32c81faf


(Some code cleanup)


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/d32c81fa
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/d32c81fa
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/d32c81fa

Branch: refs/heads/2.3-gae
Commit: d32c81faf6fcd73f209de954b7d3e7d277ceadd0
Parents: cf2ae45
Author: ddekany <ddekany@apache.org>
Authored: Sat Oct 21 21:42:43 2017 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Sat Oct 21 21:42:43 2017 +0200

----------------------------------------------------------------------
 src/main/java/freemarker/core/Environment.java  | 64 +++++++++-----------
 .../java/freemarker/ext/beans/HashAdapter.java  |  9 +++
 .../freemarker/template/TemplateHashModel.java  |  4 +-
 .../template/TemplateHashModelEx.java           |  7 ++-
 4 files changed, 45 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d32c81fa/src/main/java/freemarker/core/Environment.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/Environment.java b/src/main/java/freemarker/core/Environment.java
index a371a6a..762f646 100644
--- a/src/main/java/freemarker/core/Environment.java
+++ b/src/main/java/freemarker/core/Environment.java
@@ -2253,52 +2253,46 @@ public final class Environment extends Configurable {
     }
 
     /**
-     * Returns the data-model (also known as the template context in some other template
engines).
+     * Returns a view of the data-model (also known as the template context in some other
template engines) 
+     * that falls back to {@linkplain Configuration#setSharedVariable(String, TemplateModel)
shared variables}.
      */
     public TemplateHashModel getDataModel() {
-        final TemplateHashModel result = new TemplateHashModel() {
-
-            public boolean isEmpty() {
-                return false;
-            }
-
-            public TemplateModel get(String key) throws TemplateModelException {
-                TemplateModel value = rootDataModel.get(key);
-                if (value == null) {
-                    value = configuration.getSharedVariable(key);
-                }
-                return value;
-            }
-        };
+        return rootDataModel instanceof TemplateHashModelEx
+                ? new TemplateHashModelEx() {
+                    public boolean isEmpty() throws TemplateModelException {
+                        return false;
+                    }
 
-        if (rootDataModel instanceof TemplateHashModelEx) {
-            return new TemplateHashModelEx() {
+                    public TemplateModel get(String key) throws TemplateModelException {
+                        TemplateModel value = rootDataModel.get(key);
+                        return value != null ? value : configuration.getSharedVariable(key);
+                    }
 
-                public boolean isEmpty() throws TemplateModelException {
-                    return result.isEmpty();
-                }
+                    // NB: The methods below do not take into account
+                    // configuration shared variables even though
+                    // the hash will return them, if only for BWC reasons
+                    public TemplateCollectionModel values() throws TemplateModelException
{
+                        return ((TemplateHashModelEx) rootDataModel).values();
+                    }
 
-                public TemplateModel get(String key) throws TemplateModelException {
-                    return result.get(key);
-                }
+                    public TemplateCollectionModel keys() throws TemplateModelException {
+                        return ((TemplateHashModelEx) rootDataModel).keys();
+                    }
 
-                // NB: The methods below do not take into account
-                // configuration shared variables even though
-                // the hash will return them, if only for BWC reasons
-                public TemplateCollectionModel values() throws TemplateModelException {
-                    return ((TemplateHashModelEx) rootDataModel).values();
+                    public int size() throws TemplateModelException {
+                        return ((TemplateHashModelEx) rootDataModel).size();
+                    }
                 }
-
-                public TemplateCollectionModel keys() throws TemplateModelException {
-                    return ((TemplateHashModelEx) rootDataModel).keys();
+            : new TemplateHashModel() {
+                public boolean isEmpty() {
+                    return false;
                 }
 
-                public int size() throws TemplateModelException {
-                    return ((TemplateHashModelEx) rootDataModel).size();
+                public TemplateModel get(String key) throws TemplateModelException {
+                    TemplateModel value = rootDataModel.get(key);
+                    return value != null ? value : configuration.getSharedVariable(key);
                 }
             };
-        }
-        return result;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d32c81fa/src/main/java/freemarker/ext/beans/HashAdapter.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/beans/HashAdapter.java b/src/main/java/freemarker/ext/beans/HashAdapter.java
index d7be930..7d20e44 100644
--- a/src/main/java/freemarker/ext/beans/HashAdapter.java
+++ b/src/main/java/freemarker/ext/beans/HashAdapter.java
@@ -59,6 +59,15 @@ public class HashAdapter extends AbstractMap implements TemplateModelAdapter
{
     }
     
     @Override
+    public int size() {
+        try {
+            return getModelEx().size();
+        } catch (TemplateModelException e) {
+            throw new UndeclaredThrowableException(e);
+        }
+    }
+    
+    @Override
     public Object get(Object key) {
         try {
             return wrapper.unwrap(model.get(String.valueOf(key)));

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d32c81fa/src/main/java/freemarker/template/TemplateHashModel.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/template/TemplateHashModel.java b/src/main/java/freemarker/template/TemplateHashModel.java
index fb6b288..fa176f1 100644
--- a/src/main/java/freemarker/template/TemplateHashModel.java
+++ b/src/main/java/freemarker/template/TemplateHashModel.java
@@ -21,9 +21,9 @@ package freemarker.template;
 
 /**
  * "hash" template language data type: an object that contains other objects accessible through
string keys
- * (sub-variable names).
+ * (sub-variable names). It, in itself, doesn't support listing the keys or values ({@link
TemplateHashModelEx} does).
  * 
- * <p>In templates they are used like {@code myHash.myKey} or {@code myHash[myDynamicKey]}.

+ * <p>In templates they are used like {@code myHash.myKey} or {@code myHash[myDynamicKey]}.
  */
 public interface TemplateHashModel extends TemplateModel {
     

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/d32c81fa/src/main/java/freemarker/template/TemplateHashModelEx.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/template/TemplateHashModelEx.java b/src/main/java/freemarker/template/TemplateHashModelEx.java
index 294b28f..7eca328 100644
--- a/src/main/java/freemarker/template/TemplateHashModelEx.java
+++ b/src/main/java/freemarker/template/TemplateHashModelEx.java
@@ -21,10 +21,13 @@ package freemarker.template;
 
 /**
  * "extended hash" template language data type; extends {@link TemplateHashModel} by allowing
- * iterating through its keys and values.
+ * iterating through its keys and values. Consider implementing the modern variation of this,
+ * {@link TemplateHashModelEx2}, which allows the more efficient listing of key-value pairs.
  * 
  * <p>In templates they are used like hashes, but these will also work (among others):
- * {@code myExtHash?size}, {@code myExtHash?keys}, {@code myExtHash?values}.
+ * {@code myExtHash?size}, {@code myExtHash?keys}, {@code myExtHash?values}, {@code <#list
myMap as k, v>}.
+ * 
+ * @see DefaultMapAdapter
  * @see SimpleHash
  */
 public interface TemplateHashModelEx extends TemplateHashModel {


Mime
View raw message