001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * 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, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.rules.factory;
017    
018    import org.springframework.rules.closure.Closure;
019    import org.springframework.rules.constraint.Constraint;
020    import org.springframework.rules.closure.support.AlgorithmsAccessor;
021    import org.springframework.rules.closure.support.ClosureChain;
022    import org.springframework.rules.closure.support.IfBlock;
023    import org.springframework.util.Assert;
024    
025    /**
026     * A factory for easing the construction and composition of closure (blocks of
027     * executable code).
028     * 
029     * @author Keith Donald
030     */
031    public class Closures extends AlgorithmsAccessor {
032    
033            private static Closures INSTANCE = new Closures();
034    
035            public Closures() {
036            }
037    
038            public static Closures instance() {
039                    return INSTANCE;
040            }
041    
042            public static void load(Closures sharedInstance) {
043                    Assert.notNull(sharedInstance, "The global closures factory cannot be null");
044                    INSTANCE = sharedInstance;
045            }
046    
047            public Closure chain(Closure firstFunction, Closure secondFunction) {
048                    return new ClosureChain(firstFunction, secondFunction);
049            }
050    
051            public Closure chain(Closure[] functionsToChain) {
052                    return new ClosureChain(functionsToChain);
053            }
054    
055            public Closure ifTrue(Constraint constraint, Closure closure) {
056                    return new IfBlock(constraint, closure);
057            }
058    
059    }