summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/tools/WriterStack.java
diff options
context:
space:
mode:
authorKenny Ballou <kballou@devnulllabs.io>2018-11-19 22:59:50 -0700
committerKenny Ballou <kballou@devnulllabs.io>2018-11-19 22:59:50 -0700
commitea3e1b949dcbdc09518f17eee0bcf21d41d76896 (patch)
tree7ec7a7fb4df67815a9b7bb0e4d95d67c4050e2a2 /src/main/java/io/devnulllabs/openjava/tools/WriterStack.java
downloadopenjava-ea3e1b949dcbdc09518f17eee0bcf21d41d76896.tar.gz
openjava-ea3e1b949dcbdc09518f17eee0bcf21d41d76896.tar.xz
OJ (aka OpenJava) modernization/mirroring
Signed-off-by: Kenny Ballou <kballou@devnulllabs.io>
Diffstat (limited to 'src/main/java/io/devnulllabs/openjava/tools/WriterStack.java')
-rw-r--r--src/main/java/io/devnulllabs/openjava/tools/WriterStack.java138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/main/java/io/devnulllabs/openjava/tools/WriterStack.java b/src/main/java/io/devnulllabs/openjava/tools/WriterStack.java
new file mode 100644
index 0000000..a3d86d7
--- /dev/null
+++ b/src/main/java/io/devnulllabs/openjava/tools/WriterStack.java
@@ -0,0 +1,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();
+ }
+
+}