Constants.EMPTY_HASH now implements TemplateHashModelEx2, so it can be listed 
with <#list ... as k, v>. Earlier it was only a TemplateHashModelEx. Added 
Constants.EMPTY_KEY_VALUE_PAIR_ITERATOR.


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

Branch: refs/heads/2.3
Commit: 507ba1f4b73da433c3f3aa0176211fcc21713a42
Parents: c3672d9
Author: ddekany <ddek...@apache.org>
Authored: Sun Jul 23 11:18:34 2017 +0200
Committer: ddekany <ddek...@apache.org>
Committed: Sun Jul 23 11:18:34 2017 +0200

----------------------------------------------------------------------
 .../freemarker/template/utility/Constants.java  | 36 +++++++++++++++++++-
 src/manual/en_US/book.xml                       | 12 +++++++
 .../template/utility/ConstantsTest.java         | 21 ++++++++++++
 3 files changed, 68 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/507ba1f4/src/main/java/freemarker/template/utility/Constants.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/template/utility/Constants.java 
b/src/main/java/freemarker/template/utility/Constants.java
index ac948a9..fd48e70 100644
--- a/src/main/java/freemarker/template/utility/Constants.java
+++ b/src/main/java/freemarker/template/utility/Constants.java
@@ -20,11 +20,14 @@
 package freemarker.template.utility;
 
 import java.io.Serializable;
+import java.util.NoSuchElementException;
 
 import freemarker.template.SimpleNumber;
 import freemarker.template.TemplateBooleanModel;
 import freemarker.template.TemplateCollectionModel;
 import freemarker.template.TemplateHashModelEx;
+import freemarker.template.TemplateHashModelEx2;
+import freemarker.template.TemplateHashModelEx2.KeyValuePairIterator;
 import freemarker.template.TemplateModel;
 import freemarker.template.TemplateModelException;
 import freemarker.template.TemplateModelIterator;
@@ -95,7 +98,11 @@ public class Constants {
     
     public static final TemplateHashModelEx EMPTY_HASH = new EmptyHashModel();
     
-    private static class EmptyHashModel implements TemplateHashModelEx, 
Serializable {
+    /**
+     * An empty hash. Since 2.3.27, it implements {@link 
TemplateHashModelEx2}, before that it was only
+     * {@link TemplateHashModelEx}.
+     */
+    private static class EmptyHashModel implements TemplateHashModelEx2, 
Serializable {
         
         public int size() throws TemplateModelException {
             return 0;
@@ -116,7 +123,34 @@ public class Constants {
         public boolean isEmpty() throws TemplateModelException {
             return true;
         }
+
+        public KeyValuePairIterator keyValuePairIterator() throws 
TemplateModelException {
+            return EMPTY_KEY_VALUE_PAIR_ITERATOR;
+        }
         
     }
     
+    /**
+     * @since 2.3.27
+     */
+    public static final KeyValuePairIterator EMPTY_KEY_VALUE_PAIR_ITERATOR = 
EmptyKeyValuePairIterator.INSTANCE;
+    
+    private static class EmptyKeyValuePairIterator implements 
TemplateHashModelEx2.KeyValuePairIterator {
+
+        static final EmptyKeyValuePairIterator INSTANCE = new 
EmptyKeyValuePairIterator();
+
+        private EmptyKeyValuePairIterator() {
+            //
+        }
+
+        public boolean hasNext() throws TemplateModelException {
+            return false;
+        }
+
+        public TemplateHashModelEx2.KeyValuePair next() throws 
TemplateModelException {
+            throw new NoSuchElementException("Can't retrieve element from 
empty key-value pair iterator.");
+        }
+
+    }    
+    
 }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/507ba1f4/src/manual/en_US/book.xml
----------------------------------------------------------------------
diff --git a/src/manual/en_US/book.xml b/src/manual/en_US/book.xml
index 3897385..e072d1a 100644
--- a/src/manual/en_US/book.xml
+++ b/src/manual/en_US/book.xml
@@ -26966,6 +26966,18 @@ TemplateModel x = env.getVariable("x");  // get 
variable x</programlisting>
               
<literal>output_format=freemarker.core.XHTMLOutputFormat()</literal>
               did).</para>
             </listitem>
+
+            <listitem>
+              <para><literal>Constants.EMPTY_HASH</literal> now implements
+              <literal>TemplateHashModelEx2</literal>, so it can be listed
+              with <literal>&lt;#list ... as k, v&gt;</literal>. Earlier it
+              was only a <literal>TemplateHashModelEx</literal>.</para>
+            </listitem>
+
+            <listitem>
+              <para>Added
+              <literal>Constants.EMPTY_KEY_VALUE_PAIR_ITERATOR</literal></para>
+            </listitem>
           </itemizedlist>
         </section>
       </section>

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/507ba1f4/src/test/java/freemarker/template/utility/ConstantsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/template/utility/ConstantsTest.java 
b/src/test/java/freemarker/template/utility/ConstantsTest.java
new file mode 100644
index 0000000..ec18114
--- /dev/null
+++ b/src/test/java/freemarker/template/utility/ConstantsTest.java
@@ -0,0 +1,21 @@
+package freemarker.template.utility;
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+import freemarker.template.TemplateException;
+import freemarker.test.TemplateTest;
+
+public final class ConstantsTest extends TemplateTest {
+    
+    @Test
+    public void testEmptyHash() throws IOException, TemplateException {
+        addToDataModel("h", Constants.EMPTY_HASH);
+        assertOutput("{<#list h as k ,v>x</#list>}", "{}"); 
+        assertOutput("{<#list h?keys as k>x</#list>}", "{}"); 
+        assertOutput("{<#list h?values as k>x</#list>}", "{}"); 
+        assertOutput("${h?size}", "0"); 
+    }
+    
+}

Reply via email to