GlueGen: src/java/com/jogamp/common/util/SourcedInterruptedException.java Source File

GlueGen v2.6.0 GlueGen, Native Binding Generator for Java™ (public API).

SourcedInterruptedException.java
Go to the documentation of this file.
1/**
2 * Copyright 2015 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31import java.io.PrintStream;
32
33import com.jogamp.common.ExceptionUtils;
34import com.jogamp.common.ExceptionUtils.CustomStackTrace;
35
36/**
37 * {@link InterruptedException}, which may include the source, see {@link #getInterruptSource()}.
38 * <p>
39 * This exception may be created directly where {@link #getCause()} returns {@code null},
40 * or by propagating an existing {@link InterruptedException} as returned by {@link #getCause()}.
41 * </p>
42 * @since 2.3.2
43 */
44@SuppressWarnings("serial")
45public class SourcedInterruptedException extends InterruptedException implements CustomStackTrace {
46 final Throwable interruptSource;
47
48 /**
49 * Wraps the given {@link InterruptedException} into a {@link SourcedInterruptedException}
50 * if it is not yet of the desired type and
51 * if the current thread if a {@link InterruptSource}, i.e. the source is known.
52 * <p>
53 * Otherwise the given {@link InterruptedException} instance is returned.
54 * </p>
55 * <p>
56 * In case method is creating a new wrapping instance,
57 * {@link InterruptSource#clearInterruptSource()} is being issued.
58 * </p>
59 *
60 * @param ie the to be wrapped {@link InterruptedException}
61 */
62 public static InterruptedException wrap(final InterruptedException ie) {
63 return wrap(ie, InterruptSource.Util.currentThread());
64 }
65
66 /**
67 * Wraps the given {@link InterruptedException} into a {@link SourcedInterruptedException}
68 * if it is not yet of the same type and if {@code source} is not {@code null}.
69 * <p>
70 * Otherwise the given {@link InterruptedException} instance is returned.
71 * </p>
72 * <p>
73 * In case method is creating a new wrapping instance,
74 * {@link InterruptSource#clearInterruptSource()} is being issued.
75 * </p>
76 *
77 * @param ie the to be wrapped {@link InterruptedException}
78 * @param source the {@link InterruptSource}
79 */
80 public static InterruptedException wrap(final InterruptedException ie, final InterruptSource source) {
81 if( !(ie instanceof SourcedInterruptedException) && null != source ) {
82 return new SourcedInterruptedException(ie, source.getInterruptSource(true));
83 } else {
84 return ie;
85 }
86 }
87
88 /**
89 * @param message mandatory message of this exception
90 * @param cause optional propagated cause
91 * @param interruptSource optional propagated source of {@link Thread#interrupt()} call
92 */
93 public SourcedInterruptedException(final String message, final InterruptedException cause, final Throwable interruptSource) {
94 super(message);
95 if( null != cause ) {
96 initCause(cause);
97 }
98 this.interruptSource = interruptSource;
99 }
100
101 /**
102 * @param cause mandatory propagated cause
103 * @param interruptSource optional propagated source of {@link Thread#interrupt()} call
104 */
105 public SourcedInterruptedException(final InterruptedException cause, final Throwable interruptSource) {
106 super(cause.getMessage());
107 initCause(cause);
108 this.interruptSource = interruptSource;
109 }
110
111 /**
112 * Returns the source of the {@link Thread#interrupt()} call if known,
113 * otherwise {@code null} is returned.
114 */
115 public final Throwable getInterruptSource() {
116 return interruptSource;
117 }
118
119 /**
120 * Returns the propagated {@link InterruptedException}, i.e. the cause of this exception,
121 * or {@code null} if not applicable.
122 * <p>
123 * {@inheritDoc}
124 * </p>
125 */
126 @Override
127 public InterruptedException getCause() {
128 return (InterruptedException)super.getCause();
129 }
130
131 @Override
132 public String toString() {
133 final StringBuilder sb = new StringBuilder(256);
134 sb.append(getClass().getSimpleName()).append(": ");
135 if (null != interruptSource) {
136 sb.append("[sourced]");
137 } else {
138 sb.append("[unknown]");
139 }
140 final String m = getLocalizedMessage();
141 if( null != m ) {
142 sb.append(" ").append(m);
143 }
144 return sb.toString();
145 }
146
147 @Override
148 public final void printCauseStack(final PrintStream s, final String causeStr, final int causeIdx, final int stackDepth) {
149 final String s0 = causeStr+"["+causeIdx+"]";
150 s.println(s0+" by "+getClass().getSimpleName()+": "+getMessage()+" on thread "+Thread.currentThread().getName());
151 ExceptionUtils.dumpStack(s, getStackTrace(), 0, stackDepth);
152 if( null != interruptSource ) {
153 ExceptionUtils.printCause(s, s0, interruptSource, 0, 1, stackDepth);
154 }
155 }
156
157 @Override
158 public final void printStackTrace(final PrintStream s, final int causeDepth, final int stackDepth) {
159 s.println(getClass().getSimpleName()+": "+getMessage()+" on thread "+Thread.currentThread().getName());
160 ExceptionUtils.dumpStack(s, getStackTrace(), 0, stackDepth);
161 ExceptionUtils.printCause(s, "Caused", getCause(), 0, causeDepth, stackDepth);
162 if( null != interruptSource ) {
163 ExceptionUtils.printCause(s, "InterruptSource", interruptSource, 0, causeDepth, stackDepth);
164 }
165 }
166}
com.jogamp.common.ExceptionUtils
Definition: ExceptionUtils.java:35
com.jogamp.common.ExceptionUtils.printCause
static int printCause(final PrintStream s, final String causeStr, Throwable cause, final int causeIdx, final int causeDepth, final int stackDepth)
Prints the given Throwable cause to the output PrintStream s.
Definition: ExceptionUtils.java:94
com.jogamp.common.ExceptionUtils.dumpStack
static void dumpStack(final PrintStream out)
Definition: ExceptionUtils.java:36
com.jogamp.common.util.InterruptSource.Util
Definition: InterruptSource.java:54
com.jogamp.common.util.InterruptSource.Util.currentThread
static InterruptSource currentThread()
Casts current java.lang.Thread to InterruptSource if applicable, otherwise returns null.
Definition: InterruptSource.java:70
com.jogamp.common.util.SourcedInterruptedException
InterruptedException, which may include the source, see getInterruptSource().
Definition: SourcedInterruptedException.java:45
com.jogamp.common.util.SourcedInterruptedException.printStackTrace
final void printStackTrace(final PrintStream s, final int causeDepth, final int stackDepth)
Custom printStackTrace method, similar to Throwable#printStackTrace(PrintStream, int,...
Definition: SourcedInterruptedException.java:158
com.jogamp.common.util.SourcedInterruptedException.getInterruptSource
final Throwable getInterruptSource()
Returns the source of the Thread#interrupt() call if known, otherwise null is returned.
Definition: SourcedInterruptedException.java:115
com.jogamp.common.util.SourcedInterruptedException.toString
String toString()
Definition: SourcedInterruptedException.java:132
com.jogamp.common.util.SourcedInterruptedException.SourcedInterruptedException
SourcedInterruptedException(final InterruptedException cause, final Throwable interruptSource)
Definition: SourcedInterruptedException.java:105
com.jogamp.common.util.SourcedInterruptedException.printCauseStack
final void printCauseStack(final PrintStream s, final String causeStr, final int causeIdx, final int stackDepth)
Prints this Throwable as a cause to the output PrintStream s, not iterating over all inner causes!
Definition: SourcedInterruptedException.java:148
com.jogamp.common.util.SourcedInterruptedException.getCause
InterruptedException getCause()
Returns the propagated InterruptedException, i.e.
Definition: SourcedInterruptedException.java:127
com.jogamp.common.util.SourcedInterruptedException.wrap
static InterruptedException wrap(final InterruptedException ie, final InterruptSource source)
Wraps the given InterruptedException into a SourcedInterruptedException if it is not yet of the same ...
Definition: SourcedInterruptedException.java:80
com.jogamp.common.util.SourcedInterruptedException.SourcedInterruptedException
SourcedInterruptedException(final String message, final InterruptedException cause, final Throwable interruptSource)
Definition: SourcedInterruptedException.java:93
com.jogamp.common.util.SourcedInterruptedException.wrap
static InterruptedException wrap(final InterruptedException ie)
Wraps the given InterruptedException into a SourcedInterruptedException if it is not yet of the desir...
Definition: SourcedInterruptedException.java:62
com.jogamp.common.ExceptionUtils.CustomStackTrace
Interface allowing Throwable specializations to provide their custom stack trace presentation.
Definition: ExceptionUtils.java:64
com.jogamp.common.util.InterruptSource
Interface exposing java.lang.Thread#interrupt() source, intended for java.lang.Thread specializations...
Definition: InterruptSource.java:36
com.jogamp.common.util.InterruptSource.getInterruptSource
Throwable getInterruptSource(final boolean clear)
Returns the source of the last interrupt() call.