Class Misc

java.lang.Object
org.apache.uima.internal.util.Misc

public class Misc extends Object
  • Field Details

    • isJava9ea

      public static final boolean isJava9ea
    • blanks

      public static final String blanks
    • dots

      public static final String dots
      See Also:
    • INT0

      public static final int[] INT0
    • UIMAlookup

      public static final MethodHandles.Lookup UIMAlookup
    • numberOfCores

      public static final int numberOfCores
      For multi-core machine "tuning" - the number of cores
  • Constructor Details

    • Misc

      public Misc()
  • Method Details

    • replaceWhiteSpace

      public static String replaceWhiteSpace(String s, String replacement)
    • null2str

      public static String null2str(String s)
    • hex_string_to_bytearray

      public static byte[] hex_string_to_bytearray(String s)
    • dumpByteArray

      public static String dumpByteArray(byte[] b, int limit)
    • getCallers

      public static StringBuilder getCallers(int s, int n)
      Parameters:
      s - starting frames above invoker
      n - max number of callers to return
      Returns:
      x called by: y ...
    • dumpCallers

      public static StringBuilder dumpCallers(StackTraceElement[] e, int s, int n)
    • getCaller

      public static String getCaller()
      Returns:
      the name of the caller in the stack and their caller
    • formatcaller

      public static String formatcaller(String className, String methodName, int lineNumber)
    • getCallingClass_classLoaders

      public static ClassLoader[] getCallingClass_classLoaders()
    • elide

      public static String elide(String s, int n)
      Parameters:
      s - the string to possibly elide
      n - the length, after which, elision happens
      Returns:
      the elided string, padded on the left to length n
    • elide

      public static String elide(String s, int n, boolean pad)
      Parameters:
      s - the string to possibly elide
      n - the length, after which, elision happens
      pad - true to include left padding to length n
      Returns:
      the elided string, padded on the left to length n
    • indent

      public static StringBuilder indent(StringBuilder sb, int[] indent)
      Parameters:
      sb - the stringBuilder to indent
      indent - 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

      public static StringBuilder indent(StringBuilder sb, int indent)
      Parameters:
      sb - the stringBuilder to indent
      indent - the indent amount
      Returns:
      the stringBuilder, with nl if needed, and indention
    • addNlIfMissing

      public static void addNlIfMissing(StringBuilder sb)
    • addNlIfMissing

      public static void addNlIfMissing(StringBuffer sb)
    • getURLs

      public static URL[] getURLs(String s) throws MalformedURLException, IOException, URISyntaxException
      Throws:
      MalformedURLException
      IOException
      URISyntaxException
    • 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

      public static URL[] classpath2urls(String classpath)
      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

      public static String expandClasspath(String classpath)
    • getNoValueSystemProperty

      public static boolean getNoValueSystemProperty(String name)
      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

      public static <T> StringBuilder addElementsToStringBuilder(StringBuilder sb, Collection<T> c)
      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 to
      c - 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

      public static <T> StringBuilder addElementsToStringBuilder(List<T> c, int limit)
    • 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 assembled
      c - the collection to process
      limit - the maximum number of items, if negative, no limit
      appender - 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

      public static void toFile(ByteArrayOutputStream baos, String name)
      Writes a byte array output stream to a file
      Parameters:
      baos - the array to write
      name - the name of the file
    • toFile

      public static void toFile(ByteArrayOutputStream baos, File file)
      Writes a byte array output stream to a file
      Parameters:
      baos - the array to write
      file - 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 convert
      x - 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

      public static MethodHandle getProtectedFieldGetter(Class<?> clazz, String protectedField)
    • getStaticIntField

      public static int getStaticIntField(Class<?> clazz, String fieldName)
      Gets an int from a named field. If the field isn't present, returns Integer.MIN_VALUE;
      Parameters:
      clazz - the class where the field is
      fieldName - the name of the field
      Returns:
      the value or Integer.MIN_VALUE if not present
    • getStaticIntFieldNoInherit

      public static int getStaticIntFieldNoInherit(Class<?> clazz, String fieldName)
    • getPrivateStaticIntFieldNoInherit

      public static int getPrivateStaticIntFieldNoInherit(Class<?> clazz, String fieldName)
    • addAll

      public static void addAll(Collection<String> c, String... v)
      Takes trailing arguments of strings and adds them all the first
      Parameters:
      c - the collection to add the strings to
      v - 0 or more strings as arguments
    • debug

      public static void debug(Object o)
    • assertUie

      public static void assertUie(boolean v)
      Check and throw UIMA Internal Error if false
      Parameters:
      v - if false, throws
    • assertUie

      public static void assertUie(boolean v, Throwable e)
    • internalError

      public static RuntimeException internalError()
    • internalError

      public static void internalError(Throwable e)
    • hashInt

      public static int hashInt(int k1)
    • hashStringLong

      public static long hashStringLong(String s)
      a hash for strings as a long - less likely to be a collision
      Parameters:
      s - - the string
      Returns:
      a long hash
    • getWithExpand

      public static <T> T getWithExpand(List<T> a, int i)
      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 list
      i - the index
      Returns:
      the item at the index or null
    • setWithExpand

      public static <T> void setWithExpand(List<T> a, int i, T value)
    • equalStrings

      public static boolean equalStrings(String s1, String s2)
    • compareStrings

      public static int compareStrings(String s1, String s2)
    • elideString

      public static String elideString(String s, int len)
    • shareExisting

      public 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. This may in general be beneficial by reducing the size of the "working set" via more sharing of read-only objects. Users should insure the read-only property. This routine allows a) creating a potentially sharable object b) checking to see if we already have an "equal" one, and c) if so, using that and allowing the just created one to be GC'd.
      Type Parameters:
      T - the type of the cached object
      Parameters:
      obj - - the object to use a cached substitute for, if one exists
      cache - - the cache
      Returns:
      - the object or a cached version of it.
    • ppList

      public static <T> String ppList(List<T> items)
      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

      public static <T> String ppList(List<T> items, int max)
      Type Parameters:
      T - the type of elements in the list
      Parameters:
      items - to print
      max - - maximum number of items to print
      Returns:
      [item1, item2, ... ]
    • ppList

      public static <T> String ppList(List<T> items, int max, BiConsumer<StringBuilder,T> appender)
      Type Parameters:
      T - the type of elements in the list
      Parameters:
      items - to print
      max - - maximum number of items to print
      appender - - appender function
      Returns:
      [item1, item2, ... ]
    • ppList

      public static <T> String ppList(int[] indent, List<T> items)
      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 indentation
      items - to print
      Returns:
      [item1, item2, ... ]
    • ppList

      public static <T> String ppList(int[] indent, List<T> items, int max)
      Type Parameters:
      T - the type of elements in the list
      Parameters:
      indent - the amount to use as indentation
      items - to print
      max - - 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 indentation
      items - to print
      max - - maximum number of items to print
      appender - - appender function
      Returns:
      [item1, item2, ... ]
    • typeName2ClassName

      public static 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
      Parameters:
      typeName - the UIMA type name, fully qualified
      Returns:
      the fully qualified JCas class name
    • javaClassName2UimaTypeName

      public 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.
      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

      public static String classNameFromByteCode(byte[] bytes)
      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

      public static <T> List<T> setAsList(Set<T> set)
    • contains

      public static boolean contains(String[] strings, String item)
    • contains

      public static boolean contains(ClassLoader[] cls, ClassLoader cl)
    • decreasingWithTrace

      public static void decreasingWithTrace(AtomicInteger errorCount, String message, Logger logger)
      Issues message at warning or fine level (fine if enabled, includes stack trace)
      Parameters:
      errorCount - the count of errors used to decrease the frequency
      message - the message
      logger - the logger to use
    • decreasingMessage

      public static void decreasingMessage(AtomicInteger errorCount, Supplier<String> messageSupplier, Consumer<String> publishMessage)