View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.struts.flow.sugar;
17  
18  import org.mozilla.javascript.*;
19  
20  import java.util.*;
21  import java.io.Serializable;
22  
23  /***
24   * Adds various function extensions to java.util.List implementations.
25   *
26   * @targetClass java.util.Collection
27   */
28  public class CollectionExtensions {
29  
30      /***
31       *  Iterates through the collection and for each element,
32       *  calls the passed function with the element as the 
33       *  parameter.
34       *
35       *  @funcParams Function func
36       *  @funcReturn void
37       *  @example list.each(function(item) { print(item) })
38       */
39      public static ExtensionFunction each(final Collection col) {
40          return new ExtensionFunction() {    
41              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
42                  Function func = (Function)args[0];
43                  Object[] param = new Object[1];
44                  
45                  for (Iterator i = col.iterator(); i.hasNext(); ) {
46                      param[0] = i.next();
47                      func.call(cx, scope, thisObj, param);
48                  }
49                  return null;
50              }
51          };
52      }
53   
54      /***
55       *  Finds the first item selected by the passed function.  The function
56       *  will receive the item and should return true or false for the 
57       *  result.
58       *
59       *  @funcParams Function func
60       *  @funcReturn Object
61       *  @example item = list.find(function(item) { return item.matches(/foo[0-9]/) })
62       */
63      public static ExtensionFunction find(final Collection col) {
64          return new ExtensionFunction() {    
65              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
66                  Function func = (Function)args[0];
67                  Object[] param = new Object[1];
68                  Object match = null;
69                  for (Iterator i = col.iterator(); i.hasNext(); ) {
70                      param[0] = i.next();
71                      match = func.call(cx, scope, thisObj, param);
72                      if (match != null) {
73                          return match;
74                      }
75                  }
76                  return null;
77              }
78          };
79      }
80      
81      /***
82       *  Finds all items selected by the passed function.  The function
83       *  will receive the item and should return true or false for the 
84       *  result.  A list of all matches will be returned.
85       *
86       *  @funcParams Function func
87       *  @funcReturn java.util.List
88       *  @example matches = list.findAll(function(item) { return item.matches(/foo[0-9]/) })
89       */
90      public static ExtensionFunction findAll(final Collection col) {
91          return new ExtensionFunction() {    
92              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
93                  Function func = (Function)args[0];
94                  Object[] param = new Object[1];
95                  ArrayList found = new ArrayList();
96                  Object match = null;
97                  for (Iterator i = col.iterator(); i.hasNext(); ) {
98                      param[0] = i.next();
99                      match = func.call(cx, scope, thisObj, param);
100                     if (match != null) {
101                         found.add(match);
102                     }
103                 }
104                 return found;
105             }
106         };
107     }
108     
109     /***
110      *  Provides the current size of the collection.  Alternative to
111      *  the size() method to be more consistent with Javascript
112      *  arrays.
113      *
114      *  @propReturn int
115      *  @example list.length
116      */
117     public static int length(Collection col, Scriptable scope) {
118         return col.size();   
119     }
120 }