You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Apache Wiki <wi...@apache.org> on 2005/03/30 22:11:24 UTC

[Jakarta-velocity Wiki] Update of "ListTool" by ShinobuKawaiYoshida

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-velocity Wiki" for change notification.

The following page has been changed by ShinobuKawaiYoshida:
http://wiki.apache.org/jakarta-velocity/ListTool

The comment on the change is:
Separated the tool into two.

------------------------------------------------------------------------------
  Replacement of ArrayTool.
+ 
+ Separated the tool into two.
+ 
+ ListTool (basic functions for manipulating lists)
+  * get/set/size support
+  * isEmpty support
+  * contains support
+  * isList/isArray
+ 
+ Extended ListTool (extended functions that we are not sure we need)
+  * conversion between Lists and arrays
+  * clone support
+  * length support (same as size)
  
  {{{
  /*
@@ -22, +35 @@

  package org.apache.velocity.tools.generic;
  
  import java.lang.reflect.Array;
- import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Tool for working with Lists and arrays in Velocity templates.
-  * It provides a method to get and set specified elements,
+  * It provides a method to get and set specified elements.
-  * retrieve the length, and create clones of a list or an array object.
-  * Also provides a method to convert arrays into Lists and
-  * Lists into arrays.
+  * Also provides methods to perform the following actions to Lists and arrays:
+  * <ul>
+  *   <li>Check if it is empty.</li>
+  *   <li>Check if it contains a certain element.</li>
+  * </ul>
   *
   * <p><pre>
   * Example uses:
-  *  $primes                  -> new int[] {2, 3, 5, 7}
+  *  $primes                    -> new int[] {2, 3, 5, 7}
-  *  $list.length($primes)    -> 4
+  *  $list.size($primes)        -> 4
-  *  $list.get($primes, 2)    -> 5
+  *  $list.get($primes, 2)      -> 5
-  *  $list.clone($primes)     -> int[] {2, 3, 5, 7}, != $primes
-  *  $list.set($primes, 2, 1) -> (primes[2] becomes 1)
+  *  $list.set($primes, 2, 1)   -> (primes[2] becomes 1)
-  *  $list.get($primes, 2)    -> 1
+  *  $list.get($primes, 2)      -> 1
-  *  $list.get($clone, 2)     -> 5
+  *  $list.isEmpty($primes)     -> false
+  *  $list.contains($primes, 7) -> true
   *
   * Example toolbox.xml config (if you want to use this with VelocityView):
   * &lt;tool&gt;
@@ -178, +192 @@

      }
  
      /**
-      * Gets the length of an array/List.
-      * It will return null under the following conditions:
-      * <ul>
-      *   <li><code>array</code> is null.</li>
-      *   <li><code>array</code> is not an array/List.</li>
-      * </ul>
-      * The result will be same as the {@link #size(Object)} method.
-      * @param array the array object.
-      * @return the length of the array.
-      * @see #size(Object)
-      */
-     public Integer length(Object array)
-     {
-         if (this.isList(array))
-         {
-             return this.size(array);
-         }
-         if (!this.isArray(array))
-         {
-             return null;
-         }
- 
-         // Thanks to Eric Fixler for this refactor.
-         return new Integer(Array.getLength(array));
-     }
- 
-     /**
       * Gets the size of a List/array.
       * It will return null under the following conditions:
       * <ul>
@@ -220, +207 @@

      {
          if (this.isArray(list))
          {
-             return this.length(list);
+             // Thanks to Eric Fixler for this refactor.
+             return new Integer(Array.getLength(list));
          }
          if (!this.isList(list))
          {
@@ -228, +216 @@

          }
  
          return new Integer(((List) list).size());
-     }
- 
-     /**
-      * Gets the clone of a List/array.
-      * It will return null under the following conditions:
-      * <ul>
-      *   <li><code>list</code> is null.</li>
-      *   <li><code>list</code> is not a List/array.</li>
-      * </ul>
-      * @param list the List/array object.
-      * @return the clone of the List/array.
-      */
-     public Object clone(Object list)
-     {
-         if (this.isList(list))
-         {
-             return new ArrayList(((List) list));
-         }
-         if (!this.isArray(list))
-         {
-             return null;
-         }
- 
-         Class type = list.getClass().getComponentType();
-         int length = Array.getLength(list);
-         Object clone = Array.newInstance(type, length);
-         System.arraycopy(list, 0, clone, 0, length);
-         return clone;
      }
  
      /**
@@ -283, +243 @@

      }
  
      /**
+      * Checks if a List/array is empty.
+      * @param list the List/array to check.
+      * @return <code>true</code> if the List/array is empty.
+      */
+     public Boolean isEmpty(Object list)
+     {
+         Integer size = this.size(list);
+         if (size == null)
+         {
+             return null;
+         }
+ 
+         return new Boolean(size.intValue() == 0);
+     }
+ 
+     /**
+      * Checks if a List/array contains a certain element.
+      * @param list the List/array to check.
+      * @param element the element to check.
+      * @return <code>true</code> if the List/array contains the element.
+      */
+     public Boolean contains(Object list, Object element)
+     {
+         if (this.isArray(list))
+         {
+             return this.arrayContains(list, element);
+         }
+         if (!this.isList(list))
+         {
+             return null;
+         }
+ 
+         return new Boolean(((List) list).contains(element));
+     }
+ 
+     /**
+      * Checks if an array contains a certain element.
+      * @param array the array to check.
+      * @param element the element to check.
+      * @return <code>true</code> if the array contains the element.
+      */
+     private Boolean arrayContains(Object array, Object element)
+     {
+         int size = this.size(array).intValue();
+ 
+         for (int index = 0; index < size; ++index)
+         {
+             if (this.equals(element, this.getFromArray(array, index)))
+             {
+                 return Boolean.TRUE;
+             }
+         }
+         return Boolean.FALSE;
+     }
+ 
+     /**
+      * Check if two objects are equal.
+      * @param what an object
+      * @param with another object.
+      * @return <code>true</code> if the two objects are equal.
+      */
+     private boolean equals(Object what, Object with)
+     {
+         if (what == null)
+         {
+             return with == null;
+         }
+ 
+         return what.equals(with);
+     }
+ 
+ }
+ }}}
+ 
+ {{{
+ /*
+  * Copyright 2003-2005 The Apache Software Foundation.
+  *
+  * Licensed under the Apache License, Version 2.0 (the "License");
+  * you may not use this file except in compliance with the License.
+  * You may obtain a copy of the License at
+  *
+  *     http://www.apache.org/licenses/LICENSE-2.0
+  *
+  * Unless required by applicable law or agreed to in writing, software
+  * distributed under the License is distributed on an "AS IS" BASIS,
+  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  * See the License for the specific language governing permissions and
+  * limitations under the License.
+  */
+ 
+ package org.apache.velocity.tools.generic;
+ 
+ import java.lang.reflect.Array;
+ import java.lang.reflect.Method;
+ import java.util.ArrayList;
+ import java.util.List;
+ 
+ /**
+  * Tool for working with Lists and arrays in Velocity templates.
+  * In addition to the features ListTool provides,
+  * it provides the function to retrieve the length,
+  * and create clones of a list or an array object.
+  * Also provides a method to convert arrays into Lists and
+  * Lists into arrays.
+  *
+  * <p><pre>
+  * Example uses:
+  *  $primes                  -> new int[] {2, 3, 5, 7}
+  *  $list.length($primes)    -> 4
+  *  $list.get($primes, 2)    -> 5
+  *  $list.clone($primes)     -> int[] {2, 3, 5, 7}, != $primes
+  *  $list.set($primes, 2, 1) -> (primes[2] becomes 1)
+  *  $list.get($primes, 2)    -> 1
+  *  $list.get($clone, 2)     -> 5
+  *
+  * Example toolbox.xml config (if you want to use this with VelocityView):
+  * &lt;tool&gt;
+  *   &lt;key&gt;list&lt;/key&gt;
+  *   &lt;scope&gt;application&lt;/scope&gt;
+  *   &lt;class&gt;org.apache.velocity.tools.generic.ExtendedListTool&lt;/class&gt;
+  * &lt;/tool&gt;
+  * </pre></p>
+  *
+  * <p>This tool is entirely threadsafe, and has no instance members.
+  * It may be used in any scope (request, session, or application).
+  * </p>
+  *
+  * @author <a href="mailto:shinobu@ieee.org">Shinobu Kawai</a>
+  * @version $Id: $
+  */
+ public class ExtendedListTool extends ListTool
+ {
+ 
+     /**
+      * Default constructor.
+      */
+     public ExtendedListTool()
+     {
+     }
+ 
+     /**
       * Converts an array object into a List.
       * <ul>
       *   <li>
@@ -304, +406 @@

       * @param array an array object.
       * @return the converted java.util.List.
       */
-     public List list(Object array)
+     public List toList(Object array)
      {
          if (this.isList(array)) {
              return (List) array;
@@ -343, +445 @@

       * @param list a List object.
       * @return the converted array.
       */
-     public Object array(Object list)
+     public Object toArray(Object list)
      {
          if (this.isArray(list))
          {
@@ -358, +460 @@

          return asList.toArray(new Object[asList.size()]);
      }
  
+     /**
+      * Gets the length of an array/List.
+      * It will return null under the following conditions:
+      * <ul>
+      *   <li><code>array</code> is null.</li>
+      *   <li><code>array</code> is not an array/List.</li>
+      * </ul>
+      * The result will be same as the {@link #size(Object)} method.
+      * @param array the array object.
+      * @return the length of the array.
+      * @see #size(Object)
+      */
+     public Integer length(Object array)
+     {
+         if (this.isList(array))
+         {
+             return this.size(array);
+         }
+         if (!this.isArray(array))
+         {
+             return null;
+         }
+ 
+         // Thanks to Eric Fixler for this refactor.
+         return new Integer(Array.getLength(array));
+     }
+ 
+     /**
+      * Gets the clone of a List/array.
+      * It will return null under the following conditions:
+      * <ul>
+      *   <li><code>list</code> is null.</li>
+      *   <li><code>list</code> is not a List/array.</li>
+      * </ul>
+      * @param list the List/array object.
+      * @return the clone of the List/array.
+      */
+     public Object clone(Object list)
+     {
+         if (this.isArray(list))
+         {
+             Class type = list.getClass().getComponentType();
+             int length = Array.getLength(list);
+             Object clone = Array.newInstance(type, length);
+             System.arraycopy(list, 0, clone, 0, length);
+             return clone;
+         }
+         if (!this.isList(list))
+         {
+             return null;
+         }
+ 
+         // first, try the clone() method.
+         Class clazz = list.getClass();
+         try
+         {
+             Method cloneMethod = clazz.getMethod("clone", new Class[0]);
+             return cloneMethod.invoke(list, null);
+         }
+         catch (Exception ignoreAndTryTheNextStep)
+         {
+         }
+ 
+         // try to copy to a new instance.
+         try
+         {
+             List clone = (List) clazz.newInstance();
+             clone.addAll(((List) list));
+             return clone;
+         }
+         catch (Exception ignoreAndTryTheNextStep)
+         {
+         }
+ 
+         // last resort.
+         return new ArrayList(((List) list));
+     }
+ 
  }
  }}}