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   * @targetClass java.util.List
26   */
27  public class ListExtensions {
28  
29     
30       /***
31       *  Returns an immutable version of this list.
32       *
33       *  @funcParams 
34       *  @funcReturn java.util.List
35       *  @example frozenList = list.asImmutable()
36       */
37      public static ExtensionFunction asImmutable(final List list) {
38          return new ExtensionFunction() {    
39              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
40                  return Collections.unmodifiableList(list);
41              }
42          };
43      }
44      
45       /***
46       *  Returns a synchronized version of this list.
47       *
48       *  @funcParams 
49       *  @funcReturn java.util.List
50       *  @example multiThreadList = list.asSynchronized()
51       */
52      public static ExtensionFunction asSynchronized(final List list) {
53          return new ExtensionFunction() {    
54              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
55                  return Collections.synchronizedList(list);
56              }
57          };
58      }
59      
60       /***
61       *  Pops the last item off the list.  The last item will be returned and removed
62       *  from the list.
63       *
64       *  @funcParams 
65       *  @funcReturn Object
66       *  @example lastItem = list.pop()
67       */
68      public static ExtensionFunction pop(final List list) {
69          return new ExtensionFunction() {    
70              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
71                  Object o = null;
72                  if (list.size() > 0) {
73                      o = list.get(list.size() - 1);
74                      list.remove(list.size() - 1);
75                  }
76                  return o;
77              }
78          };
79      }
80      
81       /***
82       *  Sorts the list according to the natural order.
83       *
84       *  @funcParams 
85       *  @funcReturn java.util.List
86       *  @example sortedList = list.sort()
87       */
88      public static ExtensionFunction sort(final List list) {
89          return new ExtensionFunction() {    
90              public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) {
91                  Collections.sort(list);
92                  return null;
93              }
94          };
95      }
96      
97       /***
98       *  Sorts the list using the passed function to determine order.  The function will receive
99       *  two parameters, and should return > 0 if the first is greater, < 0 if the first
100      *  is less, and 0 if equal.
101      *
102      *  @funcParams Function func
103      *  @funcReturn java.util.List
104      *  @example sortedList = list.sort(function(val1, val2) { return val1.compareTo(val2) })
105      */
106     public static ExtensionFunction sortEach(final List list) {
107         return new ExtensionFunction() {    
108             public Object execute(final Context cx, final Scriptable scope, final Scriptable thisObj, java.lang.Object[] args) {
109                 final Object[] params = new Object[2];
110                 final Function func = (Function)args[0];
111                 Comparator comp = new Comparator() {
112                     public int compare(Object o1, Object o2) {
113                         params[0] = o1;
114                         params[1] = o2;
115                         Object result = func.call(cx, scope, thisObj,params);
116                         if (result instanceof Number) {
117                             return ((Number)result).intValue();
118                         } else {
119                             throw new RuntimeException("Invalid sorting function - should return a number.  Returned "+result);
120                         }
121                     }
122                     
123                     public boolean equals(Object o) {return false;}
124                 };
125                 
126                 Collections.sort(list, comp);
127                 return list;
128             }
129         };
130     }
131 }