summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/tools/WriterStack.java
blob: a3d86d7f52d1868e3e264f8925c36816cab7559b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/***
 *  WriterStack.java 1.0
 * this is a stack for Writer.
 * wrapper class of java.util.Stack
 *  
 * @see java.io.Writer
 * @see java.util.Stack
 * @version last updated: 06/11/97
 * @author  Michiaki Tatsubori
 ***/
/* package */
package io.devnulllabs.openjava.tools;


/* import */
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.EmptyStackException;
import java.util.Stack;


/**
 * Wrapper class of class Stack to specialize into Writer.
 **/
public class WriterStack
{
  private Stack stack = null;
  private static PrintWriter defaultWriter
      = new PrintWriter(
            new BufferedWriter(
              new OutputStreamWriter( System.out ) ) );

  /**
   * Constructs this stack with an element(standard output).
   **/
  public WriterStack() {
    stack = new Stack();
    stack.push( defaultWriter );
  }

  /**
   * Constructs this stack with the specified print writer.
   **/
  public WriterStack( PrintWriter writer ) {
    stack = new Stack();
    stack.push( writer );
  }

  /**
   * Constructs this stack with the specified writer.
   **/
  public WriterStack( Writer writer ) {
    stack = new Stack();
    stack.push( new PrintWriter( writer ) );
  }

  /**
   * Constructs this stack with the specified output stream.
   **/
  public WriterStack( OutputStream out ) {
    stack = new Stack();
    stack.push( new PrintWriter( new OutputStreamWriter( out ) ) );
  }

  /**
   * Looks at the print writer at the top of this stack 
   * without removing it from the stack. 
   *
   * @return the print writer at the top of this stack. 
   **/
  public PrintWriter peek() {
    try{
      return (PrintWriter)stack.peek();
    }catch( EmptyStackException ex ){
      System.err.println( ex );
      return defaultWriter;
    }
  }

  /**
   * Removes the print writer at the top of this stack and
   * returns that print writer as the value of this function. 
   * 
   * @return The object at the top of this stack. 
   **/
  public PrintWriter pop() {
    try{
      return (PrintWriter)stack.pop();
    }catch( EmptyStackException ex ){
      System.err.println( ex );
      return defaultWriter;
    }
  }

  /**
   * Pushes a print writer onto the top of this stack. 
   *
   * @param writer the print writer to be pushed onto this stack. 
   * @return the item argument. 
   **/
  public void push( PrintWriter writer ) {
    stack.push( writer );
  }

  /**
   * Pushes a print writer onto the top of this stack. 
   *
   * @param writer the writer to be pushed onto this stack. 
   * @return the item argument. 
   **/
  public void push( Writer writer ) {
    stack.push( new PrintWriter( writer ) );
  }

  /**
   * Pushes a print writer from the specified print stream
   * onto the top of this stack. 
   *
   * @param ostream the output stream to be pushed onto this stack. 
   * @return the item argument. 
   **/
  public void push( OutputStream ostream ) {
    stack.push( new PrintWriter( new OutputStreamWriter( ostream ) ) );
  }

  /**
   * Tests if this stack is empty. 
   *
   * @return true if this stack is empty; false otherwise. 
   **/
  public boolean empty() {
    return stack.empty();
  }

}