001/* 002 * ============================================================================ 003 * Copyright © 2002-2022 by Thomas Thrien. 004 * All Rights Reserved. 005 * ============================================================================ 006 * Licensed to the public under the agreements of the GNU Lesser General Public 007 * License, version 3.0 (the "License"). You may obtain a copy of the License at 008 * 009 * http://www.gnu.org/licenses/lgpl.html 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 014 * License for the specific language governing permissions and limitations 015 * under the License. 016 */ 017 018package org.tquadrat.foundation.lang; 019 020import static org.apiguardian.api.API.Status.STABLE; 021import static org.tquadrat.foundation.lang.Objects.isNull; 022import static org.tquadrat.foundation.lang.Objects.requireNonNullArgument; 023import static org.tquadrat.foundation.lang.Objects.requireNotBlankArgument; 024 025import java.lang.Thread.UncaughtExceptionHandler; 026 027import org.apiguardian.api.API; 028import org.tquadrat.foundation.annotation.ClassVersion; 029 030/** 031 * <p>{@summary An implementation of 032 * {@link ThreadGroup} 033 * that allows to configure the behaviour of 034 * {@link #uncaughtException(Thread, Throwable)}}.</p> 035 * <p>This class is not final, to allow further modifications.</p> 036 * 037 * @extauthor Thomas Thrien - thomas.thrien@tquadrat.org 038 * @version $Id: ThreadGroupExt.java 1118 2024-03-15 16:14:15Z tquadrat $ 039 * @since 0.1.0 040 * 041 * @UMLGraph.link 042 */ 043@ClassVersion( sourceVersion = "$Id: ThreadGroupExt.java 1118 2024-03-15 16:14:15Z tquadrat $" ) 044@API( status = STABLE, since = "0.1.0" ) 045public class ThreadGroupExt extends ThreadGroup 046{ 047 /*------------*\ 048 ====** Attributes **======================================================= 049 \*------------*/ 050 /** 051 * The 052 * {@link java.lang.Thread.UncaughtExceptionHandler} 053 * for this thread group. It can be {@code null}. 054 */ 055 private final UncaughtExceptionHandler m_UncaughtExceptionHandler; 056 057 /*--------------*\ 058 ====** Constructors **===================================================== 059 \*--------------*/ 060 /** 061 * <p>{@summary Constructs a new thread group.}</p> 062 * <p>The parent of this new group is the thread group of the currently 063 * running thread.</p> 064 * <p>The behaviour of 065 * {@link #uncaughtException(Thread, Throwable)} 066 * is that of the superclass.</p> 067 * 068 * @param name The name of the new thread group. 069 * @throws IllegalArgumentException The {@code name} argument is either 070 * {@code null}, empty or blank. 071 * @throws SecurityException The current thread cannot create a thread 072 * in this thread group. 073 */ 074 public ThreadGroupExt( final String name ) throws IllegalArgumentException 075 { 076 super( requireNotBlankArgument( name, "name" ) ); 077 m_UncaughtExceptionHandler = null; 078 } // ThreadGroupExt() 079 080 /** 081 * <p>{@summary Constructs a new thread group.}</p> 082 * <p>The parent of this new group is the specified thread group.</p> 083 * <p>The behaviour of 084 * {@link #uncaughtException(Thread, Throwable)} 085 * is that of the superclass.</p> 086 * 087 * @param parent The parent thread group. 088 * @param name The name of the new thread group. 089 * @throws IllegalArgumentException The {@code name} argument is either 090 * {@code null}, empty or blank, or the {@code parent} thread group 091 * argument is {@code null}. 092 * @throws SecurityException The current thread cannot create a thread 093 * in this thread group. 094 */ 095 public ThreadGroupExt( final ThreadGroup parent, final String name ) throws IllegalArgumentException 096 { 097 super( requireNonNullArgument( parent, "parent" ), requireNotBlankArgument( name, "name" ) ); 098 m_UncaughtExceptionHandler = null; 099 } // ThreadGroupExt() 100 101 /** 102 * <p>{@summary Constructs a new thread group.}</p> 103 * <p>The parent of this new group is the thread group of the currently 104 * running thread.</p> 105 * <p>The behaviour of 106 * {@link #uncaughtException(Thread, Throwable)} 107 * is determined by the provided handler.</p> 108 * 109 * @param name The name of the new thread group. 110 * @param handler The handler for the uncaught exceptions. 111 * @throws IllegalArgumentException The {@code name} argument is either 112 * {@code null}, empty or blank, or the {@code handler} argument is 113 * {@code null}. 114 * @throws SecurityException The current thread cannot create a thread 115 * in this thread group. 116 */ 117 public ThreadGroupExt( final String name, final UncaughtExceptionHandler handler ) throws IllegalArgumentException 118 { 119 super( requireNotBlankArgument( name, "name" ) ); 120 m_UncaughtExceptionHandler = requireNonNullArgument( handler, "handler" ); 121 } // ThreadGroupExt() 122 123 /** 124 * <p>{@summary Constructs a new thread group.}</p> 125 * <p>The parent of this new group is the specified thread group.</p> 126 * <p>The behaviour of 127 * {@link #uncaughtException(Thread, Throwable)} 128 * is determined by the provided handler.</p> 129 * 130 * @param parent The parent thread group. 131 * @param name The name of the new thread group. 132 * @param handler The handler for the uncaught exceptions. 133 * @throws IllegalArgumentException The {@code name} argument is either 134 * {@code null}, empty or blank, or one of the {@code parent} thread 135 * group or the {@code handler} arguments is {@code null}. 136 * @throws SecurityException The current thread cannot create a thread 137 * in this thread group. 138 */ 139 public ThreadGroupExt( final ThreadGroup parent, final String name, final UncaughtExceptionHandler handler ) throws IllegalArgumentException 140 { 141 super( requireNonNullArgument( parent, "parent" ), requireNotBlankArgument( name, "name" ) ); 142 m_UncaughtExceptionHandler = requireNonNullArgument( handler, "handler" ); 143 } // ThreadGroupExt() 144 145 /*---------*\ 146 ====** Methods **========================================================== 147 \*---------*/ 148 /** 149 * {@inheritDoc} 150 */ 151 @Override 152 public final void uncaughtException( final Thread t, final Throwable e ) 153 { 154 if( isNull( m_UncaughtExceptionHandler ) ) 155 { 156 super.uncaughtException( t, e ); 157 } 158 else 159 { 160 m_UncaughtExceptionHandler.uncaughtException( t, e ); 161 } 162 } // uncaughtException() 163} 164// class ThreadGroupExt 165 166/* 167 * End of File 168 */