Class Util


  • public class Util
    extends java.lang.Object
    Miscellaneous utility functions.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.BitSet EMPTY_BITSET
      The empty BitSet.
      static boolean FULLY_QUALIFIED_NAMES
      Generate strings with fully qualified names or not
    • Constructor Summary

      Constructors 
      Constructor Description
      Util()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> void addIfNotNull​(T val, java.util.Collection<T> vals)  
      static <T> void doForAll​(java.util.Collection<T> c, ObjectVisitor<T> v)
      Perform an action for all elements in a collection.
      static <S,​T>
      java.util.Set<T>
      filterByType​(java.lang.Iterable<S> c, java.lang.Class<T> klass)
      Return those elements of c that are assignable to klass.
      static <T> T find​(java.util.Collection<T> c, java.util.function.Predicate<T> p)
      Test whether some element of the given Collection satisfies the given Predicate.
      static <T> boolean forAll​(java.util.Collection<T> c, java.util.function.Predicate<T> p)
      Test whether all elements of the given Collection satisfy the given Predicate.
      static <T> boolean forSome​(java.util.Collection<T> c, java.util.function.Predicate<T> p)
      Test whether some element of the given Collection satisfies the given Predicate.
      static long getUsedMemory()  
      static <T> boolean intersecting​(java.util.Set<T> s1, java.util.Set<T> s2)
      checks if two sets have a non-empty intersection
      static <T,​U>
      java.util.List<U>
      map​(java.util.List<T> srcList, java.util.function.Function<T,​U> f)
      Map a list: generate a new list with each element mapped.
      static <T,​U>
      java.util.Set<U>
      mapToSet​(java.util.Collection<T> srcSet, java.util.function.Function<T,​U> f)
      Map a set: generate a new set with each element mapped.
      static java.lang.String objectFieldsToString​(java.lang.Object obj)
      Write object fields to string
      static int[] realloc​(int[] data, int newSize)  
      static java.lang.String removePackageName​(java.lang.String fully_qualified_name_)
      Remove the package name from a fully qualified class name
      static java.lang.String str​(java.lang.Throwable thrown)
      Get a String representation of a Throwable.
      static java.lang.String topLevelTypeString​(java.lang.String typeStr)
      given the name of a class C, returns the name of the top-most enclosing class of class C.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_BITSET

        public static final java.util.BitSet EMPTY_BITSET
        The empty BitSet.
      • FULLY_QUALIFIED_NAMES

        public static final boolean FULLY_QUALIFIED_NAMES
        Generate strings with fully qualified names or not
        See Also:
        Constant Field Values
    • Constructor Detail

      • Util

        public Util()
    • Method Detail

      • str

        public static java.lang.String str​(java.lang.Throwable thrown)
                                    throws java.lang.IllegalArgumentException
        Get a String representation of a Throwable.
        Throws:
        java.lang.IllegalArgumentException - if thrown == null
      • filterByType

        public static <S,​T> java.util.Set<T> filterByType​(java.lang.Iterable<S> c,
                                                                java.lang.Class<T> klass)
        Return those elements of c that are assignable to klass.
      • forSome

        public static <T> boolean forSome​(java.util.Collection<T> c,
                                          java.util.function.Predicate<T> p)
                                   throws java.lang.IllegalArgumentException
        Test whether some element of the given Collection satisfies the given Predicate.
        Throws:
        java.lang.IllegalArgumentException - if c == null
      • find

        public static <T> T find​(java.util.Collection<T> c,
                                 java.util.function.Predicate<T> p)
                          throws java.lang.IllegalArgumentException
        Test whether some element of the given Collection satisfies the given Predicate.
        Returns:
        The first element satisfying the predicate; otherwise null.
        Throws:
        java.lang.IllegalArgumentException - if c == null
      • forAll

        public static <T> boolean forAll​(java.util.Collection<T> c,
                                         java.util.function.Predicate<T> p)
                                  throws java.lang.NullPointerException
        Test whether all elements of the given Collection satisfy the given Predicate.
        Throws:
        java.lang.NullPointerException - if c == null
      • doForAll

        public static <T> void doForAll​(java.util.Collection<T> c,
                                        ObjectVisitor<T> v)
                                 throws java.lang.IllegalArgumentException
        Perform an action for all elements in a collection.
        Parameters:
        c - the collection
        v - the visitor defining the action
        Throws:
        java.lang.IllegalArgumentException - if c == null
      • map

        public static <T,​U> java.util.List<U> map​(java.util.List<T> srcList,
                                                        java.util.function.Function<T,​U> f)
                                                 throws java.lang.IllegalArgumentException
        Map a list: generate a new list with each element mapped. The new list is always an ArrayList; it would have been more precise to use reflection to create a list of the same type as 'srcList', but reflection works really slowly in some implementations, so it's best to avoid it.
        Throws:
        java.lang.IllegalArgumentException - if srcList == null
      • mapToSet

        public static <T,​U> java.util.Set<U> mapToSet​(java.util.Collection<T> srcSet,
                                                            java.util.function.Function<T,​U> f)
                                                     throws java.lang.IllegalArgumentException
        Map a set: generate a new set with each element mapped. The new set is always a HashSet; it would have been more precise to use reflection to create a set of the same type as 'srcSet', but reflection works really slowly in some implementations, so it's best to avoid it.
        Throws:
        java.lang.IllegalArgumentException - if srcSet == null
      • realloc

        public static int[] realloc​(int[] data,
                                    int newSize)
                             throws java.lang.IllegalArgumentException
        Throws:
        java.lang.IllegalArgumentException
      • objectFieldsToString

        public static java.lang.String objectFieldsToString​(java.lang.Object obj)
                                                     throws java.lang.IllegalArgumentException
        Write object fields to string
        Throws:
        java.lang.IllegalArgumentException - if obj == null
      • removePackageName

        public static java.lang.String removePackageName​(java.lang.String fully_qualified_name_)
        Remove the package name from a fully qualified class name
      • intersecting

        public static <T> boolean intersecting​(java.util.Set<T> s1,
                                               java.util.Set<T> s2)
        checks if two sets have a non-empty intersection
        Returns:
        true if the sets intersect; false otherwise
      • topLevelTypeString

        public static java.lang.String topLevelTypeString​(java.lang.String typeStr)
                                                   throws java.lang.IllegalArgumentException
        given the name of a class C, returns the name of the top-most enclosing class of class C. For example, given A$B$C, the method returns A
        Returns:
        String name of top-most enclosing class
        Throws:
        java.lang.IllegalArgumentException - if typeStr == null
      • addIfNotNull

        public static <T> void addIfNotNull​(T val,
                                            java.util.Collection<T> vals)
      • getUsedMemory

        public static long getUsedMemory()
        Returns:
        the amount of memory currently being used, in bytes. Often inaccurate, but there's no better thing to do from within the JVM.