Debian Codegraph @

src/main/java/org/freehep/util/io/UniquePrintStream.java

raw
1 // Copyright 2001, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.OutputStream;
5 import java.io.PrintStream;
6 import java.util.Iterator;
7 import java.util.SortedSet;
8 import java.util.TreeSet;
9
10 /**
11 * The UniquePrintStream keeps Strings buffered in sorted order, but any
12 * duplicates are removed. This stream can be used to print error messages
13 * exactly once. When finish is called all messages are printed.
14 *
15 * It only acts on the println(String) method, any other method will print
16 * directly.
17 *
18 * @author Mark Donszelmann
19 * @version $Id: UniquePrintStream.java 8584 2006-08-10 23:06:37Z duns $
20 */
21 public class UniquePrintStream extends PrintStream implements
22 FinishableOutputStream {
23
24 private SortedSet msg = new TreeSet();
25
26 /**
27 * Create a Unique Print Stream.
28 *
29 * @param out stream to write
30 */
31 public UniquePrintStream(OutputStream out) {
32 super(out);
33 }
34
35 public void println(String s) {
36 synchronized (this) {
37 msg.add(s);
38 }
39 }
40
41 public void finish() {
42 for (Iterator i = msg.iterator(); i.hasNext();) {
43 String s = (String) i.next();
44 super.println(s);
45 }
46 msg = new TreeSet();
47 }
48 }