01: /* DualFunctionMapper.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Sat Sep 1 12:15:16 2007, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.xel.util;
20:
21: import java.util.Collection;
22:
23: import org.zkoss.util.DualCollection;
24: import org.zkoss.xel.FunctionMapper;
25: import org.zkoss.xel.Function;
26:
27: /**
28: * Combine two function mappers into one function mapper.
29: *
30: * @author tomyeh
31: * @since 3.0.0
32: */
33: public class DualFunctionMapper implements FunctionMapper {
34: private FunctionMapper _first, _second;
35:
36: /** Returns a function mapper by combining two function mappers.
37: * It checks whether any of them is null, or equals. And, returns
38: * the non-null one if another is null.
39: * If both null, it returns null.
40: */
41: public static final FunctionMapper combine(FunctionMapper first,
42: FunctionMapper second) {
43: if (first == second) //we don't use equals to have better performance
44: return first;
45:
46: if (first != null)
47: if (second != null)
48: return new DualFunctionMapper(first, second);
49: else
50: return first;
51: else
52: return second;
53: }
54:
55: /** Constructor.
56: * It is better to use {@link #combine} instead of this method
57: * since it checks whether any of them is null or equals.
58: */
59: public DualFunctionMapper(FunctionMapper first,
60: FunctionMapper second) {
61: _first = first;
62: _second = second;
63: }
64:
65: //-- FunctionMapper --//
66: public Function resolveFunction(String prefix, String name) {
67: Function m = _first != null ? _first.resolveFunction(prefix,
68: name) : null;
69: return m != null ? m : _second != null ? _second
70: .resolveFunction(prefix, name) : null;
71: }
72:
73: public Collection getClassNames() {
74: return combine(_first != null ? _first.getClassNames() : null,
75: _second != null ? _second.getClassNames() : null);
76: }
77:
78: public Class resolveClass(String name) {
79: Class m = _first != null ? _first.resolveClass(name) : null;
80: return m != null ? m : _second != null ? _second
81: .resolveClass(name) : null;
82: }
83:
84: private static Collection combine(Collection first,
85: Collection second) {
86: return DualCollection.combine(
87: first != null && !first.isEmpty() ? first : null,
88: second != null && !second.isEmpty() ? second : null);
89: }
90: }
|