freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject freemarker git commit: (More JavaDoc and some cleanup for wrapAsHashUnion)
Date Wed, 12 Sep 2018 06:09:46 GMT
Repository: freemarker
Updated Branches:
  refs/heads/2.3-gae 3d29eef5e -> b1f09561f


(More JavaDoc and some cleanup for wrapAsHashUnion)


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

Branch: refs/heads/2.3-gae
Commit: b1f09561fb8857cbbf1d569766ae1abb2808eeeb
Parents: 3d29eef
Author: ddekany <ddekany@apache.org>
Authored: Wed Sep 12 08:09:33 2018 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Wed Sep 12 08:09:33 2018 +0200

----------------------------------------------------------------------
 .../template/utility/TemplateModelUtils.java    | 56 ++++++++------------
 1 file changed, 22 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/freemarker/blob/b1f09561/src/main/java/freemarker/template/utility/TemplateModelUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/template/utility/TemplateModelUtils.java b/src/main/java/freemarker/template/utility/TemplateModelUtils.java
index a22b4a8..38851f6 100644
--- a/src/main/java/freemarker/template/utility/TemplateModelUtils.java
+++ b/src/main/java/freemarker/template/utility/TemplateModelUtils.java
@@ -138,7 +138,9 @@ public final class TemplateModelUtils {
      *            is an empty hash.
      * 
      * @return The {@link TemplateHashModel} that's the union of the objects provided. This
is a "view", that delegates
-     *         to the underlying hashes, not a copy. If all elements are
+     *         to the underlying hashes, not a copy. The object is not thread safe. If all
elements in
+     *         {@code hashLikeObjects} are {@link TemplateHashModelEx} objects (or if there
are 0 elements), then the
+     *         result will implement {@link TemplateHashModelEx} as well.
      * 
      * @throws TemplateModelException
      *             If wrapping an element of {@code hashLikeObjects} fails with {@link TemplateModelException},
or if
@@ -184,14 +186,14 @@ public final class TemplateModelUtils {
                 : allTHMEx ? new HashExUnionModel((List) hashes)
                 : new HashUnionModel(hashes);
     }
-    
-    private static class HashUnionModel implements TemplateHashModel {
-        private final List<? extends TemplateHashModel> hashes;
 
-        HashUnionModel(List<? extends TemplateHashModel> hashes) {
+    private static abstract class AbstractHashUnionModel<T extends TemplateHashModel>
implements TemplateHashModel {
+        protected final List<? extends T> hashes;
+
+        public AbstractHashUnionModel(List<? extends T> hashes) {
             this.hashes = hashes;
         }
-        
+
         public TemplateModel get(String key) throws TemplateModelException {
             for (int i = hashes.size() - 1; i >= 0; i--) {
                 TemplateModel value = hashes.get(i).get(key);
@@ -210,35 +212,23 @@ public final class TemplateModelUtils {
             }
             return true;
         }
+        
+    }
+    
+    private static class HashUnionModel extends AbstractHashUnionModel<TemplateHashModel>
{
+        HashUnionModel(List<? extends TemplateHashModel> hashes) {
+            super(hashes);
+        }
     }
 
-    private static final class HashExUnionModel implements TemplateHashModelEx {
-        private final List<? extends TemplateHashModelEx> hashes;
+    private static final class HashExUnionModel extends AbstractHashUnionModel<TemplateHashModelEx>
+            implements TemplateHashModelEx {
         private CollectionAndSequence keys;
         private CollectionAndSequence values;
         private int size;
 
         private HashExUnionModel(List<? extends TemplateHashModelEx> hashes) {
-            this.hashes = hashes;
-        }
-        
-        public TemplateModel get(String key) throws TemplateModelException {
-            for (int i = hashes.size() - 1; i >= 0; i--) {
-                TemplateModel value = hashes.get(i).get(key);
-                if (value != null) {
-                    return value;
-                }
-            }
-            return null;
-        }
-
-        public boolean isEmpty() throws TemplateModelException {
-            for (TemplateHashModel hash : hashes) {
-                if (!hash.isEmpty()) {
-                    return false;
-                }
-            }
-            return true;
+            super(hashes);
         }
         
         public int size() throws TemplateModelException {
@@ -246,8 +236,7 @@ public final class TemplateModelUtils {
             return size;
         }
 
-        public TemplateCollectionModel keys()
-        throws TemplateModelException {
+        public TemplateCollectionModel keys() throws TemplateModelException {
             initKeys();
             return keys;
         }
@@ -259,7 +248,7 @@ public final class TemplateModelUtils {
 
         private void initKeys() throws TemplateModelException {
             if (keys == null) {
-                Set keySet = new HashSet();
+                Set<String> keySet = new HashSet<String>();
                 SimpleSequence keySeq = new SimpleSequence((ObjectWrapper) null);
                 for (TemplateHashModelEx hash : hashes) {
                     addKeys(keySet, keySeq, hash);
@@ -269,7 +258,7 @@ public final class TemplateModelUtils {
             }
         }
 
-        private static void addKeys(Set keySet, SimpleSequence keySeq, TemplateHashModelEx
hash)
+        private static void addKeys(Set<String> keySet, SimpleSequence keySeq, TemplateHashModelEx
hash)
                 throws TemplateModelException {
             TemplateModelIterator it = hash.keys().iterator();
             while (it.hasNext()) {
@@ -282,8 +271,7 @@ public final class TemplateModelUtils {
             }
         }        
 
-        private void initValues()
-        throws TemplateModelException {
+        private void initValues() throws TemplateModelException {
             if (values == null) {
                 SimpleSequence seq = new SimpleSequence(size(), null);
                 // Note: size() invokes initKeys() if needed.


Mime
View raw message