View Javadoc

1   /*
2   
3       dsh-timer  Timer with nanosecond resolution and summary statistics
4       on recorded elapsed times.
5       Copyright (c) 2004-2013 held jointly by the individual authors.
6   
7       This library is free software; you can redistribute it and/or modify it
8       under the terms of the GNU Lesser General Public License as published
9       by the Free Software Foundation; either version 3 of the License, or (at
10      your option) any later version.
11  
12      This library is distributed in the hope that it will be useful, but WITHOUT
13      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
14      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15      License for more details.
16  
17      You should have received a copy of the GNU Lesser General Public License
18      along with this library;  if not, write to the Free Software Foundation,
19      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
20  
21      > http://www.fsf.org/licensing/licenses/lgpl.html
22      > http://www.opensource.org/licenses/lgpl-license.php
23  
24  */
25  package org.dishevelled.timer.report;
26  
27  import java.io.BufferedWriter;
28  import java.io.File;
29  import java.io.FileWriter;
30  import java.io.IOException;
31  import java.io.OutputStream;
32  import java.io.OutputStreamWriter;
33  import java.io.Writer;
34  
35  import java.util.Map;
36  
37  import org.dishevelled.timer.Timer;
38  import org.dishevelled.timer.TimerReport;
39  
40  /**
41   * Abstract implementation of TimerReport.
42   */
43  abstract class AbstractTimerReport
44      implements TimerReport
45  {
46  
47      /** {@inheritDoc} */
48      public final void write(final Map<? extends Runnable, Timer> timers, final File file) throws IOException
49      {
50          if (timers == null)
51          {
52              throw new IllegalArgumentException("timers must not be null");
53          }
54          if (file == null)
55          {
56              throw new IllegalArgumentException("file must not be null");
57          }
58          Writer writer = null;
59          try
60          {
61              writer = new BufferedWriter(new FileWriter(file));
62              append(timers, writer);
63          }
64          finally
65          {
66              closeQuietly(writer);
67          }
68      }
69  
70      /** {@inheritDoc} */
71      public final void write(final Map<? extends Runnable, Timer> timers, final OutputStream outputStream) throws IOException
72      {
73          if (timers == null)
74          {
75              throw new IllegalArgumentException("timers must not be null");
76          }
77          if (outputStream == null)
78          {
79              throw new IllegalArgumentException("outputStream must not be null");
80          }
81          Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream));
82          append(timers, writer);
83          writer.flush();
84      }
85  
86      private static void closeQuietly(final Writer writer)
87      {
88          try
89          {
90              writer.close();
91          }
92          catch (Exception e)
93          {
94              // ignore
95          }
96      }
97  }