View Javadoc

1   /***
2    * Copyright 2004 Steven Caswell
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.mungoknotwise.sqlcli;
17  
18  import java.io.PrintStream;
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  import org.apache.log4j.Logger;
23  
24  /***
25   * A basic implementation of the {@link PrintStreamed} interface.
26   *
27   * @author  Steven Caswell
28   * @version $Id: BasicPrintStreamed.java,v 1.1.1.1 2004/07/28 01:12:38 mungoknotwise Exp $
29   */
30  public class BasicPrintStreamed implements PrintStreamed
31  {
32    
33    //----------------------------------------------------------------------------
34    // Static variables
35    //----------------------------------------------------------------------------
36    
37    private static Logger logger = Logger.getLogger(BasicPrintStreamed.class);
38    
39    //----------------------------------------------------------------------------
40    // Static methods
41    //----------------------------------------------------------------------------
42    
43    //----------------------------------------------------------------------------
44    // Constants
45    //----------------------------------------------------------------------------
46    
47    //----------------------------------------------------------------------------
48    // Instance variables
49    //----------------------------------------------------------------------------
50  
51    private List printStreams;
52    
53    //----------------------------------------------------------------------------
54    // Constructors
55    //----------------------------------------------------------------------------
56    
57    /***
58     * Constructs a new instance of
59     * <code>AbstractPrintStreamed</code>.
60     */
61    public BasicPrintStreamed()
62    {
63      this.printStreams = new ArrayList();
64    }
65    
66    //----------------------------------------------------------------------------
67    // Interface implementations
68    //----------------------------------------------------------------------------
69    //----------------------------------------------------------------------------
70    // Implementation of interface Interface1
71    //----------------------------------------------------------------------------
72    
73    //----------------------------------------------------------------------------
74    // Extends overrides
75    //----------------------------------------------------------------------------
76    //----------------------------------------------------------------------------
77    // Override of class Class1
78    //----------------------------------------------------------------------------
79    
80    
81    //----------------------------------------------------------------------------
82    // Public methods exposed by this class
83    //----------------------------------------------------------------------------
84  
85    /***
86     * {@inheritDoc}
87     */
88    public void addPrintStream(final PrintStream printStream)
89    {
90      if(!this.printStreams.contains(printStream))
91      {
92        this.printStreams.add(printStream);
93      }
94    }
95    
96    /***
97     * {@inheritDoc}
98     */
99    public void removePrintStream(final PrintStream printStream)
100   {
101     this.printStreams.remove(printStream);
102   }
103   
104   //----------------------------------------------------------------------------
105   // Protected abstract methods
106   //----------------------------------------------------------------------------
107   
108   //----------------------------------------------------------------------------
109   // Protected methods for use by subclasses
110   //----------------------------------------------------------------------------
111 
112   /***
113    * Prints the specified line to this object's print streams.
114    *
115    * @param line the line to print
116    */
117   protected void print(final String line)
118   {
119     for(Iterator iter = this.streamIterator(); iter.hasNext();)
120     {
121       PrintStream printStream = (PrintStream) iter.next();
122       printStream.print(line);
123     }
124   }
125   
126   /***
127    * Prints the specified line to this object's print streams.
128    *
129    * @param line the line to print
130    */
131   protected void println(final String line)
132   {
133     for(Iterator iter = this.streamIterator(); iter.hasNext();)
134     {
135       PrintStream printStream = (PrintStream) iter.next();
136       printStream.println(line);
137     }
138   }
139 
140   /***
141    * Returns an iterator of this object's print streams.
142    *
143    * @return the iterator
144    */
145   protected Iterator streamIterator()
146   {
147     return this.printStreams.iterator();
148   }
149   
150   //----------------------------------------------------------------------------
151   // Other methods
152   //----------------------------------------------------------------------------
153   
154   //----------------------------------------------------------------------------
155   // Member classes
156   //----------------------------------------------------------------------------
157   
158 }