My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.vineetmanohar.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* Extend this class to implement a server side method that takes arguments that
* you would like to call via expression language.
*
* @author Vineet Manohar
*/
public abstract class ELMethod extends HashMap<Object, Object> {
private static final long serialVersionUID = 1L;

private final int numArgs;

/**
* @param numArgs
* number of arguments this method takes
*/
protected ELMethod(int numArgs) {
this.numArgs = numArgs;
}

@Override
public Object get(Object key) {
// if exactly one argument, call the result() method
if (numArgs == 1) {
return result(new Object[] {key});
}

// if more tha one argument
return new Arg(this, key);
}

public int getNumArgs() {
return numArgs;
}

/**
* 1) Implement this method in the child class. This method becomes
* accesible via expression language.
*
* 2) Call this method using map syntax, by treating the instance of the
* child class as a map of map of maps...
*
* For example, you could extends this class and create a class called
* FormatDate. In that class, the result method would expect 2 arguments,
* format string and date object.
*
* ${FormatDate["MMM dd"][user.creationDate]}, where dateFormat is an
* instance of the child class.
*
* @param args
* @return
*/
public abstract Object result(Object[] args);

public static class Arg extends HashMap<Object, Object> {
private static final long serialVersionUID = 1L;
private List<Object> args = new ArrayList<Object>();
private ELMethod parent;

public Arg(ELMethod eLMethod, Object key) {
this.parent = eLMethod;
this.args.add(key);
}

@Override
public Object get(Object key) {
this.args.add(key);

// if all the arguments have been received, invoke the result method
if (args.size() == parent.getNumArgs()) {
Object retVal = parent.result(args.toArray());

return retVal;
}

return this;
}
}
}

Change log

r12 by vineet.manohar on Jul 13, 2010   Diff
added el method an example child classes
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 2049 bytes, 84 lines
Powered by Google Project Hosting