1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id: Flow.java,v 1.5 2005/09/10 23:59:42 mat007 Exp $
29 */
30
31 package jtge.engine.command;
32
33 import java.util.Iterator;
34 import java.util.Vector;
35 import jtge.engine.data.IContext;
36 import jtge.engine.user.IUser;
37
38 /***
39 * Implements a composite of commands.
40 *
41 * @author Mathieu Champlon
42 * @version $Revision: 1.5 $ $Date: 2005/09/10 23:59:42 $
43 */
44 public class Flow implements ICommand
45 {
46 private static final long serialVersionUID = -6355047856538355697L;
47 private final Vector commands;
48
49 /***
50 * Create a command composite.
51 */
52 public Flow()
53 {
54 commands = new Vector();
55 }
56
57 /***
58 * Add a command to the composite.
59 *
60 * @param command the command to add
61 */
62 public final void add( final ICommand command )
63 {
64 if( command != null )
65 commands.add( command );
66 }
67
68 /***
69 * {@inheritDoc}
70 */
71 public final boolean equals( final Object object )
72 {
73 if( object instanceof Flow )
74 return commands.equals( ((Flow)object).commands );
75 return false;
76 }
77
78 /***
79 * {@inheritDoc}
80 */
81 public final int hashCode()
82 {
83 return commands.hashCode();
84 }
85
86 /***
87 * {@inheritDoc}
88 */
89 public final boolean validate( final IUser user )
90 {
91 Iterator iterator = commands.iterator();
92 while( iterator.hasNext() )
93 if( !((ICommand)iterator.next()).validate( user ) )
94 return false;
95 return true;
96 }
97
98 /***
99 * {@inheritDoc}
100 */
101 public final void execute( final IContext context )
102 {
103 Iterator iterator = commands.iterator();
104 while( iterator.hasNext() )
105 ((ICommand)iterator.next()).execute( context );
106 }
107
108 /***
109 * {@inheritDoc}
110 */
111 public final ICommand filter( final IFilter filter )
112 {
113 Flow result = new Flow();
114 Iterator iterator = commands.iterator();
115 while( iterator.hasNext() )
116 result.add( ((ICommand)iterator.next()).filter( filter ) );
117 if( !result.isEmpty() )
118 return result;
119 return null;
120 }
121
122 /***
123 * Check whether the flow contains commands or not.
124 *
125 * @return whether the flow is empty
126 */
127 public final boolean isEmpty()
128 {
129 return commands.isEmpty();
130 }
131
132 /***
133 * {@inheritDoc}
134 */
135 public final ICommand sort( final ISorter sorter )
136 {
137 Flow result = new Flow();
138 Iterator iterator = commands.iterator();
139 while( iterator.hasNext() )
140 result.insert( ((ICommand)iterator.next()), sorter );
141 return result;
142 }
143
144 private void insert( final ICommand command, final ISorter sorter )
145 {
146 int position = commands.size();
147
148 while( position > 0 && sorter != null && sorter.lessThan( command, (ICommand)commands.get( position - 1 ) ) )
149 --position;
150 commands.add( position, command );
151 }
152 }