public abstract class BaseUtilities extends Object
Modifier and Type | Field and Description |
---|---|
static int |
OS_AIX
Operating system is IBM AIX.
|
static int |
OS_DEC
Deprecated.
please use OS_TRU64 instead
|
static int |
OS_FREEBSD
Operating system is FreeBSD
|
static int |
OS_HP
Operating system is HP-UX.
|
static int |
OS_IRIX
Operating system is SGI IRIX.
|
static int |
OS_LINUX
Operating system is Linux.
|
static int |
OS_MAC
Operating system is Mac.
|
static int |
OS_OPENBSD
Operating system is OpenBSD.
|
static int |
OS_OS2
Operating system is OS/2.
|
static int |
OS_OTHER
Operating system is unknown.
|
static int |
OS_SOLARIS
Operating system is Solaris.
|
static int |
OS_SUNOS
Operating system is Sun OS.
|
static int |
OS_TRU64
Operating system is Compaq TRU64 Unix
|
static int |
OS_UNIX_MASK
Deprecated.
Use
isUnix() instead. |
static int |
OS_UNIX_OTHER
Operating system is one of the Unix variants but we don't know which
one it is.
|
static int |
OS_VMS
Operating system is Compaq OpenVMS
|
static int |
OS_WIN_OTHER
Operating system is one of the Windows variants but we don't know which
one it is
|
static int |
OS_WIN2000
Operating system is Windows 2000.
|
static int |
OS_WIN95
Operating system is Windows 95.
|
static int |
OS_WIN98
Operating system is Windows 98.
|
static int |
OS_WINDOWS_MASK
Deprecated.
Use
isWindows() instead. |
static int |
OS_WINNT
Operating system is Windows NT.
|
static int |
OS_WINVISTA
Operating system is Windows Vista.
|
Modifier and Type | Method and Description |
---|---|
static ReferenceQueue<Object> |
activeReferenceQueue()
Useful queue for all parts of system that use
java.lang.ref.Reference s
together with some ReferenceQueue and need to do some clean up
when the reference is enqueued. |
static boolean |
compareObjects(Object o1,
Object o2)
Safe equality check.
|
static boolean |
compareObjectsImpl(Object o1,
Object o2,
int checkArraysDepth)
Safe equality check with array recursion.
|
static String |
escapeParameters(String[] params)
Complementary method to parseParameters
|
static String |
getClassName(Class<?> clazz)
Assemble a human-presentable class name for a specified class.
|
static Class<?> |
getObjectType(Class<?> c)
Get the object type for given primitive type.
|
static int |
getOperatingSystem()
Get the operating system on which NetBeans is running.
|
static Class<?> |
getPrimitiveType(Class<?> c)
Get the primitive type for given object type.
|
static String |
getShortClassName(Class<?> clazz)
Assemble a human-presentable class name for a specified class (omitting the package).
|
static boolean |
isJavaIdentifier(String id)
Test whether a given string is a valid Java identifier.
|
static boolean |
isMac()
Test whether NetBeans is running on Mac OS X.
|
static boolean |
isUnix()
Test whether NetBeans is running on some variant of Unix.
|
static boolean |
isWindows()
Test whether NetBeans is running on some variant of Windows.
|
static String[] |
parseParameters(String s)
Parses parameters from a given string in shell-like manner.
|
static String |
pureClassName(String fullName)
Turn full name of an inner class into its pure form.
|
static File |
toFile(URI u)
Converts a URI to a file while being safe for UNC paths.
|
static Object[] |
toObjectArray(Object array)
Convert an array of primitive types to an array of objects.
|
static <T> List<T> |
topologicalSort(Collection<? extends T> c,
Map<? super T,? extends Collection<? extends T>> edges)
Topologically sort some objects.
|
static Object |
toPrimitiveArray(Object[] array)
Convert an array of objects to an array of primitive types.
|
static URI |
toURI(File f)
Converts a file to a URI while being safe for UNC paths.
|
static String |
translate(String className)
Provides support for parts of the system that deal with classnames
(use
Class.forName , NbObjectInputStream , etc.) or filenames
in layers. |
static String |
wrapString(String original,
int width,
BreakIterator breakIterator,
boolean removeNewLines)
Wrap multi-line strings.
|
static String[] |
wrapStringToArray(String original,
int width,
BreakIterator breakIterator,
boolean removeNewLines)
Wrap multi-line strings (and get the individual lines).
|
public static final int OS_WINNT
public static final int OS_WIN95
public static final int OS_WIN98
public static final int OS_SOLARIS
public static final int OS_LINUX
public static final int OS_HP
public static final int OS_AIX
public static final int OS_IRIX
public static final int OS_SUNOS
public static final int OS_TRU64
@Deprecated public static final int OS_DEC
public static final int OS_OS2
public static final int OS_MAC
public static final int OS_WIN2000
public static final int OS_VMS
public static final int OS_WIN_OTHER
public static final int OS_OTHER
public static final int OS_FREEBSD
public static final int OS_WINVISTA
public static final int OS_UNIX_OTHER
public static final int OS_OPENBSD
@Deprecated public static final int OS_WINDOWS_MASK
isWindows()
instead.@Deprecated public static final int OS_UNIX_MASK
isUnix()
instead.public static ReferenceQueue<Object> activeReferenceQueue()
java.lang.ref.Reference
s
together with some ReferenceQueue
and need to do some clean up
when the reference is enqueued. Usually, in order to be notified about that, one
needs to either create a dedicated thread that blocks on the queue and is
Object.notify
-ed, which is the right approach but consumes
valuable system resources (threads) or one can periodically check the content
of the queue by RequestProcessor.Task.schedule
which is
completely wrong, because it wakes up the system every (say) 15 seconds.
In order to provide useful support for this problem, this queue has been
provided.
If you have a reference that needs cleanup, make it implement Runnable and register it with the queue:
class MyReference extends WeakReferenceWhen theimplements Runnable { private final OtherInfo dataToCleanUp; public MyReference(Thing ref, OtherInfo data) { super(ref, Utilities.queue()); dataToCleanUp = data; } public void run() { dataToCleanUp.releaseOrWhateverYouNeed(); } }
ref
object is garbage collected, your run method
will be invoked by calling
((Runnable) reference).run()
and you can perform whatever cleanup is necessary. Be sure not to block
in such cleanup for a long time as this prevents other waiting references
from cleaning themselves up.
Do not call any ReferenceQueue
methods. They
will throw exceptions. You may only enqueue a reference.
Be sure to call this method anew for each reference. Do not attempt to cache the return value.
public static int getOperatingSystem()
OS_*
constants (such as OS_WINNT
)public static boolean isWindows()
true
if Windows, false
if some other manner of operating systempublic static boolean isMac()
true
if Mac, false
if some other manner of operating systempublic static boolean isUnix()
true
some sort of Unix, false
if some other manner of operating systempublic static boolean isJavaIdentifier(String id)
id
- string which should be checkedtrue
if a valid identifierSourceVersion.isIdentifier(java.lang.CharSequence)
,
SourceVersion.isKeyword(java.lang.CharSequence)
public static String[] wrapStringToArray(String original, int width, BreakIterator breakIterator, boolean removeNewLines)
original
- the original string to wrapwidth
- the maximum width of linesbreakIterator
- breaks original to chars, words, sentences, depending on what instance you provide.removeNewLines
- if true
, any newlines in the original string are ignoredpublic static String wrapString(String original, int width, BreakIterator breakIterator, boolean removeNewLines)
original
- the original string to wrapwidth
- the maximum width of linesbreakIterator
- algorithm for breaking linesremoveNewLines
- if true
, any newlines in the original string are ignoredpublic static String pureClassName(String fullName)
fullName
- e.g. some.pkg.SomeClass$Inner
Inner
public static boolean compareObjects(Object o1, Object o2)
null
Object.equals(java.lang.Object)
null
-safe, so if one of the parameters is true and the second not,
it returns false
.
Use java.util.Objects.deepEquals
in JDK 7.
o1
- the first object to compareo2
- the second object to comparetrue
if the objects are equalpublic static boolean compareObjectsImpl(Object o1, Object o2, int checkArraysDepth)
Use java.util.Objects.deepEquals
in JDK 7.
o1
- the first object to compareo2
- the second object to comparecheckArraysDepth
- the depth to which arrays should be compared for equality (negative for infinite depth, zero for no comparison of elements, one for shallow, etc.)true
if the objects are equalcompareObjects(Object, Object)
public static String getClassName(Class<?> clazz)
java.lang.String[]
.clazz
- the class to namepublic static String getShortClassName(Class<?> clazz)
String[]
.clazz
- the class to namepublic static Object toPrimitiveArray(Object[] array)
Integer[]
would be changed to an int[]
.array
- the wrapper arrayIllegalArgumentException
- if the array element type is not a primitive wrapperpublic static Object[] toObjectArray(Object array)
int[]
would be turned into an Integer[]
.array
- the primitive arrayIllegalArgumentException
- if the array element type is not primitivepublic static Class<?> getObjectType(Class<?> c)
c
- primitive type (e.g. int
)Integer
)public static Class<?> getPrimitiveType(Class<?> c)
c
- object type (e.g. Integer
)int
)public static String[] parseParameters(String s)
org.openide.execution.NbProcessDescriptor
(Execution API)
you should be able to:
c:\Program Files\jdk\bin\javac
.
-Dname=value
.
"c:\program files\jdk\bin\java" -Dmessage="Hello /\\/\\ there!" -Xmx128m
This example would create the following executable name and arguments:
c:\program files\jdk\bin\java
-Dmessage=Hello /\/\ there!
-Xmx128m
s
- a string to parsepublic static String escapeParameters(String[] params)
parseParameters(java.lang.String)
public static <T> List<T> topologicalSort(Collection<? extends T> c, Map<? super T,? extends Collection<? extends T>> edges) throws TopologicalSortException
There may not be any nulls among the objects, nor duplicates (as per hash/equals), nor duplicates among the edge lists. The edge map need not contain an entry for every object, only if it has some outgoing edges (empty but not null map values are permitted). The edge map shall not contain neither keys nor value entries for objects not in the collection to be sorted, if that happens they will be ignored (since version 7.9).
The incoming parameters will not be modified; they must not be changed during the call and possible calls to TopologicalSortException methods. The returned list will support modifications.
There is a weak stability guarantee: if there are no edges which contradict the incoming order, the resulting list will be in the same order as the incoming elements. However if some elements need to be rearranged, it is not guaranteed that others will not also be rearranged, even if they did not strictly speaking need to be.
c
- a collection of objects to be topologically sortededges
- constraints among those objects, of type Map<Object,Collection>
;
if an object is a key in this map, the resulting order will
have that object before any objects listed in the valueTopologicalSortException
- if the sort cannot succeed due to cycles in the graph, the
exception contains additional information to describe and possibly recover from the errorpublic static String translate(String className)
Class.forName
, NbObjectInputStream
, etc.) or filenames
in layers.
Often class names (especially package names) changes during lifecycle
of a module. When some piece of the system stores the name of a class
in certain point of a time and wants to find the correct Class
later it needs to count with the possibility of rename.
For such purposes this method has been created. It allows modules to register their classes that changed names and other parts of system that deal with class names to find the correct names.
To register a mapping from old class names to new ones create a file
META-INF/netbeans/translate.names
in your module and fill it
with your mapping:
# # Mapping of legacy classes to new ones # org.oldpackage.MyClass=org.newpackage.MyClass # rename of package for one class org.mypackage.OldClass=org.mypackage.NewClass # rename of class in a package # rename of class and package org.oldpackage.OldClass=org.newpackage.NewClass # rename of whole package org.someoldpackage=org.my.new.package.structure # class was removed without replacement org.mypackage.OldClass=Btw. one can use spaces instead of
=
sign.
For a real world example
check the
xml module.
For purposes of org.openide.util.io.NbObjectInputStream there is a following special convention: If the className is not listed as one that is to be renamed, the returned string == className, if the className is registered to be renamed than the className != returned value, even in a case when className.equals (retValue)
Similar behaviour applies to filenames provided by layers (system filesystem). Filenames can be also translated to adapt to location changes e.g. in action registrations. Note that no spaces or special characters are allowed in both translated filenames or translation results. Filenames must conform to regexp^[/a-zA-Z0-9$_.+-]+$
. Keys and values are treated
as paths from fs root.
Example of file path translation (action registration file has moved):
# registration ID has changed Actions/Refactoring/RefactoringWhereUsed.instance=Actions/Refactoring/org-netbeans-modules-refactoring-api-ui-WhereUsedAction.instance
className
- fully qualified name of a class, or file path to translatepublic static URI toURI(File f)
f
.toPath
().toUri
()
which results into URI
that works with URI.normalize()
and URI.resolve(URI)
.f
- a filefile
-protocol URI which may use the host fieldPath.toUri
public static File toFile(URI u) throws IllegalArgumentException
Paths.get
(u).toFile
()
which accepts UNC URIs with a host field.u
- a file
-protocol URI which may use the host fieldIllegalArgumentException
Paths.get(java.net.URI)
Built on June 4 2024. | Copyright © 2017-2024 Apache Software Foundation. All Rights Reserved.