/*\
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
* _____________________________________________________________________________________________________________________
*
* This class is "Open Source" as defined by the Open Source Initiative (OSI). You can redistribute it and/or modify it
* under the terms of the BSD License. The license text is appended to the end of this file.
\*/
package de.klaro.base.util;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
/**
* Allgemeine Utility-Methoden.
*/
public class Util extends Object
{
/* ______________________________________________________________________________________________________________ *\
\* Konstanten */
/* ______________________________________________________________________________________________________________ *\
\* Klassenvariablen */
private static ByteArrayOutputStream $stackInfoStream = new ByteArrayOutputStream();
private static PrintWriter $stackInfoWriter = new PrintWriter($stackInfoStream,true);
private static Throwable $throwable = null;
private static int $id = 0;
private static PrintStream $panicOutput = System.err;
/* ______________________________________________________________________________________________________________ *\
\* Instanzvariablen */
/* ______________________________________________________________________________________________________________ *\
\* Konstruktoren */
/**
* Erzeugt eine Instanz dieser Klasse.
*
* Da verhindert werden soll, dass Instanzen dieser Klasse ausserhalb dieser Klasse erzeugt werdem, ist dieser
* Konstruktor 'private' deklariert.
*/
private Util()
{
super();
}
/* ______________________________________________________________________________________________________________ *\
\* Instanzmethoden */
/* ______________________________________________________________________________________________________________ *\
\* Klassenmethoden */
/**
* Liefert einen Stacktrace als String.
*
* @param throwable Das Throwable, welches den Stacktrace enthlt
* @return Der Stacktrace
*/
public synchronized static String stackTrace(Throwable throwable)
{
$stackInfoStream.reset();
throwable.printStackTrace($stackInfoWriter);
return $stackInfoStream.toString();
}
/**
* Liefert den aktuell gltigen Stacktrace als String.
*
* @return Der aktuell gltige Stacktrace
*/
public synchronized static String currentStackTrace()
{
if ($throwable == null) $throwable = new Throwable("stack info");
return stackTrace($throwable.fillInStackTrace());
}
/**
* Liefert den aktuell gltigen Stacktrace als String.
*
* @param message Die Meldung, die mit dem Stacktrace ausgegeben werden soll
* @return Der aktuell gltige Stacktrace
*/
public synchronized static String currentStackTrace(String message)
{
if ($throwable == null) $throwable = new Throwable(message);
return stackTrace($throwable.fillInStackTrace());
}
/**
* Setzt den Ausgabekanal, auf dem Panik-Meldungen ausgegeben werden sollen.
*
* @param panicOutput Der Ausgabekanal fr Panik-Meldungen
*/
public static void setPanicOutput(PrintStream panicOutput)
{
$panicOutput = panicOutput;
}
/**
* Gibt eine Panik-Meldung auf dem Panik-Ausgabekanal aus.
*
* Der Panik-Ausgabekanal ist der Standardfehlerkanal, wenn keine anderer Ausgabekanal definiert wurde.
*
* @param message Die Panik-Meldung
*/
public static void panic(String message)
{
$panicOutput.println("*** Panic: " + message);
}
/**
* Gibt eine Panik-Meldung auf dem Panik-Ausgabekanal aus und beendet das Programm.
*
* Der Panik-Ausgabekanal ist der Standardfehlerkanal, wenn keine anderer Ausgabekanal definiert wurde.
*
* @param message Die Panik-Meldung
* @param exitCode Der Exit-Code
*/
public static void panic(String message,int exitCode)
{
panic(message);
System.exit(exitCode);
}
/**
* Gibt eine Panik-Meldung auf dem Panik-Ausgabekanal aus.
*
* Der Panik-Ausgabekanal ist der Standardfehlerkanal, wenn keine anderer Ausgabekanal definiert wurde.
*
* @param throwable Das Throwable, dessen Meldung ausgegeben wird
*/
public static void panic(Throwable throwable)
{
panic(throwable.getMessage());
}
/**
* Gibt eine Panik-Meldung auf dem Panik-Ausgabekanal aus und beendet das Programm.
*
* Der Panik-Ausgabekanal ist der Standardfehlerkanal, wenn keine anderer Ausgabekanal definiert wurde.
*
* @param throwable Das Throwable, dessen Meldung ausgegeben wird
* @param exitCode Der Exit-Code
*/
public static void panic(Throwable throwable,int exitCode)
{
panic(throwable.getMessage(),exitCode);
}
/**
* Erzeugt einen String zur Reprsentation einer Exception.
*
* @param throwable Das Throwable, dessen Meldung ausgegeben wird
* @return Der String zur Reprsentation der Exception
*/
public static String toString(Throwable throwable)
{
return throwable.getClass().getName() + " (" + Strings.blankAsText(throwable.getMessage(),"no detail message") + ")";
}
/**
* Liefert eine eindeutige Zahl.
*
* Die Zahlen werden aufsteigend ab 0 erzeugt. Nachdem die grte Zahl (Integer.MAX_VALUE) geliefert wurde,
* ist die nchste Zahl die kleinste Zahl (Integer.MIN_VALUE).
*
* Fr den Fall, dass durchschnittlich jede Sekunde eine Zahl abgefordert wrde, reicht der Integer-Zahlenraum
* fr ca. 130 Jahre.
*
* @return Eine eindeutige Zahl
*/
public static int createID()
{
return $id++;
}
/**
* Der aktuelle Thread wird fr die angegebene Dauer in den Wait-State berfhrt.
*
* Der Thread kann mit einem Interrupt aus dem Wait-State geholt werden.
*
* @param duration Die Dauer in Millisekunden
*/
public static void idle(long duration)
{
try
{
Thread.sleep(duration);
}
catch (IllegalArgumentException e)
{
// Keine Aktion ntig
}
catch (InterruptedException e)
{
// Keine Aktion ntig
}
}
/**
* Der aktuelle Thread wird dauerhaft in den Wait-State berfhrt.
*
* Der Thread kann mit einem Interrupt aus dem Wait-State geholt werden.
*/
public static void idle()
{
Object sync = new Object();
while (true)
{
synchronized (sync)
{
try
{
sync.wait();
}
catch (InterruptedException e)
{
// Keine Aktion ntig
}
}
}
}
/* ______________________________________________________________________________________________________________ *\
\* Klassen */
}
/*\
* _____________________________________________________________________________________________________________________
*
* This software is distributed under the terms of the BSD License:
*
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of the Klaus Rogall nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* _____________________________________________________________________________________________________________________
\*/