Package org.apache.uima.internal.util
Class Misc
java.lang.Object
org.apache.uima.internal.util.Misc
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final String
static final int[]
static final boolean
static final int
For multi-core machine "tuning" - the number of coresstatic final MethodHandles.Lookup
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
addAll
(Collection<String> c, String... v) Takes trailing arguments of strings and adds them all the firststatic <T> StringBuilder
addElementsToStringBuilder
(int[] indent, StringBuilder sb, Collection<T> c, int limit, BiConsumer<StringBuilder, T> appender) static <T> StringBuilder
addElementsToStringBuilder
(int[] indent, List<T> c, int limit, BiConsumer<StringBuilder, T> appender) static StringBuilder
addElementsToStringBuilder
(StringBuilder sb, int size, int limit, int indent, int incr, BiConsumer<StringBuilder, Integer> appender) static <T> StringBuilder
For standardized prettyprinting, to string.static <T> StringBuilder
addElementsToStringBuilder
(StringBuilder sb, Collection<T> c, int limit) static <T> StringBuilder
addElementsToStringBuilder
(StringBuilder sb, Collection<T> c, int limit, BiConsumer<StringBuilder, T> appender) Does two styles of list formatting: Style 1: [ item1, item 2, item3] Style 2: [ item1, item2, item3 ] Starts as style 1, switches to style 2 when length > 60static <T> StringBuilder
addElementsToStringBuilder
(StringBuilder sb, Collection<T> c, BiConsumer<StringBuilder, T> appender) static <T> StringBuilder
addElementsToStringBuilder
(List<T> c, int limit) static <T> StringBuilder
addElementsToStringBuilder
(List<T> c, int limit, BiConsumer<StringBuilder, T> appender) static void
static void
static void
addUrlsFromPath
(String p, List<URL> urls) Given a String corresponding to one file path, which may be a directory, or may end in *, add the URLS it represents to the urls argument.static void
assertUie
(boolean v) Check and throw UIMA Internal Error if falsestatic void
static String
classNameFromByteCode
(byte[] bytes) extract the slashified version of the fully qualified class name from the bytecode for a classstatic URL[]
classpath2urls
(String classpath) Convert a classpath having multiple parts separated by the pathSeparator, expanding paths that end with "*" as needed.static int
compareStrings
(String s1, String s2) static boolean
contains
(ClassLoader[] cls, ClassLoader cl) static boolean
static void
static void
decreasingMessage
(AtomicInteger errorCount, Supplier<String> messageSupplier, Consumer<String> publishMessage) static void
decreasingWithTrace
(AtomicInteger errorCount, String message, Logger logger) Issues message at warning or fine level (fine if enabled, includes stack trace)static String
dumpByteArray
(byte[] b, int limit) static StringBuilder
dumpCallers
(StackTraceElement[] e, int s, int n) static String
static String
static String
elideString
(String s, int len) static boolean
equalStrings
(String s1, String s2) static String
expandClasspath
(String classpath) static String
formatcaller
(String className, String methodName, int lineNumber) static String
static StringBuilder
getCallers
(int s, int n) static ClassLoader[]
static boolean
static int
getPrivateStaticIntFieldNoInherit
(Class<?> clazz, String fieldName) static MethodHandle
getProtectedFieldGetter
(Class<?> clazz, String protectedField) static MethodHandle
getProtectedMethodHandle
(Class<?> clazz, MethodHandles.Lookup methodHandleAccessContext, String protectedMethod, Class<?>... args) Given a class, a lookup context, and a protected method and its arg classes, return the method handle for that method.static MethodHandle
getProtectedMethodHandle
(Class<?> clazz, String protectedMethod, Class<?>... args) Given a class, and a protected method and its arg classes, return the method handle for that method.static int
getStaticIntField
(Class<?> clazz, String fieldName) Gets an int from a named field.static int
getStaticIntFieldNoInherit
(Class<?> clazz, String fieldName) static URL[]
static <T> T
getWithExpand
(List<T> a, int i) Get item from array list.static int
hashInt
(int k1) static long
a hash for strings as a long - less likely to be a collisionstatic byte[]
static StringBuilder
indent
(StringBuilder sb, int indent) static StringBuilder
indent
(StringBuilder sb, int[] indent) static RuntimeException
static void
static boolean
static String
javaClassName2UimaTypeName
(String className) Convert a JCas class name (fully qualified) to a UIMA type name Normally this is the same, but for two prefixes, it's slightly different Also, class names for primitives (int, byte, etc. ) converted to int, byte, etc.static boolean
maybeShrink
(boolean secondTimeShrinkable, int size, int capacity, int factor, int minCapacity, IntConsumer realloc, Runnable reset) static int
nextHigherPowerOf2
(int i) Convert an int argument to the next higher power of 2 if not already a power of 2static int
nextHigherPowerOfX
(int i, int x) Convert an int argument to the next higher power of 2 to the x powerstatic String
static <T> String
format a list of items for pretty printing as [item1, item2, ... ]static <T> String
static <T> String
ppList
(int[] indent, List<T> items, int max, BiConsumer<StringBuilder, T> appender) static <T> String
format a list of items for pretty printing as [item1, item2, ... ]static <T> String
static <T> String
ppList
(List<T> items, int max, BiConsumer<StringBuilder, T> appender) static String
replaceWhiteSpace
(String s, String replacement) static <T> List
<T> static <T> void
setWithExpand
(List<T> a, int i, T value) static <T> T
shareExisting
(T obj, WeakHashMap<T, WeakReference<T>> cache) Some objects can be shared, if "equal", rather than creating duplicates, if they're read-only.static void
sleep
(int milliseconds) static void
timeLoops
(String title, int iterations, Runnable_withException r) static void
toFile
(ByteArrayOutputStream baos, File file) Writes a byte array output stream to a filestatic void
toFile
(ByteArrayOutputStream baos, String name) Writes a byte array output stream to a filestatic String
typeName2ClassName
(String typeName) Convert a UIMA type name to a JCas class name (fully qualified) Normally this is the same, but for two prefixes, it's slightly different
-
Field Details
-
isJava9ea
public static final boolean isJava9ea -
blanks
-
dots
- See Also:
-
INT0
public static final int[] INT0 -
UIMAlookup
-
numberOfCores
public static final int numberOfCoresFor multi-core machine "tuning" - the number of cores
-
-
Constructor Details
-
Misc
public Misc()
-
-
Method Details
-
replaceWhiteSpace
-
null2str
-
hex_string_to_bytearray
-
dumpByteArray
-
getCallers
- Parameters:
s
- starting frames above invokern
- max number of callers to return- Returns:
- x called by: y ...
-
dumpCallers
-
getCaller
- Returns:
- the name of the caller in the stack and their caller
-
formatcaller
-
getCallingClass_classLoaders
-
elide
- Parameters:
s
- the string to possibly eliden
- the length, after which, elision happens- Returns:
- the elided string, padded on the left to length n
-
elide
- Parameters:
s
- the string to possibly eliden
- the length, after which, elision happenspad
- true to include left padding to length n- Returns:
- the elided string, padded on the left to length n
-
indent
- Parameters:
sb
- the stringBuilder to indentindent
- the indent amount (passed as array of 1 item, to allow it to be final for lambdas- Returns:
- the stringBuilder, with nl if needed, and indention
-
indent
- Parameters:
sb
- the stringBuilder to indentindent
- the indent amount- Returns:
- the stringBuilder, with nl if needed, and indention
-
addNlIfMissing
-
addNlIfMissing
-
getURLs
-
addUrlsFromPath
public static void addUrlsFromPath(String p, List<URL> urls) throws MalformedURLException, IOException, URISyntaxException Given a String corresponding to one file path, which may be a directory, or may end in *, add the URLS it represents to the urls argument.- Parameters:
p
- a Jar path, or a Directory, or a directory ending with a directory-separator and a single * p may be relative or absolute, following the definition of same in the Java File class.urls
- the list to add the URLs to- Throws:
MalformedURLException
- -IOException
- -URISyntaxException
- -
-
classpath2urls
Convert a classpath having multiple parts separated by the pathSeparator, expanding paths that end with "*" as needed.- Parameters:
classpath
- - to scan and convert to list of URLs- Returns:
- the urls
-
expandClasspath
-
getNoValueSystemProperty
- Parameters:
name
- of property- Returns:
- true if property is defined, or is defined and set to anything except "false"; false if property is not defined, or is defined and set to "false".
-
addElementsToStringBuilder
For standardized prettyprinting, to string. Adds a collection of things (toString) separated by , and surrounded by [ ], to a StringBuilder- Type Parameters:
T
- the kind of elements in the collection- Parameters:
sb
- where the formatted collection results are appended toc
- the collection- Returns:
- the StringBuilder for chaining
-
addElementsToStringBuilder
public static <T> StringBuilder addElementsToStringBuilder(StringBuilder sb, Collection<T> c, int limit) -
addElementsToStringBuilder
public static <T> StringBuilder addElementsToStringBuilder(StringBuilder sb, Collection<T> c, BiConsumer<StringBuilder, T> appender) -
addElementsToStringBuilder
-
addElementsToStringBuilder
public static <T> StringBuilder addElementsToStringBuilder(List<T> c, int limit, BiConsumer<StringBuilder, T> appender) -
addElementsToStringBuilder
public static <T> StringBuilder addElementsToStringBuilder(int[] indent, List<T> c, int limit, BiConsumer<StringBuilder, T> appender) -
addElementsToStringBuilder
public static <T> StringBuilder addElementsToStringBuilder(StringBuilder sb, Collection<T> c, int limit, BiConsumer<StringBuilder, T> appender) Does two styles of list formatting: Style 1: [ item1, item 2, item3] Style 2: [ item1, item2, item3 ] Starts as style 1, switches to style 2 when length > 60- Type Parameters:
T
- the type of the collection- Parameters:
sb
- where the string is assembledc
- the collection to processlimit
- the maximum number of items, if negative, no limitappender
- the appender- Returns:
- argument sb, appeneded
-
addElementsToStringBuilder
public static <T> StringBuilder addElementsToStringBuilder(int[] indent, StringBuilder sb, Collection<T> c, int limit, BiConsumer<StringBuilder, T> appender) -
addElementsToStringBuilder
public static StringBuilder addElementsToStringBuilder(StringBuilder sb, int size, int limit, int indent, int incr, BiConsumer<StringBuilder, Integer> appender) -
toFile
Writes a byte array output stream to a file- Parameters:
baos
- the array to writename
- the name of the file
-
toFile
Writes a byte array output stream to a file- Parameters:
baos
- the array to writefile
- the file
-
nextHigherPowerOf2
public static int nextHigherPowerOf2(int i) Convert an int argument to the next higher power of 2 if not already a power of 2- Parameters:
i
- the value to convert- Returns:
- the next higher power of 2, or i if it is already a power of 2
-
nextHigherPowerOfX
public static int nextHigherPowerOfX(int i, int x) Convert an int argument to the next higher power of 2 to the x power- Parameters:
i
- the value to convertx
- the power of 2 to use- Returns:
- the next higher power of 2 to the x, or i if it is already == to 2 to the x
-
getProtectedMethodHandle
public static MethodHandle getProtectedMethodHandle(Class<?> clazz, MethodHandles.Lookup methodHandleAccessContext, String protectedMethod, Class<?>... args) Given a class, a lookup context, and a protected method and its arg classes, return the method handle for that method. Using that method handle is slow, but converting it to a lambda makes for JIT-able fast access.- Parameters:
clazz
- -methodHandleAccessContext
- -protectedMethod
- -args
- -- Returns:
- -
-
getProtectedMethodHandle
public static MethodHandle getProtectedMethodHandle(Class<?> clazz, String protectedMethod, Class<?>... args) Given a class, and a protected method and its arg classes, return the method handle for that method. Note: uses the UIMA context as the lookup context Using that method handle is slow, but converting it to a lambda makes for JIT-able fast access.- Parameters:
clazz
- -protectedMethod
- -args
- -- Returns:
- -
-
getProtectedFieldGetter
-
getStaticIntField
Gets an int from a named field. If the field isn't present, returns Integer.MIN_VALUE;- Parameters:
clazz
- the class where the field isfieldName
- the name of the field- Returns:
- the value or Integer.MIN_VALUE if not present
-
getStaticIntFieldNoInherit
-
getPrivateStaticIntFieldNoInherit
-
addAll
Takes trailing arguments of strings and adds them all the first- Parameters:
c
- the collection to add the strings tov
- 0 or more strings as arguments
-
debug
-
assertUie
public static void assertUie(boolean v) Check and throw UIMA Internal Error if false- Parameters:
v
- if false, throws
-
assertUie
-
internalError
-
internalError
-
hashInt
public static int hashInt(int k1) -
hashStringLong
a hash for strings as a long - less likely to be a collision- Parameters:
s
- - the string- Returns:
- a long hash
-
getWithExpand
Get item from array list. If index is > length, expand the array, and return null- Type Parameters:
T
- the type of the items in the list- Parameters:
a
- the listi
- the index- Returns:
- the item at the index or null
-
setWithExpand
-
equalStrings
-
compareStrings
-
elideString
-
ppList
format a list of items for pretty printing as [item1, item2, ... ]- Type Parameters:
T
- the type of elements in the list- Parameters:
items
- to print- Returns:
- [item1, item2, ... ]
-
ppList
- Type Parameters:
T
- the type of elements in the list- Parameters:
items
- to printmax
- - maximum number of items to print- Returns:
- [item1, item2, ... ]
-
ppList
- Type Parameters:
T
- the type of elements in the list- Parameters:
items
- to printmax
- - maximum number of items to printappender
- - appender function- Returns:
- [item1, item2, ... ]
-
ppList
format a list of items for pretty printing as [item1, item2, ... ]- Type Parameters:
T
- the type of elements in the list- Parameters:
indent
- the amount to use as indentationitems
- to print- Returns:
- [item1, item2, ... ]
-
ppList
- Type Parameters:
T
- the type of elements in the list- Parameters:
indent
- the amount to use as indentationitems
- to printmax
- - maximum number of items to print- Returns:
- [item1, item2, ... ]
-
ppList
public static <T> String ppList(int[] indent, List<T> items, int max, BiConsumer<StringBuilder, T> appender) - Type Parameters:
T
- the type of elements in the list- Parameters:
indent
- the amount to use as indentationitems
- to printmax
- - maximum number of items to printappender
- - appender function- Returns:
- [item1, item2, ... ]
-
typeName2ClassName
Convert a UIMA type name to a JCas class name (fully qualified) Normally this is the same, but for two prefixes, it's slightly different- Parameters:
typeName
- the UIMA type name, fully qualified- Returns:
- the fully qualified JCas class name
-
javaClassName2UimaTypeName
Convert a JCas class name (fully qualified) to a UIMA type name Normally this is the same, but for two prefixes, it's slightly different Also, class names for primitives (int, byte, etc. ) converted to int, byte, etc.- Parameters:
className
- the Java JCas class name for a UIMA type, fully qualified- Returns:
- the fully qualified UIMA Type name
-
timeLoops
public static void timeLoops(String title, int iterations, Runnable_withException r) throws Exception - Throws:
Exception
-
sleep
public static void sleep(int milliseconds) -
maybeShrink
public static boolean maybeShrink(boolean secondTimeShrinkable, int size, int capacity, int factor, int minCapacity, IntConsumer realloc, Runnable reset) -
isJava9ea
public static boolean isJava9ea() -
classNameFromByteCode
extract the slashified version of the fully qualified class name from the bytecode for a class- Parameters:
bytes
- the bytecode to extract from- Returns:
- the slashified class name eg. x/y/z/Myclass
-
setAsList
-
contains
-
contains
-
decreasingWithTrace
Issues message at warning or fine level (fine if enabled, includes stack trace)- Parameters:
errorCount
- the count of errors used to decrease the frequencymessage
- the messagelogger
- the logger to use
-
decreasingMessage
public static void decreasingMessage(AtomicInteger errorCount, Supplier<String> messageSupplier, Consumer<String> publishMessage)
-