public final class Utility
extends java.lang.Object
Sorry, basically this is mess which cries for cleanup.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
BUILT_DATE
Built date of this class.
|
static java.util.Comparator<java.lang.String> |
CASE_IGNORING_STRING_COMPARATOR
General comparator for strings, which ignores case.
|
static java.lang.String |
DEFAULT_RESOURCE_PATH
Default resource path.
|
static java.net.URL[] |
EMPTY_URL_ARRAY
Empty URL array.
|
static java.awt.image.ImageObserver |
NULL_OBSERVER
Image observer which ignores everything.
|
static float |
PHYS_BLUE_SCALE
Physiolocigal brightness value for blue.
|
static float |
PHYS_GREEN_SCALE
Physiolocigal brightness value for green.
|
static float |
PHYS_RED_SCALE
Physiolocigal brightness value for red.
|
static java.lang.String |
RELEASE_DATE
The current version of the library.
|
Constructor and Description |
---|
Utility() |
Modifier and Type | Method and Description |
---|---|
static void |
addEventQueueExceptionListener(EventQueueExceptionListener listener)
Add an exception listener which is called when an exception occurs during the
dispatch of an AWT event.
|
static boolean |
addToSystemClassLoader(java.net.URL... urls)
Add URLs tro the system class loader.
|
static boolean |
areWeOnDOS()
Deprecated.
name is outdated, use
areWeOnWindows() instead |
static boolean |
areWeOnWindows()
Test whether our System Windows-based.
|
static <T> java.util.List<T> |
asList(T... array)
Deprecated.
Use
Types.asList(Object[]) instead. |
static <T> java.util.List<T> |
asList(T[] array,
int length)
Deprecated.
Use
Types.asList(Object[], int) instead. |
static <T> java.util.List<T> |
asList(T[] array,
int start,
int length)
Deprecated.
Use
Types.asList(Object[], int, int) instead. |
static java.lang.String |
compileString(java.lang.String format,
java.lang.Object... args)
Compile a formatted string with maximum 10 args.
|
static java.lang.String |
compileString(java.lang.String tag,
java.lang.Object[] args,
java.util.ResourceBundle res)
Compile a formatted string with maximum 10 args.
|
static void |
copyImageToClipboard(java.awt.Image image)
Copy an image to the system clipboard.
|
static void |
copyTextToClipboard(java.lang.String text)
Copy text to the system clipboard.
|
static java.awt.Cursor |
createCustomCursor(javax.swing.Icon icon,
java.awt.Color bgColor,
java.awt.Point hotspot,
java.lang.String name,
java.awt.Cursor fallback)
Try to create a custom cursor from an icon.
|
static java.awt.Cursor |
createCustomCursor(javax.swing.Icon icon,
java.awt.Color bgColor,
java.lang.String name,
java.awt.Cursor fallback)
Try to create a custom cursor from an icon.
|
static java.awt.image.BufferedImage |
createFastImage(int width,
int height)
Try to create an image on which graphics operations have high performance.
|
static java.awt.image.VolatileImage |
createFastVolatileImage(java.awt.Component comp,
int width,
int height)
This will create a
VolatileImage and try to increase its acceleration priority. |
static java.io.OutputStream |
createOptionalZip(java.lang.String outPath,
java.lang.String substExt)
Create a stream which possibly points into a newly created ZIP file.
|
static java.awt.image.BufferedImage |
createPlatformImage(int width,
int height)
Create an opaque buffered image which is optimized for the current platform.
|
static java.awt.image.BufferedImage |
createPlatformImage(int width,
int height,
int transparency)
Create a buffered image which is optimized for the current platform.
|
static void |
debug(java.lang.Object x)
Print message if debug mode is on.
|
static void |
debug(java.lang.Throwable x)
Print the stack trace if debug mode is on.
|
static boolean |
equalBytes(byte[] arr1,
int off1,
byte[] arr2,
int off2,
int len)
Compare two byte arrays.
|
static byte[] |
fromHexData(java.lang.String hexData)
Convert a hex data stream to a byte array.
|
static boolean |
getBooleanParameter(java.lang.String key,
boolean def)
Look for a boolean applet parameter or application property.
|
static boolean |
getBooleanParameter(java.lang.String key,
boolean def,
java.lang.String... trueValues)
Look for a boolean applet parameter or application property.
|
static java.lang.String |
getBuildDate()
Use
getBuildDate(Class) using this class to get a built date. |
static java.lang.String |
getBuildDate(java.lang.Class<?> clazz)
Get the build date of a given class.
|
static java.lang.String |
getBuildDate(java.lang.Class<?> clazz,
java.lang.String defaultValue)
Get the build date of a given class.
|
static java.lang.String |
getBuildDate(java.lang.String defaultValue)
Use
getBuildDate(Class) using this class to get a built date. |
static float |
getColorBrightnessDistance(java.awt.Color color1,
java.awt.Color color2)
Get the distance between two colors by comparing their brightness.
|
static java.awt.Color |
getColorParameter(java.lang.String key,
java.awt.Color def)
Look for a color applet parameter or application property.
|
static double |
getDoubleParameter(java.lang.String key,
double def)
Look for a double applet parameter or application property.
|
static <T extends java.lang.Enum<?>> |
getEnumParameter(java.lang.String key,
T def)
Get an enum value from an applet parameter or application property.
|
static java.lang.String |
getErrorMessage(java.lang.Throwable x)
Deprecated.
use
Debug.getErrorMessage(Throwable) instead |
static java.lang.ClassLoader |
getExtensionClassLoader()
Get the class loader for extension classes.
|
static float |
getFloatParameter(java.lang.String key,
float def)
Look for a float applet parameter or application property.
|
static java.awt.FontMetrics |
getFontMetrics(java.awt.Font font)
Get a font metrics.
|
static java.awt.Frame |
getFrame(java.awt.Component comp)
Method to get the frame parent of any component.
|
static int |
getIntParameter(java.lang.String key,
int def)
Look for an integer applet parameter or application property.
|
static int |
getIntParameter(java.lang.String key,
int def,
int base)
Look for an integer applet parameter or application property.
|
static PhysicalLength |
getLengthParameter(java.lang.String key,
PhysicalLength def)
Look for a length applet parameter or application property.
|
static long |
getLongParameter(java.lang.String key,
long def)
Look for a long applet parameter of application property.
|
static long |
getLongParameter(java.lang.String key,
long def,
int base)
Look for a long applet parameter of application property.
|
static float |
getPhysiologicalBrightness(java.awt.Color color)
Get the brightness of a color.
|
static float |
getPhysiologicalBrightness(int red,
int green,
int blue)
Get the physiological brightness of a RGB color.
|
static java.awt.Color |
getPhysiologicalGray(java.awt.Color color)
Get a gray with the same brightness as a given color.
|
static Dict<java.lang.String,ParameterTypes> |
getRequestedParameters()
Get the requested parameters.
|
static java.awt.image.BufferedImage |
getRootComponentImage(java.awt.Component component)
Get a screen shot of a frame or dialog.
|
static java.util.List<Pair<java.lang.String>> |
getSortedSystemProperties()
Get a list of system properties and their values, sorted alphabetically by property name.
|
static java.lang.String |
getStackDump(java.lang.Throwable x)
Deprecated.
use
Debug.getStackDump(Throwable) instead |
static java.lang.String |
getStringParameter(java.lang.String key,
java.lang.String def)
Look for a String applet parameter or application property.
|
static boolean |
globEquals(java.lang.String mask,
java.lang.String str)
An equal function which accepts globbing.
|
static boolean |
isDebug()
Get the debug mode.
|
static boolean |
isNullOrEmpty(java.lang.String str)
Is a string
null or empty? |
static java.awt.Image |
loadImage(java.lang.String path)
Load an image and prepare a representation.
|
static java.awt.Image |
loadImage(java.lang.String path,
java.awt.Component renderer)
Load an image and prepare a representation.
|
static java.awt.Image |
loadImage(java.lang.String path,
java.awt.Component renderer,
java.awt.image.ImageObserver observer)
Load an image and prepare a representation.
|
static java.awt.Image |
loadImage(java.lang.String path,
java.awt.image.ImageObserver observer)
Load an image and prepare a representation.
|
static java.lang.String |
loadText(java.lang.String path)
Load a text file into a string.
|
static void |
main(java.lang.String[] args)
Print out revisions and built dates.
|
static java.net.URL |
openResourceViaClass(java.lang.String classResource)
Open a resource via a class.
|
static void |
printProperties()
In debug mode: print properties to console.
|
static void |
printProperty(java.lang.String prop)
Print a given property to the console.
|
static void |
removeEventQueueExceptionListener(EventQueueExceptionListener listener)
Remove an exception listener which was called when an exception occurs during the
dispatch of an AWT event.
|
static void |
setDebug(boolean mode)
Set the debugging mode.
|
static void |
setExtensionClassLoadUrls(java.net.URL... urls)
Update the extension class load mechanism by adding JAR files and file locations
to load classes.
|
static void |
setResourceDir(java.lang.String dir)
Set the resource directory.
|
static java.lang.String |
spaces(int num)
Get a string consisting of a given number of space characters.
|
static java.lang.String |
toASCII(java.lang.String str)
Get a string where all non-ASCII and non-printable characters are escaped by their Unicode escapes.
|
static java.lang.String |
toHexDump(byte[] data)
Expand a byte array into a hexduimp text structure.
|
static void |
updateExtensionClassLoader(Function1<java.lang.ClassLoader,java.lang.ClassLoader> updater)
Update the extension class loader.
|
public static final java.lang.String RELEASE_DATE
public static final float PHYS_RED_SCALE
public static final float PHYS_GREEN_SCALE
public static final float PHYS_BLUE_SCALE
public static final java.awt.image.ImageObserver NULL_OBSERVER
public static final java.lang.String DEFAULT_RESOURCE_PATH
public static final java.lang.String BUILT_DATE
public static final java.net.URL[] EMPTY_URL_ARRAY
@NotNull public static final java.util.Comparator<java.lang.String> CASE_IGNORING_STRING_COMPARATOR
public static void setDebug(boolean mode)
mode
- new modepublic static boolean isDebug()
@NotNull public static java.lang.ClassLoader getExtensionClassLoader()
updateExtensionClassLoader(Function1)
public static void updateExtensionClassLoader(@NotNull Function1<java.lang.ClassLoader,java.lang.ClassLoader> updater)
This special way of setting the extension class loader
is chosen to support clean synchronization.
Changing the class load mechanism requires certain rights and might not work in restricted environments like applets.
updater
- updater function which is called with the current extension class loader as argument
and has to return the new extension class loader. The new class loader should use the
old extension class loader as its parent to allow for nested extensions. The updater
function must not return null
public static boolean addToSystemClassLoader(@NotNull java.net.URL... urls)
This is an evil hack, but allows to add libraries at runtime.
This method takes care of loading additional JARs which are linked via the Class_path Manifest property, but not recursively.
It will not work in secure restricted environments like applets.
urls
- JAR or class directory URLs to add to the system class loadertrue
: if this worked for all URLsfalse
: if it failed, probably due to security restrictionspublic static void setExtensionClassLoadUrls(@NotNull java.net.URL... urls)
This method takes care of loading additional JARs which are linked via the Class_path Manifest property, but not recursively.
Changing the class load mechanism requires certain rights and might not work in restricted environments like applets.
urls
- URLs of local JAR files and class directories, net URLs are not accepted by the underlying
Java class loader extension mechanism@Nullable public static java.awt.Image loadImage(@NotNull java.lang.String path)
path
- path of the image file (may include loader class prepended with a |
(pipe symbol) separatornull
if the image was not found or not accessible@Nullable public static java.awt.Image loadImage(@NotNull java.lang.String path, @Nullable java.awt.image.ImageObserver observer)
path
- path of the image file (may include loader class prepended with a |
(pipe symbol) separatorobserver
- image observernull
if the image was not found or not accessible@Nullable public static java.awt.Image loadImage(@NotNull java.lang.String path, @NotNull java.awt.Component renderer)
path
- path of the image file (may include loader class prepended with a |
(pipe symbol) separatorrenderer
- renderer component for image creationnull
if the image was not found or not accessible@Nullable public static java.awt.Image loadImage(@NotNull java.lang.String path, @NotNull java.awt.Component renderer, @Nullable java.awt.image.ImageObserver observer)
path
- path of the image file (may include loader class prepended with a |
(pipe symbol) separatorrenderer
- renderer component for image creationobserver
- image observernull
if the image was not found or not accessible@NotNull @Deprecated public static java.lang.String getStackDump(@NotNull java.lang.Throwable x)
Debug.getStackDump(Throwable)
insteadx
- the ThrowablegetErrorMessage(Throwable)
@NotNull @Deprecated public static java.lang.String getErrorMessage(@NotNull java.lang.Throwable x)
Debug.getErrorMessage(Throwable)
insteadx
- exceptiongetStackDump(Throwable)
@NotNull public static java.lang.String loadText(@NotNull java.lang.String path)
path
- name of the text file@Deprecated public static boolean areWeOnDOS()
areWeOnWindows()
insteadtrue
we are on Windows/DOSfalse
we are elsewherepublic static boolean areWeOnWindows()
true
we are running on Windowsfalse
we are running elsewherepublic static void setResourceDir(@Nullable java.lang.String dir)
dir
- the image directory@NotNull public static java.lang.String compileString(@NotNull java.lang.String tag, @NotNull java.lang.Object[] args, @NotNull java.util.ResourceBundle res)
Special signs: %# where hash is a digit from 0 to 9 means insert arg # @# where hash is a digit from 0 to 9 means insert localized arg # %% means % @@ means @
tag
- resource tag for format stringargs
- arguments for insertionres
- active resource bundlepublic static java.lang.String compileString(@NotNull java.lang.String format, java.lang.Object... args)
Special signs: %# where hash is a digit from 0 to 9 means insert arg # %% means %
format
- format stringargs
- arguments for insertion@NotNull public static java.awt.Frame getFrame(@Nullable java.awt.Component comp)
comp
- the component to search the frame forpublic static boolean equalBytes(@NotNull byte[] arr1, int off1, @NotNull byte[] arr2, int off2, int len)
len
bytes from array 1 starting with offset 1
with len
bytes from array 2 starting with offset 2.
Will return always true
for len ≤= 0
arr1
- array 1off1
- offset 1arr2
- array 2off2
- offset 2len
- length to comparetrue
if both chunks are equalfalse
otherwisepublic static boolean getBooleanParameter(@NotNull java.lang.String key, boolean def)
true
is "true"
.key
- parameter keydef
- default valuepublic static boolean getBooleanParameter(@NotNull java.lang.String key, boolean def, @NotNull java.lang.String... trueValues)
true
.key
- parameter keydef
- default valuetrueValues
- values to be used for true
return, compared case-insensitively.@NotNull public static <T extends java.lang.Enum<?>> T getEnumParameter(@NotNull java.lang.String key, @NotNull T def)
T
- enum typekey
- key for the value, expected to evaluate to the name of an enum of the required typedef
- default value, also defining the type of enum return by this methoddef
if unset or value is invalidpublic static java.lang.String getStringParameter(@NotNull java.lang.String key, @Nullable java.lang.String def)
key
- parameter keydef
- default value@Nullable public static java.awt.Color getColorParameter(@NotNull java.lang.String key, @Nullable java.awt.Color def)
key
- parameter keydef
- default valuepublic static int getIntParameter(@NotNull java.lang.String key, int def)
key
- parameter keydef
- default valuepublic static int getIntParameter(@NotNull java.lang.String key, int def, int base)
key
- parameter keydef
- default valuebase
- number basepublic static long getLongParameter(@NotNull java.lang.String key, long def)
key
- parameter keydef
- default valuepublic static long getLongParameter(@NotNull java.lang.String key, long def, int base)
key
- parameter keydef
- default valuebase
- number basepublic static double getDoubleParameter(@NotNull java.lang.String key, double def)
Uses percentage if ending with %.
key
- parameter keydef
- default valuepublic static float getFloatParameter(@NotNull java.lang.String key, float def)
Uses percentage if ending with %.
key
- parameter keydef
- default valuepublic static PhysicalLength getLengthParameter(@NotNull java.lang.String key, PhysicalLength def)
Uses percentage if ending with %.
key
- parameter keydef
- default valuepublic static void debug(@Nullable java.lang.Object x)
x
- object which's toString is calledpublic static void debug(@NotNull java.lang.Throwable x)
x
- exceptionpublic static void printProperty(@NotNull java.lang.String prop)
prop
- poperty namepublic static void printProperties()
public static boolean globEquals(@NotNull java.lang.String mask, @NotNull java.lang.String str)
mask
- glob mask (containing special chars)str
- string to be checked against maskpublic static void addEventQueueExceptionListener(@NotNull EventQueueExceptionListener listener)
listener
- listener to addpublic static void removeEventQueueExceptionListener(@NotNull EventQueueExceptionListener listener)
listener
- listener to remove@NotNull public static java.awt.Cursor createCustomCursor(@NotNull javax.swing.Icon icon, @NotNull java.awt.Color bgColor, @NotNull java.lang.String name, @NotNull java.awt.Cursor fallback)
icon
- icon from which to create the cursorbgColor
- background color for cursorname
- name for accessibilityfallback
- fallback cursor taken if the image size is not supported@NotNull public static java.awt.Cursor createCustomCursor(@NotNull javax.swing.Icon icon, @NotNull java.awt.Color bgColor, @Nullable java.awt.Point hotspot, @NotNull java.lang.String name, @NotNull java.awt.Cursor fallback)
icon
- icon from which to create the cursorbgColor
- background color for cursorhotspot
- hot spot of cursor (if null
the center of the icon is taken)name
- name for accessibilityfallback
- fallback cursor taken if the image size is not supportedpublic static java.awt.FontMetrics getFontMetrics(@NotNull java.awt.Font font)
font
- font for which a metrics is requested@NotNull public static java.awt.image.BufferedImage createPlatformImage(int width, int height)
Please note that it's always a good idea to call the image's flush()
method
before it is discarded.
width
- image widthheight
- image height@NotNull public static java.awt.image.BufferedImage createPlatformImage(int width, int height, int transparency)
Please note that the transparency type usually has an impact on performance. Best is opaque, followed by bitmap, and translucent images may be a drawn a lot slower.
Regardless of that it's always a good idea to call the image's flush()
method
before it is discarded.
width
- image widthheight
- image heighttransparency
- transparency type, either Transparency.OPAQUE
,
Transparency.BITMASK
, or Transparency.TRANSLUCENT
@Nullable public static java.awt.image.BufferedImage createFastImage(int width, int height)
Note that it's always a good idea to call the image's flush()
method
before it is discarded.
width
- image widthheight
- image heightnull
if the PerfGraphics lib is not available@NotNull public static java.awt.image.VolatileImage createFastVolatileImage(@NotNull java.awt.Component comp, int width, int height)
VolatileImage
and try to increase its acceleration priority.
Note that it's always a good idea to call the image's flush()
method
before it is discarded.
comp
- component from which to create imagewidth
- image widthheight
- image heightpublic static float getPhysiologicalBrightness(@NotNull java.awt.Color color)
color
- color which brightness is needed0
(black) and 1.0
(white)public static float getPhysiologicalBrightness(int red, int green, int blue)
0
to 255
,
otherwise the return of this method is useless.red
- red color valuegreen
- green color valueblue
- blue color value0
(black) and 1
(white)@NotNull public static java.awt.Color getPhysiologicalGray(@NotNull java.awt.Color color)
color
- color to convert to graypublic static float getColorBrightnessDistance(@NotNull java.awt.Color color1, @NotNull java.awt.Color color2)
color1
- first colorcolor2
- second color0
(same brightness) and 1
(completely different brightness)@NotNull public static java.util.List<Pair<java.lang.String>> getSortedSystemProperties()
Note that in restricted environments it's possible that not all properties are accessible. In that case only some properties are returned.
@NotNull public static java.lang.String toASCII(@NotNull java.lang.String str)
str
- stringpublic static boolean isNullOrEmpty(@Nullable java.lang.String str)
null
or empty?str
- string to checktrue
if the string is null
or emptyfalse
otherwisepublic static void copyImageToClipboard(@NotNull java.awt.Image image)
image
- the image to copyjava.lang.IllegalStateException
- if the clipboard is currently unavailablepublic static void copyTextToClipboard(@NotNull java.lang.String text)
text
- the text to copyjava.lang.IllegalStateException
- if the clipboard is currently unavailable@Deprecated @NotNull @SafeVarargs public static <T> java.util.List<T> asList(@NotNull T... array)
Types.asList(Object[])
instead.Arrays.asList()
,
useful if only an unmodifiable list is needed.
Changes of the array will be reflected in the list return from this method, so the safest use is as a temporary object.
T
- element type of array and listarray
- the array to wrap into a list view@Deprecated @NotNull public static <T> java.util.List<T> asList(@NotNull T[] array, int length)
Types.asList(Object[], int)
instead.Arrays.asList()
,
useful if only an unmodifiable list is needed.
Changes of the array will be reflected in the list returned from this method, so the safest use is as a temporary object.
T
- element type of array and listarray
- the array to wrap into a list viewlength
- the number of elements to be used from the array@Deprecated @NotNull public static <T> java.util.List<T> asList(@NotNull T[] array, int start, int length)
Types.asList(Object[], int, int)
instead.Arrays.asList()
,
useful if only an unmodifiable list is needed.
Changes of the array will be reflected in the list returned from this method.
T
- element type of array and listarray
- the array to wrap into a list viewstart
- index of first used element of the arraylength
- the number of elements to be used from the array@Nullable public static java.awt.image.BufferedImage getRootComponentImage(@NotNull java.awt.Component component)
component
- component in the frame or dialognull
if there have been problems with the graphical setup@Nullable public static java.lang.String getBuildDate(@NotNull java.lang.Class<?> clazz)
Built-Date
property is set in the Manifest file
of the jar which provides the class.clazz
- class for accessing the associated manifestnull
if no manifest is found@NotNull public static java.lang.String getBuildDate(@NotNull java.lang.Class<?> clazz, @NotNull java.lang.String defaultValue)
Built-Date
property is set in the Manifest file
of the jar which provides the class.clazz
- class for accessing the associated manifestdefaultValue
- default value returned if no manifest is founddefaultValue
if no manifest is found@Nullable public static java.lang.String getBuildDate()
getBuildDate(Class)
using this class to get a built date.null
if no Manifest is found@NotNull public static java.lang.String getBuildDate(@NotNull java.lang.String defaultValue)
getBuildDate(Class)
using this class to get a built date.defaultValue
- default value returned if no manifest is founddefaultValue
if no manifest is found@NotNull public static java.io.OutputStream createOptionalZip(@NotNull java.lang.String outPath, @NotNull java.lang.String substExt) throws java.io.IOException
outPath
- Output path.
If it ends with a .zip
extension the output stream goes into a zip file
with the given name.substExt
- if a zip file is created, the first entry will get this extension instead of .zip
java.io.IOException
- on errors opening the file or creating the zip header@NotNull public static java.lang.String toHexDump(@NotNull byte[] data)
data
- byte array@NotNull public static byte[] fromHexData(@NotNull java.lang.String hexData)
This extracts hexadecimal digits from the string, converts each pair to a byte, and collects these into a byte array.
White space is accepted, but non-hexadecimal digits will result in a NumberFormatException.
hexData
- hexadecimal digit text@Nullable public static java.net.URL openResourceViaClass(@NotNull java.lang.String classResource)
classResource
- class name and resource URL string separated by a |
characternull
if the resource is invalid or couldn't be opened@NotNull public static java.lang.String spaces(int num)
num
- number of spaces@NotNull public static Dict<java.lang.String,ParameterTypes> getRequestedParameters()
Parameters are requested using the following static methods of this class:
getStringParameter(String, String)
getBooleanParameter(String, boolean)
getBooleanParameter(String, boolean, String...)
getIntParameter(String, int)
getIntParameter(String, int, int)
getLongParameter(String, long)
getLongParameter(String, long, int)
getDoubleParameter(String, double)
getFloatParameter(String, float)
getEnumParameter(String, Enum)
getColorParameter(String, Color)
getLengthParameter(String, PhysicalLength)
public static void main(@NotNull java.lang.String[] args)
Implementation-Version
is included in the Manifest)
and the release date constant of this class
and of all classes which complete names are found in the arguments.args
- complete class names like de.caff.util.Utility