001    /*
002     * Copyright (c) 2007-2014 Concurrent, Inc. All Rights Reserved.
003     *
004     * Project and contact information: http://www.cascading.org/
005     *
006     * This file is part of the Cascading project.
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     *     http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing, software
015     * distributed under the License is distributed on an "AS IS" BASIS,
016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017     * See the License for the specific language governing permissions and
018     * limitations under the License.
019     */
020    
021    package cascading.cascade;
022    
023    import java.util.Collection;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import cascading.flow.Flow;
028    import cascading.property.UnitOfWorkDef;
029    
030    /**
031     * Class CascadeDef is a fluent interface for defining a {@link Cascade}.
032     * <p/>
033     * This allows for ad-hoc building of Cascade data and meta-data like tags.
034     * <p/>
035     * Instead of calling one of the {@link CascadeConnector} connect methods, {@link CascadeConnector#connect(CascadeDef)}
036     * can be called.
037     *
038     * @see cascading.property.UnitOfWorkDef
039     * @see cascading.flow.FlowDef
040     */
041    public class CascadeDef extends UnitOfWorkDef<CascadeDef>
042      {
043      Map<String, Flow> flows = new HashMap<String, Flow>();
044      int maxConcurrentFlows = -1;
045    
046      /**
047       * Creates a new instance of a CascadeDef.
048       *
049       * @return a CascadeDef
050       */
051      public static CascadeDef cascadeDef()
052        {
053        return new CascadeDef();
054        }
055    
056      /** Constructor CascadeDef creates a new CascadeDef instance. */
057      public CascadeDef()
058        {
059        }
060    
061      /**
062       * Method getFlows returns the flows of this CascadeDef object.
063       *
064       * @return the flows (type Collection<Flow>) of this CascadeDef object.
065       */
066      public Collection<Flow> getFlows()
067        {
068        return flows.values();
069        }
070    
071      /**
072       * Method getFlowsArray returns the flows as an array of this CascadeDef object.
073       *
074       * @return the flowsArray (type Flow[]) of this CascadeDef object.
075       */
076      public Flow[] getFlowsArray()
077        {
078        return getFlows().toArray( new Flow[ flows.size() ] );
079        }
080    
081      /**
082       * Method addFlow adds a new {@link cascading.flow.Flow} instance that is intended to participate in a {@link Cascade}.
083       *
084       * @param flow of Flow
085       * @return CascadeDef
086       */
087      public CascadeDef addFlow( Flow flow )
088        {
089        if( flow == null )
090          return this;
091    
092        if( flows.containsKey( flow.getName() ) )
093          throw new CascadeException( "all flow names must be unique, found duplicate: " + flow.getName() );
094    
095        flows.put( flow.getName(), flow );
096    
097        return this;
098        }
099    
100      /**
101       * Method addFlows adds many new {@link cascading.flow.Flow} instances intended to participate in a {@link Cascade}.
102       *
103       * @param flows of Flow[]
104       * @return CascadeDef
105       */
106      public CascadeDef addFlows( Flow... flows )
107        {
108        for( Flow flow : flows )
109          addFlow( flow );
110    
111        return this;
112        }
113    
114      /**
115       * Method addFlows adds many new {@link cascading.flow.Flow} instances intended to participate in a {@link Cascade}.
116       *
117       * @param flows of Collection<Flow>
118       * @return CascadeDef
119       */
120      public CascadeDef addFlows( Collection<Flow> flows )
121        {
122        for( Flow flow : flows )
123          addFlow( flow );
124    
125        return this;
126        }
127    
128      public CascadeDef setMaxConcurrentFlows( int maxConcurrentFlows )
129        {
130        this.maxConcurrentFlows = maxConcurrentFlows;
131    
132        return this;
133        }
134    
135      public int getMaxConcurrentFlows()
136        {
137        return maxConcurrentFlows;
138        }
139      }