Source Code Cross Referenced for IfClosure.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:
020:        import org.apache.commons.collections.Closure;
021:        import org.apache.commons.collections.Predicate;
022:
023:        /**
024:         * Closure implementation acts as an if statement calling one or other closure
025:         * based on a predicate.
026:         * 
027:         * @since Commons Collections 3.0
028:         * @version $Revision: 375766 $ $Date: 2006-02-07 23:10:36 +0000 (Tue, 07 Feb 2006) $
029:         *
030:         * @author Stephen Colebourne
031:         * @author Matt Benson
032:         */
033:        public class IfClosure implements  Closure, Serializable {
034:
035:            /** Serial version UID */
036:            private static final long serialVersionUID = 3518477308466486130L;
037:
038:            /** The test */
039:            private final Predicate iPredicate;
040:            /** The closure to use if true */
041:            private final Closure iTrueClosure;
042:            /** The closure to use if false */
043:            private final Closure iFalseClosure;
044:
045:            /**
046:             * Factory method that performs validation.
047:             * <p>
048:             * This factory creates a closure that performs no action when
049:             * the predicate is false.
050:             * 
051:             * @param predicate  predicate to switch on
052:             * @param trueClosure  closure used if true
053:             * @return the <code>if</code> closure
054:             * @throws IllegalArgumentException if either argument is null
055:             * @since Commons Collections 3.2
056:             */
057:            public static Closure getInstance(Predicate predicate,
058:                    Closure trueClosure) {
059:                return getInstance(predicate, trueClosure, NOPClosure.INSTANCE);
060:            }
061:
062:            /**
063:             * Factory method that performs validation.
064:             * 
065:             * @param predicate  predicate to switch on
066:             * @param trueClosure  closure used if true
067:             * @param falseClosure  closure used if false
068:             * @return the <code>if</code> closure
069:             * @throws IllegalArgumentException if any argument is null
070:             */
071:            public static Closure getInstance(Predicate predicate,
072:                    Closure trueClosure, Closure falseClosure) {
073:                if (predicate == null) {
074:                    throw new IllegalArgumentException(
075:                            "Predicate must not be null");
076:                }
077:                if (trueClosure == null || falseClosure == null) {
078:                    throw new IllegalArgumentException(
079:                            "Closures must not be null");
080:                }
081:                return new IfClosure(predicate, trueClosure, falseClosure);
082:            }
083:
084:            /**
085:             * Constructor that performs no validation.
086:             * Use <code>getInstance</code> if you want that.
087:             * <p>
088:             * This constructor creates a closure that performs no action when
089:             * the predicate is false.
090:             * 
091:             * @param predicate  predicate to switch on, not null
092:             * @param trueClosure  closure used if true, not null
093:             * @since Commons Collections 3.2
094:             */
095:            public IfClosure(Predicate predicate, Closure trueClosure) {
096:                this (predicate, trueClosure, NOPClosure.INSTANCE);
097:            }
098:
099:            /**
100:             * Constructor that performs no validation.
101:             * Use <code>getInstance</code> if you want that.
102:             * 
103:             * @param predicate  predicate to switch on, not null
104:             * @param trueClosure  closure used if true, not null
105:             * @param falseClosure  closure used if false, not null
106:             */
107:            public IfClosure(Predicate predicate, Closure trueClosure,
108:                    Closure falseClosure) {
109:                super ();
110:                iPredicate = predicate;
111:                iTrueClosure = trueClosure;
112:                iFalseClosure = falseClosure;
113:            }
114:
115:            /**
116:             * Executes the true or false closure accoring to the result of the predicate.
117:             * 
118:             * @param input  the input object
119:             */
120:            public void execute(Object input) {
121:                if (iPredicate.evaluate(input) == true) {
122:                    iTrueClosure.execute(input);
123:                } else {
124:                    iFalseClosure.execute(input);
125:                }
126:            }
127:
128:            /**
129:             * Gets the predicate.
130:             * 
131:             * @return the predicate
132:             * @since Commons Collections 3.1
133:             */
134:            public Predicate getPredicate() {
135:                return iPredicate;
136:            }
137:
138:            /**
139:             * Gets the closure called when true.
140:             * 
141:             * @return the closure
142:             * @since Commons Collections 3.1
143:             */
144:            public Closure getTrueClosure() {
145:                return iTrueClosure;
146:            }
147:
148:            /**
149:             * Gets the closure called when false.
150:             * 
151:             * @return the closure
152:             * @since Commons Collections 3.1
153:             */
154:            public Closure getFalseClosure() {
155:                return iFalseClosure;
156:            }
157:
158:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.