001    package com.hammurapi.review;
002    
003    import java.io.PrintStream;
004    
005    public class PrintStreamReporter implements Reporter<Object> {
006            
007            private PrintStream out;
008            private boolean closeStream;
009    
010            public PrintStreamReporter(PrintStream out, boolean closeStream) {
011                    this.out = out;
012                    this.closeStream = closeStream;
013                    out.println("New reporter.");
014            }
015            
016            /**
017             * Constructs print stream reporter from System.out
018             */
019            public PrintStreamReporter() {
020                    this(System.out, false);
021            }
022    
023            @Override
024            public void close() throws ReviewException {
025                    out.println("Review finished.");
026                    if (closeStream) {
027                            out.close();
028                    }               
029            }
030    
031            @Override
032            public ObservationSink createObservationSink(final Module module) throws ReviewException {
033                    out.println("Started review of module "+module.getName());
034                    
035                    return new ObservationSink() {
036                            
037                            @Override
038                            public void consumeObservation(Observation observation) throws ReviewException {
039                                    out.println("["+module.getName()+"] "+observation);
040                            }
041                            
042                            @Override
043                            public void close() throws ReviewException {
044                                    out.println("Finished review of moudle "+module.getName());                             
045                            }
046                    };
047            }
048    
049            @Override
050            public void onException(Exception e) {
051                    out.println("Exception during review: ");
052                    e.printStackTrace(out);         
053            }
054    
055    }