Source Code Cross Referenced for TransformerUtils.java in  » Library » Apache-common-Collections » org » apache » commons » collections » 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 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2002-2004 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;
017:
018:        import java.util.Collection;
019:        import java.util.Iterator;
020:        import java.util.Map;
021:
022:        import org.apache.commons.collections.functors.ChainedTransformer;
023:        import org.apache.commons.collections.functors.CloneTransformer;
024:        import org.apache.commons.collections.functors.ClosureTransformer;
025:        import org.apache.commons.collections.functors.ConstantTransformer;
026:        import org.apache.commons.collections.functors.EqualPredicate;
027:        import org.apache.commons.collections.functors.ExceptionTransformer;
028:        import org.apache.commons.collections.functors.FactoryTransformer;
029:        import org.apache.commons.collections.functors.InstantiateTransformer;
030:        import org.apache.commons.collections.functors.InvokerTransformer;
031:        import org.apache.commons.collections.functors.MapTransformer;
032:        import org.apache.commons.collections.functors.NOPTransformer;
033:        import org.apache.commons.collections.functors.PredicateTransformer;
034:        import org.apache.commons.collections.functors.StringValueTransformer;
035:        import org.apache.commons.collections.functors.SwitchTransformer;
036:
037:        /**
038:         * <code>TransformerUtils</code> provides reference implementations and 
039:         * utilities for the Transformer functor interface. The supplied transformers are:
040:         * <ul>
041:         * <li>Invoker - returns the result of a method call on the input object
042:         * <li>Clone - returns a clone of the input object
043:         * <li>Constant - always returns the same object
044:         * <li>Closure - performs a Closure and returns the input object
045:         * <li>Predicate - returns the result of the predicate as a Boolean
046:         * <li>Factory - returns a new object from a factory
047:         * <li>Chained - chains two or more transformers together
048:         * <li>Switch - calls one transformer based on one or more predicates
049:         * <li>SwitchMap - calls one transformer looked up from a Map
050:         * <li>Instantiate - the Class input object is instantiated
051:         * <li>Map - returns an object from a supplied Map
052:         * <li>Null - always returns null
053:         * <li>NOP - returns the input object, which should be immutable
054:         * <li>Exception - always throws an exception
055:         * <li>StringValue - returns a <code>java.lang.String</code> representation of the input object
056:         * </ul>
057:         * All the supplied transformers are Serializable.
058:         *
059:         * @since Commons Collections 3.0
060:         * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
061:         * 
062:         * @author Stephen Colebourne
063:         * @author James Carman
064:         */
065:        public class TransformerUtils {
066:
067:            /**
068:             * This class is not normally instantiated.
069:             */
070:            public TransformerUtils() {
071:                super ();
072:            }
073:
074:            /**
075:             * Gets a transformer that always throws an exception.
076:             * This could be useful during testing as a placeholder.
077:             * 
078:             * @see org.apache.commons.collections.functors.ExceptionTransformer
079:             * 
080:             * @return the transformer
081:             */
082:            public static Transformer exceptionTransformer() {
083:                return ExceptionTransformer.INSTANCE;
084:            }
085:
086:            /**
087:             * Gets a transformer that always returns null.
088:             * 
089:             * @see org.apache.commons.collections.functors.ConstantTransformer
090:             * 
091:             * @return the transformer
092:             */
093:            public static Transformer nullTransformer() {
094:                return ConstantTransformer.NULL_INSTANCE;
095:            }
096:
097:            /**
098:             * Gets a transformer that returns the input object.
099:             * The input object should be immutable to maintain the
100:             * contract of Transformer (although this is not checked).
101:             * 
102:             * @see org.apache.commons.collections.functors.NOPTransformer
103:             * 
104:             * @return the transformer
105:             */
106:            public static Transformer nopTransformer() {
107:                return NOPTransformer.INSTANCE;
108:            }
109:
110:            /**
111:             * Gets a transformer that returns a clone of the input
112:             * object. The input object will be cloned using one of these
113:             * techniques (in order):
114:             * <ul>
115:             * <li>public clone method
116:             * <li>public copy constructor
117:             * <li>serialization clone
118:             * <ul>
119:             * 
120:             * @see org.apache.commons.collections.functors.CloneTransformer
121:             * 
122:             * @return the transformer
123:             */
124:            public static Transformer cloneTransformer() {
125:                return CloneTransformer.INSTANCE;
126:            }
127:
128:            /**
129:             * Creates a Transformer that will return the same object each time the 
130:             * transformer is used.
131:             *
132:             * @see org.apache.commons.collections.functors.ConstantTransformer
133:             * 
134:             * @param constantToReturn  the constant object to return each time in the transformer
135:             * @return the transformer.
136:             */
137:            public static Transformer constantTransformer(
138:                    Object constantToReturn) {
139:                return ConstantTransformer.getInstance(constantToReturn);
140:            }
141:
142:            /**
143:             * Creates a Transformer that calls a Closure each time the transformer is used.
144:             * The transformer returns the input object.
145:             *
146:             * @see org.apache.commons.collections.functors.ClosureTransformer
147:             * 
148:             * @param closure  the closure to run each time in the transformer, not null
149:             * @return the transformer
150:             * @throws IllegalArgumentException if the closure is null
151:             */
152:            public static Transformer asTransformer(Closure closure) {
153:                return ClosureTransformer.getInstance(closure);
154:            }
155:
156:            /**
157:             * Creates a Transformer that calls a Predicate each time the transformer is used.
158:             * The transformer will return either Boolean.TRUE or Boolean.FALSE.
159:             *
160:             * @see org.apache.commons.collections.functors.PredicateTransformer
161:             * 
162:             * @param predicate  the predicate to run each time in the transformer, not null
163:             * @return the transformer
164:             * @throws IllegalArgumentException if the predicate is null
165:             */
166:            public static Transformer asTransformer(Predicate predicate) {
167:                return PredicateTransformer.getInstance(predicate);
168:            }
169:
170:            /**
171:             * Creates a Transformer that calls a Factory each time the transformer is used.
172:             * The transformer will return the value returned by the factory.
173:             *
174:             * @see org.apache.commons.collections.functors.FactoryTransformer
175:             * 
176:             * @param factory  the factory to run each time in the transformer, not null
177:             * @return the transformer
178:             * @throws IllegalArgumentException if the factory is null
179:             */
180:            public static Transformer asTransformer(Factory factory) {
181:                return FactoryTransformer.getInstance(factory);
182:            }
183:
184:            /**
185:             * Create a new Transformer that calls two transformers, passing the result of
186:             * the first into the second.
187:             * 
188:             * @see org.apache.commons.collections.functors.ChainedTransformer
189:             * 
190:             * @param transformer1  the first transformer
191:             * @param transformer2  the second transformer
192:             * @return the transformer
193:             * @throws IllegalArgumentException if either transformer is null
194:             */
195:            public static Transformer chainedTransformer(
196:                    Transformer transformer1, Transformer transformer2) {
197:                return ChainedTransformer.getInstance(transformer1,
198:                        transformer2);
199:            }
200:
201:            /**
202:             * Create a new Transformer that calls each transformer in turn, passing the 
203:             * result into the next transformer.
204:             * 
205:             * @see org.apache.commons.collections.functors.ChainedTransformer
206:             * 
207:             * @param transformers  an array of transformers to chain
208:             * @return the transformer
209:             * @throws IllegalArgumentException if the transformers array is null
210:             * @throws IllegalArgumentException if any transformer in the array is null
211:             */
212:            public static Transformer chainedTransformer(
213:                    Transformer[] transformers) {
214:                return ChainedTransformer.getInstance(transformers);
215:            }
216:
217:            /**
218:             * Create a new Transformer that calls each transformer in turn, passing the 
219:             * result into the next transformer. The ordering is that of the iterator()
220:             * method on the collection.
221:             * 
222:             * @see org.apache.commons.collections.functors.ChainedTransformer
223:             * 
224:             * @param transformers  a collection of transformers to chain
225:             * @return the transformer
226:             * @throws IllegalArgumentException if the transformers collection is null
227:             * @throws IllegalArgumentException if any transformer in the collection is null
228:             */
229:            public static Transformer chainedTransformer(Collection transformers) {
230:                return ChainedTransformer.getInstance(transformers);
231:            }
232:
233:            /**
234:             * Create a new Transformer that calls one of two transformers depending 
235:             * on the specified predicate.
236:             * 
237:             * @see org.apache.commons.collections.functors.SwitchTransformer
238:             * 
239:             * @param predicate  the predicate to switch on
240:             * @param trueTransformer  the transformer called if the predicate is true
241:             * @param falseTransformer  the transformer called if the predicate is false
242:             * @return the transformer
243:             * @throws IllegalArgumentException if the predicate is null
244:             * @throws IllegalArgumentException if either transformer is null
245:             */
246:            public static Transformer switchTransformer(Predicate predicate,
247:                    Transformer trueTransformer, Transformer falseTransformer) {
248:                return SwitchTransformer
249:                        .getInstance(new Predicate[] { predicate },
250:                                new Transformer[] { trueTransformer },
251:                                falseTransformer);
252:            }
253:
254:            /**
255:             * Create a new Transformer that calls one of the transformers depending 
256:             * on the predicates. The transformer at array location 0 is called if the
257:             * predicate at array location 0 returned true. Each predicate is evaluated
258:             * until one returns true. If no predicates evaluate to true, null is returned.
259:             * 
260:             * @see org.apache.commons.collections.functors.SwitchTransformer
261:             * 
262:             * @param predicates  an array of predicates to check
263:             * @param transformers  an array of transformers to call
264:             * @return the transformer
265:             * @throws IllegalArgumentException if the either array is null
266:             * @throws IllegalArgumentException if the either array has 0 elements
267:             * @throws IllegalArgumentException if any element in the arrays is null
268:             * @throws IllegalArgumentException if the arrays are different sizes
269:             */
270:            public static Transformer switchTransformer(Predicate[] predicates,
271:                    Transformer[] transformers) {
272:                return SwitchTransformer.getInstance(predicates, transformers,
273:                        null);
274:            }
275:
276:            /**
277:             * Create a new Transformer that calls one of the transformers depending 
278:             * on the predicates. The transformer at array location 0 is called if the
279:             * predicate at array location 0 returned true. Each predicate is evaluated
280:             * until one returns true. If no predicates evaluate to true, the default
281:             * transformer is called. If the default transformer is null, null is returned.
282:             * 
283:             * @see org.apache.commons.collections.functors.SwitchTransformer
284:             * 
285:             * @param predicates  an array of predicates to check
286:             * @param transformers  an array of transformers to call
287:             * @param defaultTransformer  the default to call if no predicate matches, null means return null
288:             * @return the transformer
289:             * @throws IllegalArgumentException if the either array is null
290:             * @throws IllegalArgumentException if the either array has 0 elements
291:             * @throws IllegalArgumentException if any element in the arrays is null
292:             * @throws IllegalArgumentException if the arrays are different sizes
293:             */
294:            public static Transformer switchTransformer(Predicate[] predicates,
295:                    Transformer[] transformers, Transformer defaultTransformer) {
296:                return SwitchTransformer.getInstance(predicates, transformers,
297:                        defaultTransformer);
298:            }
299:
300:            /**
301:             * Create a new Transformer that calls one of the transformers depending 
302:             * on the predicates. 
303:             * <p>
304:             * The Map consists of Predicate keys and Transformer values. A transformer 
305:             * is called if its matching predicate returns true. Each predicate is evaluated
306:             * until one returns true. If no predicates evaluate to true, the default
307:             * transformer is called. The default transformer is set in the map with a 
308:             * null key. If no default transformer is set, null will be returned in a default
309:             * case. The ordering is that of the iterator() method on the entryset collection 
310:             * of the map.
311:             * 
312:             * @see org.apache.commons.collections.functors.SwitchTransformer
313:             * 
314:             * @param predicatesAndTransformers  a map of predicates to transformers
315:             * @return the transformer
316:             * @throws IllegalArgumentException if the map is null
317:             * @throws IllegalArgumentException if the map is empty
318:             * @throws IllegalArgumentException if any transformer in the map is null
319:             * @throws ClassCastException  if the map elements are of the wrong type
320:             */
321:            public static Transformer switchTransformer(
322:                    Map predicatesAndTransformers) {
323:                return SwitchTransformer.getInstance(predicatesAndTransformers);
324:            }
325:
326:            /**
327:             * Create a new Transformer that uses the input object as a key to find the
328:             * transformer to call. 
329:             * <p>
330:             * The Map consists of object keys and Transformer values. A transformer 
331:             * is called if the input object equals the key. If there is no match, the
332:             * default transformer is called. The default transformer is set in the map
333:             * using a null key. If no default is set, null will be returned in a default case.
334:             * 
335:             * @see org.apache.commons.collections.functors.SwitchTransformer
336:             * 
337:             * @param objectsAndTransformers  a map of objects to transformers
338:             * @return the transformer
339:             * @throws IllegalArgumentException if the map is null
340:             * @throws IllegalArgumentException if the map is empty
341:             * @throws IllegalArgumentException if any transformer in the map is null
342:             */
343:            public static Transformer switchMapTransformer(
344:                    Map objectsAndTransformers) {
345:                Transformer[] trs = null;
346:                Predicate[] preds = null;
347:                if (objectsAndTransformers == null) {
348:                    throw new IllegalArgumentException(
349:                            "The object and transformer map must not be null");
350:                }
351:                Transformer def = (Transformer) objectsAndTransformers
352:                        .remove(null);
353:                int size = objectsAndTransformers.size();
354:                trs = new Transformer[size];
355:                preds = new Predicate[size];
356:                int i = 0;
357:                for (Iterator it = objectsAndTransformers.entrySet().iterator(); it
358:                        .hasNext();) {
359:                    Map.Entry entry = (Map.Entry) it.next();
360:                    preds[i] = EqualPredicate.getInstance(entry.getKey());
361:                    trs[i] = (Transformer) entry.getValue();
362:                    i++;
363:                }
364:                return switchTransformer(preds, trs, def);
365:            }
366:
367:            /**
368:             * Gets a Transformer that expects an input Class object that it will instantiate.
369:             * 
370:             * @see org.apache.commons.collections.functors.InstantiateTransformer
371:             * 
372:             * @return the transformer
373:             */
374:            public static Transformer instantiateTransformer() {
375:                return InstantiateTransformer.NO_ARG_INSTANCE;
376:            }
377:
378:            /** 
379:             * Creates a Transformer that expects an input Class object that it will 
380:             * instantiate. The constructor used is determined by the arguments specified
381:             * to this method.
382:             *
383:             * @see org.apache.commons.collections.functors.InstantiateTransformer
384:             * 
385:             * @param paramTypes  parameter types for the constructor, can be null
386:             * @param args  the arguments to pass to the constructor, can be null
387:             * @return the transformer
388:             * @throws IllegalArgumentException if the paramTypes and args don't match
389:             */
390:            public static Transformer instantiateTransformer(
391:                    Class[] paramTypes, Object[] args) {
392:                return InstantiateTransformer.getInstance(paramTypes, args);
393:            }
394:
395:            /** 
396:             * Creates a Transformer that uses the passed in Map to transform the input 
397:             * object (as a simple lookup).
398:             *
399:             * @see org.apache.commons.collections.functors.MapTransformer
400:             * 
401:             * @param map  the map to use to transform the objects
402:             * @return the transformer
403:             * @throws IllegalArgumentException if the map is null
404:             */
405:            public static Transformer mapTransformer(Map map) {
406:                return MapTransformer.getInstance(map);
407:            }
408:
409:            /**
410:             * Gets a Transformer that invokes a method on the input object.
411:             * The method must have no parameters. If the input object is null, 
412:             * null is returned.
413:             * <p>
414:             * For example, <code>TransformerUtils.invokerTransformer("getName");</code>
415:             * will call the <code>getName/code> method on the input object to 
416:             * determine the transformer result.
417:             * 
418:             * @see org.apache.commons.collections.functors.InvokerTransformer
419:             * 
420:             * @param methodName  the method name to call on the input object, may not be null
421:             * @return the transformer
422:             * @throws IllegalArgumentException if the methodName is null.
423:             */
424:            public static Transformer invokerTransformer(String methodName) {
425:                return InvokerTransformer.getInstance(methodName, null, null);
426:            }
427:
428:            /**
429:             * Gets a Transformer that invokes a method on the input object.
430:             * The method parameters are specified. If the input object is null, 
431:             * null is returned.
432:             * 
433:             * @see org.apache.commons.collections.functors.InvokerTransformer
434:             * 
435:             * @param methodName  the name of the method
436:             * @param paramTypes  the parameter types
437:             * @param args  the arguments
438:             * @return the transformer
439:             * @throws IllegalArgumentException if the method name is null
440:             * @throws IllegalArgumentException if the paramTypes and args don't match
441:             */
442:            public static Transformer invokerTransformer(String methodName,
443:                    Class[] paramTypes, Object[] args) {
444:                return InvokerTransformer.getInstance(methodName, paramTypes,
445:                        args);
446:            }
447:
448:            /**
449:             * Gets a transformer that returns a <code>java.lang.String</code>
450:             * representation of the input object. This is achieved via the
451:             * <code>toString</code> method, <code>null</code> returns 'null'.
452:             * 
453:             * @see org.apache.commons.collections.functors.StringValueTransformer
454:             * 
455:             * @return the transformer
456:             */
457:            public static Transformer stringValueTransformer() {
458:                return StringValueTransformer.INSTANCE;
459:            }
460:
461:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.