Source Code Cross Referenced for SwitchTransformer.java in  » Library » Apache-common-Collections » org » apache » commons » collections » functors » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common Collections » org.apache.commons.collections.functors 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2001-2004,2006 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.apache.commons.collections.functors;
017:
018:        import java.io.Serializable;
019:        import java.util.Iterator;
020:        import java.util.Map;
021:
022:        import org.apache.commons.collections.Predicate;
023:        import org.apache.commons.collections.Transformer;
024:
025:        /**
026:         * Transformer implementation calls the transformer whose predicate returns true,
027:         * like a switch statement.
028:         * 
029:         * @since Commons Collections 3.0
030:         * @version $Revision: 393105 $ $Date: 2006-04-10 23:23:11 +0100 (Mon, 10 Apr 2006) $
031:         *
032:         * @author Stephen Colebourne
033:         */
034:        public class SwitchTransformer implements  Transformer, Serializable {
035:
036:            /** Serial version UID */
037:            private static final long serialVersionUID = -6404460890903469332L;
038:
039:            /** The tests to consider */
040:            private final Predicate[] iPredicates;
041:            /** The matching transformers to call */
042:            private final Transformer[] iTransformers;
043:            /** The default transformer to call if no tests match */
044:            private final Transformer iDefault;
045:
046:            /**
047:             * Factory method that performs validation and copies the parameter arrays.
048:             * 
049:             * @param predicates  array of predicates, cloned, no nulls
050:             * @param transformers  matching array of transformers, cloned, no nulls
051:             * @param defaultTransformer  the transformer to use if no match, null means return null
052:             * @return the <code>chained</code> transformer
053:             * @throws IllegalArgumentException if array is null
054:             * @throws IllegalArgumentException if any element in the array is null
055:             */
056:            public static Transformer getInstance(Predicate[] predicates,
057:                    Transformer[] transformers, Transformer defaultTransformer) {
058:                FunctorUtils.validate(predicates);
059:                FunctorUtils.validate(transformers);
060:                if (predicates.length != transformers.length) {
061:                    throw new IllegalArgumentException(
062:                            "The predicate and transformer arrays must be the same size");
063:                }
064:                if (predicates.length == 0) {
065:                    return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE
066:                            : defaultTransformer);
067:                }
068:                predicates = FunctorUtils.copy(predicates);
069:                transformers = FunctorUtils.copy(transformers);
070:                return new SwitchTransformer(predicates, transformers,
071:                        defaultTransformer);
072:            }
073:
074:            /**
075:             * Create a new Transformer that calls one of the transformers depending 
076:             * on the predicates. 
077:             * <p>
078:             * The Map consists of Predicate keys and Transformer values. A transformer 
079:             * is called if its matching predicate returns true. Each predicate is evaluated
080:             * until one returns true. If no predicates evaluate to true, the default
081:             * transformer is called. The default transformer is set in the map with a 
082:             * null key. The ordering is that of the iterator() method on the entryset 
083:             * collection of the map.
084:             * 
085:             * @param predicatesAndTransformers  a map of predicates to transformers
086:             * @return the <code>switch</code> transformer
087:             * @throws IllegalArgumentException if the map is null
088:             * @throws IllegalArgumentException if any transformer in the map is null
089:             * @throws ClassCastException  if the map elements are of the wrong type
090:             */
091:            public static Transformer getInstance(Map predicatesAndTransformers) {
092:                Transformer[] transformers = null;
093:                Predicate[] preds = null;
094:                if (predicatesAndTransformers == null) {
095:                    throw new IllegalArgumentException(
096:                            "The predicate and transformer map must not be null");
097:                }
098:                if (predicatesAndTransformers.size() == 0) {
099:                    return ConstantTransformer.NULL_INSTANCE;
100:                }
101:                // convert to array like this to guarantee iterator() ordering
102:                Transformer defaultTransformer = (Transformer) predicatesAndTransformers
103:                        .remove(null);
104:                int size = predicatesAndTransformers.size();
105:                if (size == 0) {
106:                    return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE
107:                            : defaultTransformer);
108:                }
109:                transformers = new Transformer[size];
110:                preds = new Predicate[size];
111:                int i = 0;
112:                for (Iterator it = predicatesAndTransformers.entrySet()
113:                        .iterator(); it.hasNext();) {
114:                    Map.Entry entry = (Map.Entry) it.next();
115:                    preds[i] = (Predicate) entry.getKey();
116:                    transformers[i] = (Transformer) entry.getValue();
117:                    i++;
118:                }
119:                return new SwitchTransformer(preds, transformers,
120:                        defaultTransformer);
121:            }
122:
123:            /**
124:             * Constructor that performs no validation.
125:             * Use <code>getInstance</code> if you want that.
126:             * 
127:             * @param predicates  array of predicates, not cloned, no nulls
128:             * @param transformers  matching array of transformers, not cloned, no nulls
129:             * @param defaultTransformer  the transformer to use if no match, null means return null
130:             */
131:            public SwitchTransformer(Predicate[] predicates,
132:                    Transformer[] transformers, Transformer defaultTransformer) {
133:                super ();
134:                iPredicates = predicates;
135:                iTransformers = transformers;
136:                iDefault = (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE
137:                        : defaultTransformer);
138:            }
139:
140:            /**
141:             * Transforms the input to result by calling the transformer whose matching
142:             * predicate returns true.
143:             * 
144:             * @param input  the input object to transform
145:             * @return the transformed result
146:             */
147:            public Object transform(Object input) {
148:                for (int i = 0; i < iPredicates.length; i++) {
149:                    if (iPredicates[i].evaluate(input) == true) {
150:                        return iTransformers[i].transform(input);
151:                    }
152:                }
153:                return iDefault.transform(input);
154:            }
155:
156:            /**
157:             * Gets the predicates, do not modify the array.
158:             * 
159:             * @return the predicates
160:             * @since Commons Collections 3.1
161:             */
162:            public Predicate[] getPredicates() {
163:                return iPredicates;
164:            }
165:
166:            /**
167:             * Gets the transformers, do not modify the array.
168:             * 
169:             * @return the transformers
170:             * @since Commons Collections 3.1
171:             */
172:            public Transformer[] getTransformers() {
173:                return iTransformers;
174:            }
175:
176:            /**
177:             * Gets the default transformer.
178:             * 
179:             * @return the default transformer
180:             * @since Commons Collections 3.1
181:             */
182:            public Transformer getDefaultTransformer() {
183:                return iDefault;
184:            }
185:
186:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.