KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > util > CommandSink


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */

17 package org.apache.avalon.fortress.util;
18
19 import org.d_haven.event.Sink;
20 import org.d_haven.event.SinkException;
21 import org.d_haven.event.PreparedEnqueue;
22 import org.d_haven.event.command.CommandManager;
23 import org.d_haven.event.command.Command;
24
25 /**
26  * Created by IntelliJ IDEA. User: bloritsch Date: Jun 24, 2004 Time:
27  * 2:59:37 PM To change this template use File | Settings | File
28  * Templates.
29  */

30 public class CommandSink implements Sink
31 {
32     private final CommandManager m_manager;
33
34     public CommandSink(CommandManager manager)
35     {
36         if (manager == null) throw new IllegalArgumentException JavaDoc("manager");
37
38         m_manager = manager;
39     }
40
41     public void enqueue( Object JavaDoc o ) throws SinkException
42     {
43         checkValid( o );
44         m_manager.enqueueCommand( (Command)o );
45     }
46
47     private void checkValid( Object JavaDoc o )
48             throws SinkException
49     {
50         if ( ! (o instanceof Command) ) throw new SinkException("The object must be a command");
51     }
52
53     public void enqueue( Object JavaDoc[] objects ) throws SinkException
54     {
55         for (int i = 0; i < objects.length; i++)
56         {
57             checkValid( objects[i] );
58         }
59
60         for (int i = 0; i < objects.length; i++)
61         {
62             m_manager.enqueueCommand( (Command) objects[i] );
63         }
64     }
65
66     public boolean tryEnqueue( Object JavaDoc o )
67     {
68         boolean didEnqueue = true;
69
70         try
71         {
72             enqueue(o);
73         }
74         catch (SinkException se)
75         {
76             didEnqueue = false;
77         }
78
79         return didEnqueue;
80     }
81
82     public PreparedEnqueue prepareEnqueue( Object JavaDoc[] objects )
83             throws SinkException
84     {
85         throw new SinkException("Method not supported");
86     }
87
88     public int size()
89     {
90         return -1;
91     }
92 }
93
Popular Tags