~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/core/Debug.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This program is free software; you can redistribute it and/or modify
 
3
 *    it under the terms of the GNU General Public License as published by
 
4
 *    the Free Software Foundation; either version 2 of the License, or
 
5
 *    (at your option) any later version.
 
6
 *
 
7
 *    This program is distributed in the hope that it will be useful,
 
8
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *    GNU General Public License for more details.
 
11
 *
 
12
 *    You should have received a copy of the GNU General Public License
 
13
 *    along with this program; if not, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
15
 */
 
16
 
 
17
/*
 
18
 * Debug.java
 
19
 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
 
20
 */
 
21
 
 
22
package weka.core;
 
23
 
 
24
import java.io.BufferedWriter;
 
25
import java.io.FileWriter;
 
26
import java.io.PrintWriter;
 
27
import java.io.Serializable;
 
28
import java.io.StringWriter;
 
29
import java.lang.management.ManagementFactory;
 
30
import java.lang.management.ThreadMXBean;
 
31
import java.text.SimpleDateFormat;
 
32
import java.util.Date;
 
33
import java.util.logging.FileHandler;
 
34
import java.util.logging.Handler;
 
35
import java.util.logging.Level;
 
36
import java.util.logging.Logger;
 
37
import java.util.logging.SimpleFormatter;
 
38
 
 
39
/**
 
40
 * A helper class for debug output, logging, clocking, etc.
 
41
 * 
 
42
 * @author  fracpete (fracpete at waikato dot ac dot nz)
 
43
 * @version $Revision: 1.7 $
 
44
 */
 
45
public class Debug
 
46
  implements Serializable {
 
47
 
 
48
  /** for serialization */
 
49
  private static final long serialVersionUID = 66171861743328020L;
 
50
  
 
51
  /** the log level All */
 
52
  public final static Level ALL = Level.ALL;
 
53
  /** the log level Vonfig */
 
54
  public final static Level CONFIG = Level.CONFIG;
 
55
  /** the log level Fine */
 
56
  public final static Level FINE = Level.FINE;
 
57
  /** the log level Finer */
 
58
  public final static Level FINER = Level.FINER;
 
59
  /** the log level Finest */
 
60
  public final static Level FINEST = Level.FINEST;
 
61
  /** the log level Info */
 
62
  public final static Level INFO = Level.INFO;
 
63
  /** the log level Off - i.e., no logging */
 
64
  public final static Level OFF = Level.OFF;
 
65
  /** the log level Severe */
 
66
  public final static Level SEVERE = Level.SEVERE;
 
67
  /** the log level Warning */
 
68
  public final static Level WARNING = Level.WARNING;
 
69
 
 
70
  /** whether logging is enabled */
 
71
  protected boolean m_Enabled = true;
 
72
  
 
73
  /** for logging */
 
74
  protected Log m_Log;
 
75
  
 
76
  /** for clocking */
 
77
  protected Clock m_Clock = new Clock();
 
78
  
 
79
  /**
 
80
   * A little helper class for clocking and outputting times. It measures the
 
81
   * CPU time if possible, otherwise it's just based on the system time. In 
 
82
   * case one just wants to measure time (e.g., database queries don't take up
 
83
   * much CPU time, but still might take a long time to finish), then one can
 
84
   * disable the use of CPU time as well.
 
85
   *
 
86
   * @author FracPete (fracpete at waikato dot ac dot nz)
 
87
   * @version $Revision: 1.7 $ 
 
88
   * @see ThreadMXBean#isThreadCpuTimeEnabled()
 
89
   */
 
90
  public static class Clock 
 
91
    implements Serializable {
 
92
    
 
93
    /** for serialization */
 
94
    private static final long serialVersionUID = 4622161807307942201L;
 
95
    
 
96
    /** the output format in milli-seconds */
 
97
    public final static int FORMAT_MILLISECONDS = 0;
 
98
    
 
99
    /** the output format in seconds, with fraction of msecs */
 
100
    public final static int FORMAT_SECONDS = 1;
 
101
    
 
102
    /** the output format in hours:minutes:seconds, with fraction of msecs */
 
103
    public final static int FORMAT_HHMMSS = 2;
 
104
    
 
105
    /** the output formats */
 
106
    public static final Tag[] TAGS_FORMAT = {
 
107
      new Tag(FORMAT_MILLISECONDS, "milli-seconds"),
 
108
      new Tag(FORMAT_SECONDS, "seconds"),
 
109
      new Tag(FORMAT_HHMMSS, "hh:mm:ss")
 
110
    };
 
111
    
 
112
    /** the format of the output */
 
113
    public int m_OutputFormat = FORMAT_SECONDS;
 
114
    
 
115
    /** the start time */
 
116
    protected long m_Start;
 
117
    
 
118
    /** the end time */
 
119
    protected long m_Stop;
 
120
    
 
121
    /** whether the time is still clocked */
 
122
    protected boolean m_Running;
 
123
    
 
124
    /** the thread ID */
 
125
    protected long m_ThreadID;
 
126
    
 
127
    /** whether the system can measure the CPU time */
 
128
    protected boolean m_CanMeasureCpuTime;
 
129
    
 
130
    /** whether to use the CPU time (by default TRUE) */
 
131
    protected boolean m_UseCpuTime;
 
132
    
 
133
    /** the thread monitor, if the system can measure the CPU time */
 
134
    protected transient ThreadMXBean m_ThreadMonitor;
 
135
    
 
136
    /**
 
137
     * automatically starts the clock with FORMAT_SECONDS format and CPU
 
138
     * time if available
 
139
     * 
 
140
     * @see             #m_OutputFormat
 
141
     */
 
142
    public Clock() {
 
143
      this(true);
 
144
    }
 
145
    
 
146
    /**
 
147
     * automatically starts the clock with the given output format and CPU
 
148
     * time if available
 
149
     * 
 
150
     * @param format    the output format
 
151
     * @see             #m_OutputFormat
 
152
     */
 
153
    public Clock(int format) {
 
154
      this(true, format);
 
155
    }
 
156
    
 
157
    /**
 
158
     * starts the clock depending on <code>start</code> immediately with the
 
159
     * FORMAT_SECONDS output format and CPU time if available
 
160
     * 
 
161
     * @param start     whether to start the clock immediately
 
162
     * @see             #m_OutputFormat
 
163
     */
 
164
    public Clock(boolean start) {
 
165
      this(start, FORMAT_SECONDS);
 
166
    }
 
167
    
 
168
    /**
 
169
     * starts the clock depending on <code>start</code> immediately, using
 
170
     * CPU time if available
 
171
     * 
 
172
     * @param start     whether to start the clock immediately
 
173
     * @param format    the format
 
174
     * @see             #m_OutputFormat
 
175
     */
 
176
    public Clock(boolean start, int format) {
 
177
      m_Running    = false;
 
178
      m_Start      = 0;
 
179
      m_Stop       = 0;
 
180
      m_UseCpuTime = true;
 
181
      setOutputFormat(format);
 
182
 
 
183
      if (start)
 
184
        start();
 
185
    }
 
186
    
 
187
    /**
 
188
     * initializes the clocking, ensure to get the correct thread ID.
 
189
     */
 
190
    protected void init() {
 
191
      m_ThreadMonitor = null;
 
192
      m_ThreadMonitor = getThreadMonitor();
 
193
 
 
194
      // can we measure cpu time?
 
195
      m_CanMeasureCpuTime = m_ThreadMonitor.isThreadCpuTimeSupported();
 
196
    }
 
197
    
 
198
    /**
 
199
     * whether the measurement is based on the msecs returned from the System
 
200
     * class or on the more accurate CPU time. Also depends on whether the 
 
201
     * usage of the CPU time was disabled or enabled.
 
202
     * 
 
203
     * @return          true if the more accurate CPU time of the thread is 
 
204
     *                  used and the use of CPU time hasn't been disabled
 
205
     * @see System#currentTimeMillis()
 
206
     * @see ThreadMXBean#isThreadCpuTimeEnabled()
 
207
     * @see #getUseCpuTime()
 
208
     */
 
209
    public boolean isCpuTime() {
 
210
      return m_UseCpuTime && m_CanMeasureCpuTime;
 
211
    }
 
212
 
 
213
    /**
 
214
     * enables/disables the use of CPU time (if measurement of CPU time is 
 
215
     * available). The actual use of CPU time still depends on whether the 
 
216
     * system supports it. Resets the current timer, if running.
 
217
     * 
 
218
     * @param value     if true the CPU time is used (if possible)
 
219
     */
 
220
    public void setUseCpuTime(boolean value) {
 
221
      m_UseCpuTime = value;
 
222
      
 
223
      // we have to re-initialize the start time, otherwise we get bogus
 
224
      // results
 
225
      if (m_Running) {
 
226
        stop();
 
227
        start();
 
228
      }
 
229
    }
 
230
 
 
231
    /**
 
232
     * returns whether the use of CPU is time is enabled/disabled (regardless
 
233
     * whether the system supports it or not)
 
234
     * 
 
235
     * @return          true the CPU time is used (if possible)
 
236
     */
 
237
    public boolean getUseCpuTime() {
 
238
      return m_UseCpuTime;
 
239
    }
 
240
    
 
241
    /**
 
242
     * Returns a new thread monitor if the current one is null (e.g., due to
 
243
     * serialization) or the currently set one. The thread ID is also updated
 
244
     * if necessary.
 
245
     * 
 
246
     * @return          the thread monitor to use
 
247
     */
 
248
    protected ThreadMXBean getThreadMonitor() {
 
249
      if (m_ThreadMonitor == null) {
 
250
        m_ThreadMonitor = ManagementFactory.getThreadMXBean();
 
251
        if (!m_ThreadMonitor.isThreadCpuTimeEnabled())
 
252
          m_ThreadMonitor.setThreadCpuTimeEnabled(true);
 
253
        m_ThreadID = Thread.currentThread().getId();
 
254
      }
 
255
      
 
256
      return m_ThreadMonitor;
 
257
    }
 
258
    
 
259
    /**
 
260
     * returns the current time in msec
 
261
     * 
 
262
     * @return          the current time
 
263
     */
 
264
    protected long getCurrentTime() {
 
265
      long      result;
 
266
      
 
267
      if (isCpuTime())
 
268
        result = getThreadMonitor().getThreadUserTime(m_ThreadID) / 1000000;
 
269
      else
 
270
        result = System.currentTimeMillis();
 
271
      
 
272
      return result;
 
273
    }
 
274
    
 
275
    /**
 
276
     * saves the current system time (or CPU time) in msec as start time
 
277
     * 
 
278
     * @see       #m_Start
 
279
     */
 
280
    public void start() {
 
281
      // make sure that we get the right thread ID!
 
282
      init();
 
283
 
 
284
      m_Start   = getCurrentTime();
 
285
      m_Stop    = m_Start;
 
286
      m_Running = true;
 
287
    }
 
288
    
 
289
    /**
 
290
     * saves the current system (or CPU time) in msec as stop time
 
291
     * 
 
292
     * @see       #m_Stop
 
293
     */
 
294
    public void stop() {
 
295
      m_Stop    = getCurrentTime();
 
296
      m_Running = false;
 
297
    }
 
298
    
 
299
    /**
 
300
     * returns the start time
 
301
     * 
 
302
     * @return  the start time
 
303
     */
 
304
    public long getStart() {
 
305
      return m_Start;
 
306
    }
 
307
    
 
308
    /**
 
309
     * returns the stop time or, if still running, the current time
 
310
     * 
 
311
     * @return  the stop time
 
312
     */
 
313
    public long getStop() {
 
314
      long      result;
 
315
      
 
316
      if (isRunning())
 
317
        result = getCurrentTime();
 
318
      else
 
319
        result = m_Stop;
 
320
      
 
321
      return result;
 
322
    }
 
323
    
 
324
    /**
 
325
     * whether the time is still being clocked
 
326
     * 
 
327
     * @return          true if the time is still being clocked
 
328
     */
 
329
    public boolean isRunning() {
 
330
      return m_Running;
 
331
    }
 
332
    
 
333
    /**
 
334
     * sets the format of the output
 
335
     * 
 
336
     * @param value       the format of the output
 
337
     * @see               #m_OutputFormat
 
338
     */
 
339
    public void setOutputFormat(int value) {
 
340
      if (value == FORMAT_MILLISECONDS)
 
341
        m_OutputFormat = value;
 
342
      else if (value == FORMAT_SECONDS)
 
343
        m_OutputFormat = value;
 
344
      else if (value == FORMAT_HHMMSS)
 
345
        m_OutputFormat = value;
 
346
      else
 
347
        System.out.println("Format '" + value + "' is not recognized!");
 
348
    }
 
349
    
 
350
    /**
 
351
     * returns the output format
 
352
     * 
 
353
     * @return          the output format
 
354
     * @see             #m_OutputFormat
 
355
     */
 
356
    public int getOutputFormat() {
 
357
      return m_OutputFormat;
 
358
    }
 
359
    
 
360
    /**
 
361
     * returns the elapsed time, getStop() - getStart(), as string
 
362
     * 
 
363
     * @return  the elapsed time as string
 
364
     * @see       #getStart()
 
365
     * @see       #getStop()
 
366
     */
 
367
    public String toString() {
 
368
      String    result;
 
369
      long      elapsed;
 
370
      long      hours;
 
371
      long      mins;
 
372
      long      secs;
 
373
      long      msecs;
 
374
      
 
375
      result  = "";
 
376
      elapsed = getStop() - getStart();
 
377
      
 
378
      switch (getOutputFormat()) {
 
379
        case FORMAT_HHMMSS:
 
380
          hours   = elapsed / (3600 * 1000);
 
381
          elapsed = elapsed % (3600 * 1000);
 
382
          mins    = elapsed / (60 * 1000);
 
383
          elapsed = elapsed % (60 * 1000);
 
384
          secs    = elapsed / 1000;
 
385
          msecs   = elapsed % 1000;
 
386
          
 
387
          if (hours > 0)
 
388
            result += "" + hours + ":";
 
389
          
 
390
          if (mins < 10)
 
391
            result += "0" + mins + ":";
 
392
          else
 
393
            result += ""  + mins + ":";
 
394
          
 
395
          if (secs < 10)
 
396
            result += "0" + secs + ".";
 
397
          else
 
398
            result += "" + secs + ".";
 
399
          
 
400
          result += Utils.doubleToString(
 
401
              (double) msecs / (double) 1000, 3).replaceAll(".*\\.", "");
 
402
          break;
 
403
          
 
404
        case FORMAT_SECONDS:
 
405
          result = Utils.doubleToString((double) elapsed / (double) 1000, 3) + "s";
 
406
          break;
 
407
          
 
408
        case FORMAT_MILLISECONDS:
 
409
          result = "" + elapsed + "ms";
 
410
          break;
 
411
          
 
412
        default:
 
413
          result = "<unknown time format>";
 
414
      }
 
415
      
 
416
      return result;
 
417
    }
 
418
  }
 
419
  
 
420
  /**
 
421
   * A class that can be used for timestamps in files, The toString() method
 
422
   * simply returns the associated Date object in a timestamp format. For
 
423
   * formatting options, see java.text.SimpleDateFormat.
 
424
   *
 
425
   * @author FracPete (fracpete at waikato dot ac dot nz)
 
426
   * @version $Revision: 1.7 $ 
 
427
   * @see SimpleDateFormat
 
428
   */
 
429
  public static class Timestamp
 
430
    implements Serializable {
 
431
    
 
432
    /** for serialization */
 
433
    private static final long serialVersionUID = -6099868388466922753L;
 
434
 
 
435
    /** the default format */
 
436
    public final static String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
 
437
    
 
438
    /** the actual date */
 
439
    protected Date m_Stamp;
 
440
    
 
441
    /** the format of the timestamp */
 
442
    protected String m_Format;
 
443
    
 
444
    /** handles the format of the output */
 
445
    protected SimpleDateFormat m_Formatter;
 
446
    
 
447
    /**
 
448
     * creates a timestamp with the current date and time and the default
 
449
     * format.
 
450
     */
 
451
    public Timestamp() {
 
452
      this(DEFAULT_FORMAT);
 
453
    }
 
454
    
 
455
    /**
 
456
     * creates a timestamp with the current date and time and the specified
 
457
     * format.
 
458
     * 
 
459
     * @param format    the format of the timestamp
 
460
     * @see             SimpleDateFormat
 
461
     */
 
462
    public Timestamp(String format) {
 
463
      this(new Date(), format);
 
464
    }
 
465
    
 
466
    /**
 
467
     * creates a timestamp with the given date and the default format.
 
468
     * 
 
469
     * @param stamp     the associated date/time for the timestamp
 
470
     */
 
471
    public Timestamp(Date stamp) {
 
472
      this(stamp, DEFAULT_FORMAT);
 
473
    }
 
474
    
 
475
    /**
 
476
     * creates a timestamp with the given date and format.
 
477
     * 
 
478
     * @param stamp     the associated date/time for the timestamp
 
479
     * @param format    the format of the timestamp
 
480
     * @see             SimpleDateFormat
 
481
     */
 
482
    public Timestamp(Date stamp, String format) {
 
483
      super();
 
484
      
 
485
      m_Stamp = stamp;
 
486
      setFormat(format);
 
487
    }
 
488
    
 
489
    /**
 
490
     * sets the format for the timestamp
 
491
     * 
 
492
     * @param value     the format string
 
493
     * @see             SimpleDateFormat
 
494
     */
 
495
    public void setFormat(String value) {
 
496
      try {
 
497
        m_Formatter = new SimpleDateFormat(value);
 
498
        m_Format    = value;
 
499
      }
 
500
      catch (Exception e) {
 
501
        m_Formatter = new SimpleDateFormat(DEFAULT_FORMAT);
 
502
        m_Format    = DEFAULT_FORMAT;
 
503
      }
 
504
    }
 
505
    
 
506
    /**
 
507
     * returns the current timestamp format
 
508
     * 
 
509
     * @return          the current format
 
510
     */
 
511
    public String getFormat() {
 
512
      return m_Format;
 
513
    }
 
514
    
 
515
    /**
 
516
     * returns the associated date/time
 
517
     * 
 
518
     * @return          the timestamp value
 
519
     */
 
520
    public Date getStamp() {
 
521
      return m_Stamp;
 
522
    }
 
523
    
 
524
    /**
 
525
     * returns the timestamp as string in the specified format
 
526
     * 
 
527
     * @return          the timestamp as string
 
528
     */
 
529
    public String toString() {
 
530
      return m_Formatter.format(getStamp());
 
531
    }
 
532
  }
 
533
  
 
534
  /**
 
535
   * A little, simple helper class for logging stuff. Uses simple file access
 
536
   * and not the java.util.logging stuff (see Log for that). Uses the 
 
537
   * writeToFile methods of the Debug class.
 
538
   * 
 
539
   * @see Debug.Log
 
540
   * @see Debug#writeToFile(String, String)
 
541
   * @see Debug#writeToFile(String, String, boolean)
 
542
   */
 
543
  public static class SimpleLog 
 
544
    implements Serializable {
 
545
    
 
546
    /** for serialization */
 
547
    private static final long serialVersionUID = -2671928223819510830L;
 
548
    
 
549
    /** the file to write to (if null then only stdout is used) */
 
550
    protected String m_Filename = null;
 
551
    
 
552
    /**
 
553
     * default constructor, uses only stdout
 
554
     */
 
555
    public SimpleLog() {
 
556
      this(null);
 
557
    }
 
558
    
 
559
    /**
 
560
     * Creates a logger that writes into the specified file. Appends to the 
 
561
     * file by default.
 
562
     * 
 
563
     * @param filename  the file to write to, if null then only stdout is used
 
564
     */
 
565
    public SimpleLog(String filename) {
 
566
      this(filename, true);
 
567
    }
 
568
    
 
569
    /**
 
570
     * Creates a logger that writes into the specified file. Appends to the 
 
571
     * file by default.
 
572
     * 
 
573
     * @param filename  the file to write to, if null then only stdout is used
 
574
     * @param append    if false, the file will be deleted first
 
575
     */
 
576
    public SimpleLog(String filename, boolean append) {
 
577
      super();
 
578
      
 
579
      m_Filename = filename;
 
580
      
 
581
      Debug.writeToFile(m_Filename, "--> Log started", append);
 
582
    }
 
583
    
 
584
    /**
 
585
     * returns the filename of the log, can be null
 
586
     * 
 
587
     * @return          the filename of the log
 
588
     */
 
589
    public String getFilename() {
 
590
      return m_Filename;
 
591
    }
 
592
    
 
593
    /**
 
594
     * logs the given message to the file
 
595
     * 
 
596
     * @param message   the message to log
 
597
     */
 
598
    public void log(String message) {
 
599
      String    log;
 
600
      
 
601
      log = new Timestamp() + " " + message;
 
602
      
 
603
      if (getFilename() != null)
 
604
        Debug.writeToFile(getFilename(), log);
 
605
 
 
606
      System.out.println(log);
 
607
    }
 
608
    
 
609
    /**
 
610
     * a convenience method for dumping the current system info in the 
 
611
     * log file
 
612
     * 
 
613
     * @see SystemInfo
 
614
     */
 
615
    public void logSystemInfo() {
 
616
      log("SystemInfo:\n" + new SystemInfo().toString());
 
617
    }
 
618
    
 
619
    /**
 
620
     * returns a string representation of the logger
 
621
     * 
 
622
     * @return          a string representation of the logger
 
623
     */
 
624
    public String toString() {
 
625
      String    result;
 
626
      
 
627
      result =   "Filename: " + getFilename();
 
628
      
 
629
      return result;
 
630
    }
 
631
  }
 
632
  
 
633
  /**
 
634
   * A helper class for logging stuff. Uses the java.util.logging
 
635
   * package. If this approach seems an "overkill" (it can create quite a few 
 
636
   * log files if used in different threads), one can use the 
 
637
   * Debug.SimpleLog class.
 
638
   *
 
639
   * @author FracPete (fracpete at waikato dot ac dot nz)
 
640
   * @version $Revision: 1.7 $ 
 
641
   * @see Debug.SimpleLog
 
642
   */
 
643
  public static class Log
 
644
    implements Serializable {
 
645
    
 
646
    /** for serialization */
 
647
    private static final long serialVersionUID = 1458435732111675823L;
 
648
 
 
649
    /** the actual logger, if null only stdout is used */
 
650
    protected transient Logger m_Logger = null;
 
651
    
 
652
    /** the filename, if any */
 
653
    protected String m_Filename = null;
 
654
    
 
655
    /** the size of the file (in bytes) */
 
656
    protected int m_Size;
 
657
    
 
658
    /** the number of files for rotating the logs */
 
659
    protected int m_NumFiles;
 
660
 
 
661
    /** whether the initialization of the logger failed */
 
662
    protected boolean m_LoggerInitFailed = false;
 
663
    
 
664
    /**
 
665
     * default constructor, uses only stdout
 
666
     */
 
667
    public Log() {
 
668
      this(null);
 
669
    }
 
670
    
 
671
    /**
 
672
     * creates a logger that logs into the specified file, if null then only
 
673
     * stdout is used. It uses 1,000,000 bytes for file size and 1 file.
 
674
     * 
 
675
     * @param filename  the file to log into
 
676
     */
 
677
    public Log(String filename) {
 
678
      this(filename, 1000000, 1);
 
679
    }
 
680
    
 
681
    /**
 
682
     * creates a logger that logs into the specified file, if null then only
 
683
     * stdout is used.
 
684
     * 
 
685
     * @param filename  the file to log into
 
686
     * @param size      the size of the files in bytes
 
687
     * @param numFiles  the number of files for rotating
 
688
     */
 
689
    public Log(String filename, int size, int numFiles) {
 
690
      m_Filename = filename;
 
691
      m_Size     = size;
 
692
      m_NumFiles = numFiles;
 
693
    }
 
694
    
 
695
    /**
 
696
     * initializes and returns the logger if necessary (e.g., due to 
 
697
     * serialization).
 
698
     * 
 
699
     * @return          the logger, can be null, e.g., if no filename provided
 
700
     */
 
701
    protected Logger getLogger() {
 
702
      if ( (m_Logger == null) && (!m_LoggerInitFailed) ) {
 
703
        if (m_Filename != null) {
 
704
          m_Logger = Logger.getLogger(m_Filename);
 
705
          Handler fh = null;
 
706
          try{       
 
707
            fh = new FileHandler(m_Filename, m_Size, m_NumFiles);
 
708
            fh.setFormatter(new SimpleFormatter());
 
709
            m_Logger.addHandler(fh);      
 
710
            m_LoggerInitFailed = false;
 
711
          }
 
712
          catch(Exception e) {
 
713
            System.out.println("Cannot init fileHandler for logger:" + e.toString());
 
714
            m_Logger = null;
 
715
            m_LoggerInitFailed = true;
 
716
          }  
 
717
        }
 
718
      }
 
719
      
 
720
      return m_Logger;
 
721
    }
 
722
    
 
723
    /**
 
724
     * turns the string representing a level, e.g., "FINE" or "ALL" into
 
725
     * the corresponding level (case-insensitive). The default is ALL.
 
726
     * 
 
727
     * @param level     the string to return a level for
 
728
     * @return          the corresponding level or the default
 
729
     */
 
730
    public static Level stringToLevel(String level) {
 
731
      Level     result;
 
732
      
 
733
      if (level.equalsIgnoreCase("ALL"))
 
734
        result = ALL;
 
735
      else if (level.equalsIgnoreCase("CONFIG"))
 
736
        result = CONFIG;
 
737
      else if (level.equalsIgnoreCase("FINE"))
 
738
        result = FINE;
 
739
      else if (level.equalsIgnoreCase("FINER"))
 
740
        result = FINER;
 
741
      else if (level.equalsIgnoreCase("FINEST"))
 
742
        result = FINEST;
 
743
      else if (level.equalsIgnoreCase("INFO"))
 
744
        result = INFO;
 
745
      else if (level.equalsIgnoreCase("OFF"))
 
746
        result = OFF;
 
747
      else if (level.equalsIgnoreCase("SEVERE"))
 
748
        result = SEVERE;
 
749
      else if (level.equalsIgnoreCase("WARNING"))
 
750
        result = WARNING;
 
751
      else
 
752
        result = ALL;
 
753
      
 
754
      return result;
 
755
    }
 
756
    
 
757
    /**
 
758
     * returns the filename of the log, can be null
 
759
     * 
 
760
     * @return          the filename of the log
 
761
     */
 
762
    public String getFilename() {
 
763
      return m_Filename;
 
764
    }
 
765
    
 
766
    /**
 
767
     * returns the size of the files
 
768
     * 
 
769
     * @return          the size of a file
 
770
     */
 
771
    public int getSize() {
 
772
      return m_Size;
 
773
    }
 
774
    
 
775
    /**
 
776
     * returns the number of files being used
 
777
     * 
 
778
     * @return          the number of files
 
779
     */
 
780
    public int getNumFiles() {
 
781
      return m_NumFiles;
 
782
    }
 
783
 
 
784
    /**
 
785
     * logs the given message
 
786
     * 
 
787
     * @param level     the level of severity
 
788
     * @param message   the message to log
 
789
     */
 
790
    public void log(Level level, String message) {
 
791
      log(level, "", message);
 
792
    }
 
793
    
 
794
    /**
 
795
     * prints the given message with the specified level
 
796
     * 
 
797
     * @param level     the level of logging
 
798
     * @param sourceclass       the class that logs the message
 
799
     * @param message   the message to print
 
800
     */
 
801
    public void log(Level level, String sourceclass, String message) {
 
802
      log(level, sourceclass, "", message);
 
803
    }
 
804
    
 
805
    /**
 
806
     * prints the given message with the specified level
 
807
     * 
 
808
     * @param level             the level of logging
 
809
     * @param sourceclass               the class that logs the message
 
810
     * @param sourcemethod      the method that logs the message
 
811
     * @param message           the message to print
 
812
     */
 
813
    public void log(Level level, String sourceclass, String sourcemethod, String message) {
 
814
      Logger    logger;
 
815
      
 
816
      logger = getLogger();
 
817
      
 
818
      if (logger != null)
 
819
        logger.logp(level, sourceclass, sourcemethod, message);
 
820
      else
 
821
        System.out.println(message);
 
822
    }
 
823
    
 
824
    /**
 
825
     * a convenience method for dumping the current system info in the 
 
826
     * log file
 
827
     * 
 
828
     * @see SystemInfo
 
829
     */
 
830
    public void logSystemInfo() {
 
831
      log(INFO, "SystemInfo:\n" + new SystemInfo().toString());
 
832
    }
 
833
    
 
834
    /**
 
835
     * returns a string representation of the logger
 
836
     * 
 
837
     * @return          a string representation of the logger
 
838
     */
 
839
    public String toString() {
 
840
      String    result;
 
841
      
 
842
      result =   "Filename: " + getFilename() + ", "
 
843
               + "Size: " + getSize() + ", "
 
844
               + "# Files: " + getNumFiles();
 
845
      
 
846
      return result;
 
847
    }
 
848
  }
 
849
 
 
850
  /**
 
851
   * This extended Random class enables one to print the generated random
 
852
   * numbers etc., before they are returned. It can either use stdout (default)
 
853
   * for outputting the logging information or a Log object (level is then 
 
854
   * INFO).
 
855
   *
 
856
   * @author  FracPete (fracpete at waikato dot ac dot nz)
 
857
   * @version $Revision: 1.7 $
 
858
   */
 
859
  public static class Random
 
860
    extends java.util.Random
 
861
    implements Serializable {
 
862
 
 
863
    /** for serialization */
 
864
    private static final long serialVersionUID = 1256846887618333956L;
 
865
 
 
866
    /** whether to output debug information */
 
867
    protected boolean m_Debug = false;
 
868
 
 
869
    /** the unique ID for this number generator */
 
870
    protected long m_ID;
 
871
    
 
872
    /** for keeping track of unique IDs */
 
873
    protected static long m_CurrentID;
 
874
    
 
875
    /** the log to use for outputting the data, otherwise just stdout */
 
876
    protected Log m_Log = null;
 
877
    
 
878
    /**
 
879
     * Creates a new random number generator. With no debugging.
 
880
     */
 
881
    public Random() {
 
882
      this(false);
 
883
    }
 
884
 
 
885
    /**
 
886
     * Creates a new random number generator using a single long seed.
 
887
     * With no debugging
 
888
     * 
 
889
     * @param seed      the seed value
 
890
     */
 
891
    public Random(long seed) {
 
892
      this(seed, false);
 
893
    }
 
894
 
 
895
    /**
 
896
     * Creates a new random number generator. With optional debugging.
 
897
     * 
 
898
     * @param debug     if true, debugging output is enabled
 
899
     */
 
900
    public Random(boolean debug) {
 
901
      super();
 
902
      setDebug(debug);
 
903
      m_ID = nextID();
 
904
      if (getDebug())
 
905
        printStackTrace();
 
906
    }
 
907
 
 
908
    /**
 
909
     * Creates a new random number generator using a single long seed.
 
910
     * With optional debugging
 
911
     * 
 
912
     * @param seed      the seed value
 
913
     * @param debug     if true, debugging output is enabled
 
914
     */
 
915
    public Random(long seed, boolean debug) {
 
916
      super(seed);
 
917
      setDebug(debug);
 
918
      m_ID = nextID();
 
919
      if (getDebug())
 
920
        printStackTrace();
 
921
    }
 
922
 
 
923
    /**
 
924
     * sets whether to print the generated random values or not
 
925
     * 
 
926
     * @param value     if true debugging output is enabled
 
927
     */
 
928
    public void setDebug(boolean value) {
 
929
      m_Debug = value;
 
930
    }
 
931
 
 
932
    /**
 
933
     * returns whether to print the generated random values or not
 
934
     * 
 
935
     * @return          true if debugging output is enabled
 
936
     */
 
937
    public boolean getDebug() {
 
938
      return m_Debug;
 
939
    }
 
940
    
 
941
    /**
 
942
     * the log to use, if it is null then stdout is used
 
943
     * 
 
944
     * @param value     the log to use
 
945
     */
 
946
    public void setLog(Log value) {
 
947
      m_Log = value;
 
948
    }
 
949
    
 
950
    /**
 
951
     * the currently used log, if null then stdout is used for outputting
 
952
     * the debugging information
 
953
     * 
 
954
     * @return          the log, can be null
 
955
     */
 
956
    public Log getLog() {
 
957
      return m_Log;
 
958
    }
 
959
 
 
960
    /**
 
961
     * returns the next unique ID for a number generator
 
962
     * 
 
963
     * @return          the next unique ID
 
964
     */
 
965
    protected static long nextID() {
 
966
      m_CurrentID++;
 
967
      
 
968
      return m_CurrentID;
 
969
    }
 
970
 
 
971
    /**
 
972
     * returns the unique ID of this number generator
 
973
     * 
 
974
     * @return          the unique ID of this number generator
 
975
     */
 
976
    public long getID() {
 
977
      return m_ID;
 
978
    }
 
979
 
 
980
    /**
 
981
     * prints the given message only if m_Debug is TRUE
 
982
     * 
 
983
     * @param msg       the message to print
 
984
     * @see             #m_Debug
 
985
     */
 
986
    protected void println(String msg) {
 
987
      if (getDebug()) {
 
988
        if (getLog() != null)
 
989
          getLog().log(Level.INFO, m_ID + ": " + msg);
 
990
        else
 
991
          System.out.println(m_ID + ": " + msg);
 
992
      }
 
993
    }
 
994
 
 
995
    /**
 
996
     * prints the current stacktrace
 
997
     */
 
998
    public void printStackTrace() {
 
999
      Throwable         t;
 
1000
      StringWriter      writer;
 
1001
 
 
1002
      writer = new StringWriter();
 
1003
      
 
1004
      // generate stacktrace
 
1005
      t = new Throwable();
 
1006
      t.fillInStackTrace();
 
1007
      t.printStackTrace(new PrintWriter(writer));
 
1008
 
 
1009
      println(writer.toString());
 
1010
    }
 
1011
 
 
1012
    /**
 
1013
     * Returns the next pseudorandom, uniformly distributed boolean value from
 
1014
     * this random number generator's sequence.
 
1015
     * 
 
1016
     * @return          random boolean
 
1017
     */
 
1018
    public boolean nextBoolean() {
 
1019
      boolean result = super.nextBoolean();
 
1020
      println("nextBoolean=" + result);
 
1021
      return result;
 
1022
    }
 
1023
 
 
1024
    /**
 
1025
     * Generates random bytes and places them into a user-supplied byte array.
 
1026
     * 
 
1027
     * @param bytes     array to fill with random bytes
 
1028
     */
 
1029
    public void nextBytes(byte[] bytes) {
 
1030
      super.nextBytes(bytes);
 
1031
      println("nextBytes=" + Utils.arrayToString(bytes));
 
1032
    }
 
1033
 
 
1034
    /**
 
1035
     * Returns the next pseudorandom, uniformly distributed double value between
 
1036
     * 0.0 and 1.0 from this random number generator's sequence.
 
1037
     * 
 
1038
     * @return          random double
 
1039
     */
 
1040
    public double nextDouble() {
 
1041
      double result = super.nextDouble();
 
1042
      println("nextDouble=" + result);
 
1043
      return result;
 
1044
    }
 
1045
 
 
1046
    /**
 
1047
     * Returns the next pseudorandom, uniformly distributed float  value between
 
1048
     * 0.0 and 1.0 from this random number generator's sequence.
 
1049
     * 
 
1050
     * @return          random float
 
1051
     */
 
1052
    public float nextFloat() {
 
1053
      float result = super.nextFloat();
 
1054
      println("nextFloat=" + result);
 
1055
      return result;
 
1056
    }
 
1057
 
 
1058
    /**
 
1059
     * Returns the next pseudorandom, Gaussian ("normally") distributed double
 
1060
     * value with mean 0.0 and standard deviation 1.0 from this random number
 
1061
     * generator's sequence.
 
1062
     * 
 
1063
     * @return          random double, gaussian distributed
 
1064
     */
 
1065
    public double nextGaussian() {
 
1066
      double result = super.nextGaussian();
 
1067
      println("nextGaussian=" + result);
 
1068
      return result;
 
1069
    }
 
1070
 
 
1071
    /**
 
1072
     * Returns the next pseudorandom, uniformly distributed int  value from this
 
1073
     * random number generator's sequence.
 
1074
     * 
 
1075
     * @return          random int
 
1076
     */
 
1077
    public int nextInt() {
 
1078
      int result = super.nextInt();
 
1079
      println("nextInt=" + result);
 
1080
      return result;
 
1081
    }
 
1082
 
 
1083
    /**
 
1084
     * Returns a pseudorandom, uniformly distributed int value between 0
 
1085
     * (inclusive) and the specified value (exclusive), drawn from this random
 
1086
     * number generator's sequence.
 
1087
     * 
 
1088
     * @param n         the upper limit (exclusive)
 
1089
     * @return          random int
 
1090
     */
 
1091
    public int nextInt(int n) {
 
1092
      int result = super.nextInt(n);
 
1093
      println("nextInt(" + n + ")=" + result);
 
1094
      return result;
 
1095
    }
 
1096
 
 
1097
    /**
 
1098
     * Returns the next pseudorandom, uniformly distributed long  value from this
 
1099
     * random number generator's sequence.
 
1100
     * 
 
1101
     * @return          random long
 
1102
     */
 
1103
    public long nextLong() {
 
1104
      long result = super.nextLong();
 
1105
      println("nextLong=" + result);
 
1106
      return result;
 
1107
    }
 
1108
 
 
1109
    /**
 
1110
     * Sets the seed of this random number generator using a single long seed.
 
1111
     * 
 
1112
     * @param seed      the seed value
 
1113
     */
 
1114
    public void setSeed(long seed) {
 
1115
      super.setSeed(seed);
 
1116
      println("setSeed(" + seed + ")");
 
1117
    }
 
1118
 
 
1119
    /**
 
1120
     * returns a string representation of this number generator
 
1121
     * 
 
1122
     * @return          a string representation
 
1123
     */
 
1124
    public String toString() {
 
1125
      return this.getClass().getName() + ": " + getID();
 
1126
    }
 
1127
  }
 
1128
  /**
 
1129
   * contains debug methods
 
1130
   *
 
1131
   * @author Gabi Schmidberger (gabi at cs dot waikato dot ac dot nz)
 
1132
   * @version $Revision: 1.7 $
 
1133
   */
 
1134
  public static class DBO 
 
1135
    implements Serializable {
 
1136
 
 
1137
    /** for serialization */
 
1138
    static final long serialVersionUID = -5245628124742606784L;  
 
1139
 
 
1140
    /** enables/disables output of debug information */
 
1141
    public boolean m_verboseOn = false;
 
1142
 
 
1143
    /** range of outputtyp */
 
1144
    public Range m_outputTypes = new Range();
 
1145
 
 
1146
    /** 
 
1147
     * Set the verbose on flag on
 
1148
     */
 
1149
    public void setVerboseOn() {
 
1150
      m_verboseOn = true;
 
1151
    }
 
1152
 
 
1153
    /** 
 
1154
     * Initialize ranges, upper limit must be set
 
1155
     * 
 
1156
     * @param upper upper limit
 
1157
     */
 
1158
    public void initializeRanges(int upper) {
 
1159
      m_outputTypes.setUpper(upper);
 
1160
    }
 
1161
 
 
1162
    /**
 
1163
     * Return true if the outputtype is set
 
1164
     * 
 
1165
     * @param num value that is reserved for a specific outputtype
 
1166
     * @return return true if the output type is set
 
1167
     */
 
1168
    public boolean outputTypeSet(int num) {
 
1169
      return (m_outputTypes.isInRange(num));
 
1170
    }
 
1171
 
 
1172
     /**
 
1173
     * Return true if the debug level is set
 
1174
     * same method as outpuTypeSet but better name
 
1175
     * 
 
1176
     * @param num value that is reserved for a specific outputtype
 
1177
     * @return return true if the debug level is set
 
1178
     */
 
1179
    public boolean dl(int num) {
 
1180
      return (outputTypeSet(num));
 
1181
    }
 
1182
 
 
1183
   /**
 
1184
     * Switches the outputs on that are requested from the option O
 
1185
     * 
 
1186
     * @param list list of integers, all are used for an output type
 
1187
     */
 
1188
    public void setOutputTypes(String list) {
 
1189
      if (list.length() > 0) {
 
1190
        m_verboseOn = true; 
 
1191
 
 
1192
        m_outputTypes.setRanges(list);
 
1193
        m_outputTypes.setUpper(30);
 
1194
      }
 
1195
    }
 
1196
 
 
1197
    /**
 
1198
     * Gets the current output type selection
 
1199
     *
 
1200
     * @return a string containing a comma separated list of ranges
 
1201
     */
 
1202
    public String getOutputTypes() {
 
1203
      return m_outputTypes.getRanges();
 
1204
    }
 
1205
 
 
1206
    /**
 
1207
     * prints out text + endofline if verbose is on.
 
1208
     * helps to make debug output commands more visible in text
 
1209
     * 
 
1210
     * @param text the text to print
 
1211
     */
 
1212
    public void dpln(String text) {
 
1213
      if (m_verboseOn) {
 
1214
        System.out.println(text);
 
1215
      }
 
1216
    } 
 
1217
 
 
1218
    /**
 
1219
     * prints out text + endofline but only if parameter debug type is set.
 
1220
     * helps to make debug output commands more visible in text
 
1221
     *
 
1222
     * @param debugType the type of the output
 
1223
     * @param text the text to print
 
1224
     */
 
1225
    public void dpln(int debugType, String text) {
 
1226
      if (outputTypeSet(debugType)) {
 
1227
        System.out.println(text);
 
1228
      }
 
1229
    } 
 
1230
 
 
1231
     /**
 
1232
     * prints out text  if verbose is on.
 
1233
     * helps to make debug output commands more visible in text
 
1234
     * 
 
1235
     * @param text the text to print
 
1236
     */
 
1237
    public void dp(String text) {
 
1238
      if (m_verboseOn) {
 
1239
        System.out.print(text);
 
1240
      }
 
1241
    } 
 
1242
 
 
1243
   /**
 
1244
     * prints out text but only if debug level is set.
 
1245
     * helps to make debug output commands more visible in text
 
1246
     *
 
1247
     * @param debugType the type of the output
 
1248
     * @param text the text to print
 
1249
     */
 
1250
    public void dp(int debugType, String text) {
 
1251
     if (outputTypeSet(debugType)) {
 
1252
        System.out.print(text);
 
1253
      }
 
1254
    } 
 
1255
 
 
1256
    /**
 
1257
     * prints out text + endofline.
 
1258
     * helps to make debug output commands more visible in text
 
1259
     * 
 
1260
     * @param text the text to print
 
1261
     */
 
1262
    public static void pln(String text) {
 
1263
      System.out.println(text);
 
1264
    } 
 
1265
 
 
1266
    /**
 
1267
     * prints out text.
 
1268
     * helps to make debug output commands more visible in text
 
1269
     * 
 
1270
     * @param text the text to print
 
1271
     */
 
1272
    public static void p (String text) {
 
1273
      System.out.print(text);
 
1274
    } 
 
1275
  }
 
1276
  
 
1277
  /**
 
1278
   * default constructor, prints only to stdout
 
1279
   */
 
1280
  public Debug() {
 
1281
    this(null);
 
1282
  }
 
1283
  
 
1284
  /**
 
1285
   * logs the output to the specified file (and stdout). Size is 1,000,000 bytes 
 
1286
   * and 1 file.
 
1287
   * 
 
1288
   * @param filename    the name of the log
 
1289
   */
 
1290
  public Debug(String filename) {
 
1291
    this(filename, 1000000, 1);
 
1292
  }
 
1293
  
 
1294
  /**
 
1295
   * logs the output
 
1296
   * 
 
1297
   * @param filename    the name of the log
 
1298
   * @param size        the size of the files in bytes
 
1299
   * @param numFiles    the number of files for rotating
 
1300
   */
 
1301
  public Debug(String filename, int size, int numFiles) {
 
1302
    super();
 
1303
    
 
1304
    m_Log = newLog(filename, size, numFiles);
 
1305
  }
 
1306
  
 
1307
  /**
 
1308
   * turns the string representing a level, e.g., "FINE" or "ALL" into
 
1309
   * the corresponding level (case-insensitive). The default is ALL.
 
1310
   * 
 
1311
   * @param level       the string to return a level for
 
1312
   * @return            the corresponding level or the default
 
1313
   */
 
1314
  public static Level stringToLevel(String level) {
 
1315
    return Log.stringToLevel(level);
 
1316
  }
 
1317
  
 
1318
  /**
 
1319
   * returns a new Log instance
 
1320
   * 
 
1321
   * @param filename    the name of the log
 
1322
   * @param size        the size of the files in bytes
 
1323
   * @param numFiles    the number of files for rotating
 
1324
   * @return            the log instance
 
1325
   */
 
1326
  public static Log newLog(String filename, int size, int numFiles) {
 
1327
    return new Log(filename, size, numFiles);
 
1328
  }
 
1329
  
 
1330
  /**
 
1331
   * prints the given message with level INFO
 
1332
   * 
 
1333
   * @param message     the message to print
 
1334
   */
 
1335
  public void log(String message) {
 
1336
    log(INFO, message);
 
1337
  }
 
1338
  
 
1339
  /**
 
1340
   * prints the given message with the specified level and an empty sourceclass
 
1341
   * 
 
1342
   * @param level       the level of logging
 
1343
   * @param message     the message to print
 
1344
   */
 
1345
  public void log(Level level, String message) {
 
1346
    log(level, "", message);
 
1347
  }
 
1348
  
 
1349
  /**
 
1350
   * prints the given message with the specified level
 
1351
   * 
 
1352
   * @param level       the level of logging
 
1353
   * @param sourceclass the class that logs the message
 
1354
   * @param message     the message to print
 
1355
   */
 
1356
  public void log(Level level, String sourceclass, String message) {
 
1357
    log(level, sourceclass, "", message);
 
1358
  }
 
1359
  
 
1360
  /**
 
1361
   * prints the given message with the specified level
 
1362
   * 
 
1363
   * @param level               the level of logging
 
1364
   * @param sourceclass         the class that logs the message
 
1365
   * @param sourcemethod        the method that logs the message
 
1366
   * @param message             the message to print
 
1367
   */
 
1368
  public void log(Level level, String sourceclass, String sourcemethod, String message) {
 
1369
    if (getEnabled())
 
1370
      m_Log.log(level, sourceclass, sourcemethod, message);
 
1371
  }
 
1372
  
 
1373
  /**
 
1374
   * sets whether the logging is enabled or not
 
1375
   * 
 
1376
   * @param value       if true logging will be enabled
 
1377
   */
 
1378
  public void setEnabled(boolean value) {
 
1379
    m_Enabled = value;
 
1380
  }
 
1381
  
 
1382
  /**
 
1383
   * returns whether the logging is enabled
 
1384
   * 
 
1385
   * @return            true if the logging is enabled
 
1386
   */
 
1387
  public boolean getEnabled() {
 
1388
    return m_Enabled;
 
1389
  }
 
1390
  
 
1391
  /**
 
1392
   * returns a new instance of a clock
 
1393
   * 
 
1394
   * @return            a new instance of a Clock
 
1395
   */
 
1396
  public static Clock newClock() {
 
1397
    return new Clock();
 
1398
  }
 
1399
  
 
1400
  /**
 
1401
   * returns the instance of the Clock that is internally used
 
1402
   * 
 
1403
   * @return            the clock that's being used
 
1404
   */
 
1405
  public Clock getClock() {
 
1406
    return m_Clock;
 
1407
  }
 
1408
  
 
1409
  /**
 
1410
   * starts the clock
 
1411
   */
 
1412
  public void startClock() {
 
1413
    m_Clock.start();
 
1414
  }
 
1415
  
 
1416
  /**
 
1417
   * stops the clock and prints the message associated with the time, but only
 
1418
   * if the logging is enabled.
 
1419
   * 
 
1420
   * @param message     the message to print
 
1421
   * @see               #getEnabled()
 
1422
   */
 
1423
  public void stopClock(String message) {
 
1424
    log(message + ": " + m_Clock);
 
1425
  }
 
1426
  
 
1427
  /**
 
1428
   * returns a default debug random object, with no particular seed and 
 
1429
   * debugging enabled.
 
1430
   * 
 
1431
   * @return            a new instance of a Random object
 
1432
   */
 
1433
  public static java.util.Random newRandom() {
 
1434
    return new Random(true);
 
1435
  }
 
1436
  
 
1437
  /**
 
1438
   * returns a debug random object with the specified seed and debugging 
 
1439
   * enabled.
 
1440
   * 
 
1441
   * @param seed        the seed value
 
1442
   * @return            a new instance of a Random object
 
1443
   */
 
1444
  public static java.util.Random newRandom(int seed) {
 
1445
    return new Random(seed, true);
 
1446
  }
 
1447
 
 
1448
  /**
 
1449
   * returns a default timestamp for the current date/time
 
1450
   * 
 
1451
   * @return            a new timestamp
 
1452
   */
 
1453
  public static Timestamp newTimestamp() {
 
1454
    return new Timestamp();
 
1455
  }
 
1456
  
 
1457
  /**
 
1458
   * returns the system temp directory
 
1459
   * 
 
1460
   * @return            the temp directory
 
1461
   */
 
1462
  public static String getTempDir() {
 
1463
    return System.getProperty("java.io.tmpdir");
 
1464
  }
 
1465
  
 
1466
  /**
 
1467
   * returns the home directory of the user
 
1468
   * 
 
1469
   * @return            the user's home directory
 
1470
   */
 
1471
  public static String getHomeDir() {
 
1472
    return System.getProperty("user.home");
 
1473
  }
 
1474
  
 
1475
  /**
 
1476
   * returns the current working directory of the user
 
1477
   * 
 
1478
   * @return            the user's current working directory
 
1479
   */
 
1480
  public static String getCurrentDir() {
 
1481
    return System.getProperty("user.dir");
 
1482
  }
 
1483
  
 
1484
  /**
 
1485
   * Writes the given object to the specified file. The string representation
 
1486
   * of the object is appended to the file.
 
1487
   * 
 
1488
   * @param filename    the file to write to
 
1489
   * @param obj         the object to write to the file
 
1490
   * @return            true if writing was successful
 
1491
   */
 
1492
  public static boolean writeToFile(String filename, Object obj) {
 
1493
    return writeToFile(filename, obj, true);
 
1494
  }
 
1495
  
 
1496
  /**
 
1497
   * Writes the given message to the specified file. The message is appended 
 
1498
   * to the file.
 
1499
   * 
 
1500
   * @param filename    the file to write to
 
1501
   * @param message     the message to write
 
1502
   * @return            true if writing was successful
 
1503
   */
 
1504
  public static boolean writeToFile(String filename, String message) {
 
1505
    return writeToFile(filename, message, true);
 
1506
  }
 
1507
  
 
1508
  /**
 
1509
   * Writes the given object to the specified file. The string representation 
 
1510
   * of the object is either appended or replaces the current content of the 
 
1511
   * file.
 
1512
   * 
 
1513
   * @param filename    the file to write to
 
1514
   * @param obj         the object to write to the file
 
1515
   * @param append      whether to append the message or not
 
1516
   * @return            true if writing was successful
 
1517
   */
 
1518
  public static boolean writeToFile(String filename, Object obj, boolean append) {
 
1519
    return writeToFile(filename, obj.toString(), append);
 
1520
  }
 
1521
  
 
1522
  /**
 
1523
   * Writes the given message to the specified file. The message is either 
 
1524
   * appended or replaces the current content of the file.
 
1525
   * 
 
1526
   * @param filename    the file to write to
 
1527
   * @param message     the message to write
 
1528
   * @param append      whether to append the message or not
 
1529
   * @return            true if writing was successful
 
1530
   */
 
1531
  public static boolean writeToFile(String filename, String message, boolean append) {
 
1532
    boolean             result;
 
1533
    BufferedWriter      writer;
 
1534
    
 
1535
    try {
 
1536
      writer = new BufferedWriter(new FileWriter(filename, append));
 
1537
      writer.write(message);
 
1538
      writer.newLine();
 
1539
      writer.flush();
 
1540
      writer.close();
 
1541
      result = true;
 
1542
    }
 
1543
    catch (Exception e) {
 
1544
      result = false;
 
1545
    }
 
1546
    
 
1547
    return result;
 
1548
  }
 
1549
  
 
1550
  /**
 
1551
   * writes the serialized object to the speicified file
 
1552
   * 
 
1553
   * @param filename    the file to serialize the object to
 
1554
   * @param o           the object to serialize
 
1555
   * @return            true if writing was successful
 
1556
   */
 
1557
  public static boolean saveToFile(String filename, Object o) {
 
1558
    boolean     result;
 
1559
    
 
1560
    if (SerializationHelper.isSerializable(o.getClass())) {
 
1561
      try {
 
1562
        SerializationHelper.write(filename, o);
 
1563
        result = true;
 
1564
      }
 
1565
      catch (Exception e) {
 
1566
        result = false;
 
1567
      }
 
1568
    }
 
1569
    else {
 
1570
      result = false;
 
1571
    }
 
1572
    
 
1573
    return result;
 
1574
  }
 
1575
  
 
1576
  /**
 
1577
   * deserializes the content of the file and returns it, null if an error
 
1578
   * occurred.
 
1579
   * 
 
1580
   * @param filename    the name of the file to deserialize
 
1581
   * @return            the deserialized content, null if problem occurred
 
1582
   */
 
1583
  public static Object loadFromFile(String filename) {
 
1584
    Object      result;
 
1585
    
 
1586
    try {
 
1587
      result = SerializationHelper.read(filename);
 
1588
    }
 
1589
    catch (Exception e) {
 
1590
      result = null;
 
1591
    }
 
1592
    
 
1593
    return result;
 
1594
  }
 
1595
}