Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 65b4a60c9fd3ae32f67e689c5f0effa468e7a377 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (c) 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.event;

import java.io.PrintStream;

/**
 * A {@link IListener listener} that dispatches throwable {@link ThrowableEvent events} to methods that can be
 * overridden by extenders.
 *
 * @author Eike Stepper
 * @apiviz.exclude
 * @since 3.3
 */
public class ThrowableEventAdapter implements IListener
{
  public ThrowableEventAdapter()
  {
  }

  public final void notifyEvent(IEvent event)
  {
    if (event instanceof ThrowableEvent)
    {
      ThrowableEvent e = (ThrowableEvent)event;
      notifyLifecycleEvent(e);
    }
    else
    {
      notifyOtherEvent(event);
    }
  }

  protected void notifyLifecycleEvent(ThrowableEvent event)
  {
    onThrowable(event.getSource(), event.getThrowable());
  }

  protected void notifyOtherEvent(IEvent event)
  {
  }

  protected void onThrowable(INotifier source, Throwable t)
  {
  }

  /**
   * Prints the stack traces of throwable {@link ThrowableEvent events} to a {@link PrintStream}.
   *
   * @author Eike Stepper
   * @apiviz.exclude
   */
  public static class ToPrintStream extends ThrowableEventAdapter
  {
    public static final ToPrintStream CONSOLE = new ToPrintStream(System.out);

    private final PrintStream out;

    public ToPrintStream(PrintStream out)
    {
      this.out = out;
    }

    public final PrintStream getOut()
    {
      return out;
    }

    @Override
    protected void onThrowable(INotifier source, Throwable t)
    {
      t.printStackTrace(out);
    }
  }
}

Back to the top