freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [2/9] incubator-freemarker git commit: (Some internal code cleanup)
Date Sun, 17 Sep 2017 10:56:40 GMT
(Some internal 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/78d4a6e2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/78d4a6e2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/78d4a6e2

Branch: refs/heads/2.3
Commit: 78d4a6e2690bcd476629f9e0d0045da752f27593
Parents: b185b98
Author: ddekany <ddekany@apache.org>
Authored: Fri Sep 1 22:18:29 2017 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Fri Sep 1 22:18:29 2017 +0200

----------------------------------------------------------------------
 .../freemarker/core/BuiltInsForSequences.java   | 62 ++++++++++----------
 .../freemarker/core/DefaultToExpression.java    | 16 +++--
 src/main/java/freemarker/core/Environment.java  | 10 +++-
 .../java/freemarker/ext/beans/BeansWrapper.java |  4 +-
 .../java/freemarker/ext/dom/ElementModel.java   |  3 +-
 .../java/freemarker/ext/dom/NodeListModel.java  | 17 +++---
 .../freemarker/template/utility/DeepUnwrap.java |  5 +-
 7 files changed, 64 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/78d4a6e2/src/main/java/freemarker/core/BuiltInsForSequences.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/BuiltInsForSequences.java b/src/main/java/freemarker/core/BuiltInsForSequences.java
index eca5fec..8430133 100644
--- a/src/main/java/freemarker/core/BuiltInsForSequences.java
+++ b/src/main/java/freemarker/core/BuiltInsForSequences.java
@@ -251,10 +251,11 @@ class BuiltInsForSequences {
         @Override
         TemplateModel calculateResult(TemplateSequenceModel tsm)
         throws TemplateModelException {
-            if (tsm.size() == 0) {
+            int size = tsm.size();
+            if (size == 0) {
                 return null;
             }
-            return tsm.get(tsm.size() - 1);
+            return tsm.get(size - 1);
         }
     }
 
@@ -388,39 +389,36 @@ class BuiltInsForSequences {
                 int argCnt = args.size();
                 checkMethodArgCount(argCnt, 1, 2);
                 
-                TemplateModel target = (TemplateModel) args.get(0);
+                TemplateModel searched = (TemplateModel) args.get(0);
                 int foundAtIdx;
                 if (argCnt > 1) {
                     int startIndex = getNumberMethodArg(args, 1).intValue();
-                    // In 2.3.x only, we prefer TemplateSequenceModel for
-                    // backward compatibility:
+                    // In 2.3, we prefer TemplateSequenceModel for backward compatibility,
even if startIndex is 0:
                     foundAtIdx = m_seq != null
-                            ? findInSeq(target, startIndex)
-                            : findInCol(target, startIndex);
+                            ? findInSeq(searched, startIndex)
+                            : findInCol(searched, startIndex);
                 } else {
-                    // In 2.3.x only, we prefer TemplateSequenceModel for
-                    // backward compatibility:
                     foundAtIdx = m_seq != null
-                            ? findInSeq(target)
-                            : findInCol(target);
+                            ? findInSeq(searched)
+                            : findInCol(searched);
                 }
                 return foundAtIdx == -1 ? Constants.MINUS_ONE : new SimpleNumber(foundAtIdx);
             }
             
-            int findInCol(TemplateModel target) throws TemplateModelException {
-                return findInCol(target, 0, Integer.MAX_VALUE);
+            int findInCol(TemplateModel searched) throws TemplateModelException {
+                return findInCol(searched, 0, Integer.MAX_VALUE);
             }
             
-            protected int findInCol(TemplateModel target, int startIndex)
+            protected int findInCol(TemplateModel searched, int startIndex)
                     throws TemplateModelException {
-                if (m_dir == 1) {
-                    return findInCol(target, startIndex, Integer.MAX_VALUE);
+                if (findFirst) {
+                    return findInCol(searched, startIndex, Integer.MAX_VALUE);
                 } else {
-                    return findInCol(target, 0, startIndex);
+                    return findInCol(searched, 0, startIndex);
                 }
             }
         
-            protected int findInCol(TemplateModel target,
+            protected int findInCol(TemplateModel searched,
                     final int allowedRangeStart, final int allowedRangeEnd)
                     throws TemplateModelException {
                 if (allowedRangeEnd < 0) return -1;
@@ -434,10 +432,12 @@ class BuiltInsForSequences {
                     
                     TemplateModel current = it.next();
                     if (idx >= allowedRangeStart) {
-                        if (modelsEqual(idx, current, target, m_env)) {
+                        if (modelsEqual(idx, current, searched, m_env)) {
                             foundAtIdx = idx;
-                            if (m_dir == 1) break searchItem; // "find first"
-                            // Otherwise it's "find last".
+                            // Don't stop if it's "find last".
+                            if (findFirst) {
+                                break searchItem;
+                            }
                         }
                     }
                     idx++;
@@ -445,25 +445,25 @@ class BuiltInsForSequences {
                 return foundAtIdx;
             }
 
-            int findInSeq(TemplateModel target)
+            int findInSeq(TemplateModel searched)
             throws TemplateModelException {
                 final int seqSize = m_seq.size();
                 final int actualStartIndex;
                 
-                if (m_dir == 1) {
+                if (findFirst) {
                     actualStartIndex = 0;
                 } else {
                     actualStartIndex = seqSize - 1;
                 }
             
-                return findInSeq(target, actualStartIndex, seqSize); 
+                return findInSeq(searched, actualStartIndex, seqSize); 
             }
 
-            private int findInSeq(TemplateModel target, int startIndex)
+            private int findInSeq(TemplateModel searched, int startIndex)
                     throws TemplateModelException {
                 int seqSize = m_seq.size();
                 
-                if (m_dir == 1) {
+                if (findFirst) {
                     if (startIndex >= seqSize) {
                         return -1;
                     }
@@ -479,13 +479,13 @@ class BuiltInsForSequences {
                     }
                 }
                 
-                return findInSeq(target, startIndex, seqSize); 
+                return findInSeq(searched, startIndex, seqSize); 
             }
             
             private int findInSeq(
                     TemplateModel target, int scanStartIndex, int seqSize)
                     throws TemplateModelException {
-                if (m_dir == 1) {
+                if (findFirst) {
                     for (int i = scanStartIndex; i < seqSize; i++) {
                         if (modelsEqual(i, m_seq.get(i), target, m_env)) return i;
                     }
@@ -499,10 +499,10 @@ class BuiltInsForSequences {
             
         }
 
-        private int m_dir;
+        private boolean findFirst;
 
-        seq_index_ofBI(int dir) {
-            m_dir = dir;
+        seq_index_ofBI(boolean findFirst) {
+            this.findFirst = findFirst;
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/78d4a6e2/src/main/java/freemarker/core/DefaultToExpression.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/DefaultToExpression.java b/src/main/java/freemarker/core/DefaultToExpression.java
index 1406fa6..245525d 100755
--- a/src/main/java/freemarker/core/DefaultToExpression.java
+++ b/src/main/java/freemarker/core/DefaultToExpression.java
@@ -23,18 +23,20 @@ package freemarker.core;
 import freemarker.template.SimpleCollection;
 import freemarker.template.TemplateCollectionModel;
 import freemarker.template.TemplateException;
-import freemarker.template.TemplateHashModelEx;
+import freemarker.template.TemplateHashModelEx2;
 import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
 import freemarker.template.TemplateScalarModel;
 import freemarker.template.TemplateSequenceModel;
+import freemarker.template.utility.Constants;
 
 /** {@code exp!defExp}, {@code (exp)!defExp} and the same two with {@code (exp)!}. */
 class DefaultToExpression extends Expression {
 	
     private static final TemplateCollectionModel EMPTY_COLLECTION = new SimpleCollection(new
java.util.ArrayList(0));
     
-	static private class EmptyStringAndSequence 
-	  implements TemplateScalarModel, TemplateSequenceModel, TemplateHashModelEx {
+	static private class EmptyStringAndSequenceAndHash implements TemplateScalarModel, TemplateSequenceModel,
+	        TemplateHashModelEx2 {
 		public String getAsString() {
 			return "";
 		}
@@ -56,10 +58,12 @@ class DefaultToExpression extends Expression {
 		public TemplateCollectionModel values() {
 			return EMPTY_COLLECTION;
 		}
-		
+        public KeyValuePairIterator keyValuePairIterator() throws TemplateModelException
{
+            return Constants.EMPTY_KEY_VALUE_PAIR_ITERATOR;
+        }
 	}
 	
-	static final TemplateModel EMPTY_STRING_AND_SEQUENCE = new EmptyStringAndSequence();
+	static final TemplateModel EMPTY_STRING_AND_SEQUENCE_AND_HASH = new EmptyStringAndSequenceAndHash();
 	
 	private final Expression lho, rho;
 	
@@ -85,7 +89,7 @@ class DefaultToExpression extends Expression {
 		}
 		
 		if (left != null) return left;
-		else if (rho == null) return EMPTY_STRING_AND_SEQUENCE;
+		else if (rho == null) return EMPTY_STRING_AND_SEQUENCE_AND_HASH;
 		else return rho.eval(env);
 	}
 

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/78d4a6e2/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 565dcd4..8b03afd 100644
--- a/src/main/java/freemarker/core/Environment.java
+++ b/src/main/java/freemarker/core/Environment.java
@@ -831,8 +831,11 @@ public final class Environment extends Configurable {
             }
         }
         TemplateSequenceModel children = node.getChildNodes();
-        if (children == null) return;
-        for (int i = 0; i < children.size(); i++) {
+        if (children == null) {
+            return;
+        }
+        int size = children.size();
+        for (int i = 0; i < size; i++) {
             TemplateNodeModel child = (TemplateNodeModel) children.get(i);
             if (child != null) {
                 invokeNodeHandlerFor(child, namespaces);
@@ -2359,7 +2362,8 @@ public final class Environment extends Configurable {
             throws TemplateException {
         TemplateModel result = null;
         int i;
-        for (i = startIndex; i < nodeNamespaces.size(); i++) {
+        int size = nodeNamespaces.size();
+        for (i = startIndex; i < size; i++) {
             Namespace ns = null;
             try {
                 ns = (Namespace) nodeNamespaces.get(i);

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/78d4a6e2/src/main/java/freemarker/ext/beans/BeansWrapper.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/beans/BeansWrapper.java b/src/main/java/freemarker/ext/beans/BeansWrapper.java
index ea9158d..0195234 100644
--- a/src/main/java/freemarker/ext/beans/BeansWrapper.java
+++ b/src/main/java/freemarker/ext/beans/BeansWrapper.java
@@ -1301,10 +1301,10 @@ public class BeansWrapper implements RichObjectWrapper, WriteProtectable
{
             recursionStops = new IdentityHashMap<Object, Object>();
         }
         Class<?> componentType = arrayClass.getComponentType();
-        Object array = Array.newInstance(componentType, seq.size());
+        final int size = seq.size();
+        Object array = Array.newInstance(componentType, size);
         recursionStops.put(seq, array);
         try {
-            final int size = seq.size();
             for (int i = 0; i < size; i++) {
                 final TemplateModel seqItem = seq.get(i);
                 Object val = tryUnwrapTo(seqItem, componentType, 0, recursionStops);

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/78d4a6e2/src/main/java/freemarker/ext/dom/ElementModel.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/dom/ElementModel.java b/src/main/java/freemarker/ext/dom/ElementModel.java
index 20481a8..3f140c1 100644
--- a/src/main/java/freemarker/ext/dom/ElementModel.java
+++ b/src/main/java/freemarker/ext/dom/ElementModel.java
@@ -60,7 +60,8 @@ class ElementModel extends NodeModel implements TemplateScalarModel {
         if (key.equals("*")) {
             NodeListModel ns = new NodeListModel(this);
             TemplateSequenceModel children = getChildNodes();
-            for (int i = 0; i < children.size(); i++) {
+            int size = children.size();
+            for (int i = 0; i < size; i++) {
                 NodeModel child = (NodeModel) children.get(i);
                 if (child.node.getNodeType() == Node.ELEMENT_NODE) {
                     ns.add(child);

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/78d4a6e2/src/main/java/freemarker/ext/dom/NodeListModel.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/ext/dom/NodeListModel.java b/src/main/java/freemarker/ext/dom/NodeListModel.java
index 46644d9..ce83d8c 100644
--- a/src/main/java/freemarker/ext/dom/NodeListModel.java
+++ b/src/main/java/freemarker/ext/dom/NodeListModel.java
@@ -119,7 +119,8 @@ class NodeListModel extends SimpleSequence implements TemplateHashModel,
_Unexpe
     }
     
     public TemplateModel get(String key) throws TemplateModelException {
-        if (size() == 1) {
+        int size = size();
+        if (size == 1) {
             NodeModel nm = (NodeModel) get(0);
             return nm.get(key);
         }
@@ -128,7 +129,7 @@ class NodeListModel extends SimpleSequence implements TemplateHashModel,
_Unexpe
                     || key.equals(AtAtKey.NESTED_MARKUP.getKey()) 
                     || key.equals(AtAtKey.TEXT.getKey())) {
                 StringBuilder result = new StringBuilder();
-                for (int i = 0; i < size(); i++) {
+                for (int i = 0; i < size; i++) {
                     NodeModel nm = (NodeModel) get(i);
                     TemplateScalarModel textModel = (TemplateScalarModel) nm.get(key);
                     result.append(textModel.getAsString());
@@ -139,8 +140,8 @@ class NodeListModel extends SimpleSequence implements TemplateHashModel,
_Unexpe
                 if (AtAtKey.containsKey(key)) {
                     throw new TemplateModelException(
                             "\"" + key + "\" is only applicable to a single XML node, but
it was applied on "
-                            + (size() != 0
-                                    ? size() + " XML nodes (multiple matches)."
+                            + (size != 0
+                                    ? size + " XML nodes (multiple matches)."
                                     : "an empty list of XML nodes (no matches)."));
                 } else {
                     throw new TemplateModelException("Unsupported @@ key: " + key);
@@ -152,13 +153,13 @@ class NodeListModel extends SimpleSequence implements TemplateHashModel,
_Unexpe
                         && (DomStringUtil.isXMLNameLike(key, 1)  || key.equals("@@")
|| key.equals("@*"))))
                 || key.equals("*") || key.equals("**")) {
             NodeListModel result = new NodeListModel(contextNode);
-            for (int i = 0; i < size(); i++) {
+            for (int i = 0; i < size; i++) {
                 NodeModel nm = (NodeModel) get(i);
                 if (nm instanceof ElementModel) {
                     TemplateSequenceModel tsm = (TemplateSequenceModel) ((ElementModel) nm).get(key);
                     if (tsm != null) {
-                        int size = tsm.size();
-                        for (int j = 0; j < size; j++) {
+                        int tsmSize = tsm.size();
+                        for (int j = 0; j < tsmSize; j++) {
                             result.add(tsm.get(j));
                         }
                     }
@@ -171,7 +172,7 @@ class NodeListModel extends SimpleSequence implements TemplateHashModel,
_Unexpe
         }
         XPathSupport xps = getXPathSupport();
         if (xps != null) {
-            Object context = (size() == 0) ? null : rawNodeList(); 
+            Object context = (size == 0) ? null : rawNodeList(); 
             return xps.executeQuery(context, key);
         } else {
             throw new TemplateModelException(

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/78d4a6e2/src/main/java/freemarker/template/utility/DeepUnwrap.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/template/utility/DeepUnwrap.java b/src/main/java/freemarker/template/utility/DeepUnwrap.java
index 586787f..85b032c 100644
--- a/src/main/java/freemarker/template/utility/DeepUnwrap.java
+++ b/src/main/java/freemarker/template/utility/DeepUnwrap.java
@@ -129,8 +129,9 @@ public class DeepUnwrap {
         }
         if (model instanceof TemplateSequenceModel) {
             TemplateSequenceModel seq = (TemplateSequenceModel) model;
-            ArrayList list = new ArrayList(seq.size());
-            for (int i = 0; i < seq.size(); ++i) {
+            int size = seq.size();
+            ArrayList list = new ArrayList(size);
+            for (int i = 0; i < size; ++i) {
                 list.add(unwrap(seq.get(i), nullModel, permissive));
             }
             return list;


Mime
View raw message