Class GeneralUtilities

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean attemptClose​(java.io.Closeable closeable)
      Attempts to close a closeable, ignoring any failures (useful e.g.
      static boolean attemptCloseWhenFailingAnyway​(java.io.Closeable closeable)
      Attempts to close a closeable, ignoring any failures (useful e.g.
      static void println​(java.lang.String format, java.lang.Object... arguments)
      Prints a line to standard out, with a leading message displaying the time and a trailing message indicating the location in the source code of the calling method.
      static void printWithCLIFormatting​(java.lang.String message)
      Prints a message to stnadard out using the standard CLI format (eg wrapped at ~120 characters)
      static void reorder​(boolean[] list, int[] ordering)
      adjusts the ordering of elements in the array according to the ordering array.
      static void reorder​(byte[] list, int[] ordering)
      adjusts the ordering of elements in the array according to the ordering array.
      static void reorder​(char[] list, int[] ordering)
      adjusts the ordering of elements in the array according to the ordering array.
      static void reorder​(int[] list, int[] ordering)
      adjusts the ordering of elements in the array according to the ordering array.
      static void reorder​(java.util.BitSet bitSet, int[] ordering)
      adjusts the ordering of elements in the BitSet according to the ordering array.
      static <T> void reorder​(java.util.List<T> list, int[] ordering)
      adjusts the ordering of elements in the array according to the ordering array.
      static void rethrow​(java.lang.Throwable throwable)
      rethrows this throwable (if it isn't null), wrapping it in a RuntimeException if necessary
      static <T> boolean safeEquals​(T a, T b)
      Safely checks whether two Objects are either equal (a.equals(b)) or both null, without ever throwing a NullPointerException.
      static void sort​(java.lang.Object[] arrayToSort)
      Just like Arrays.sort(Object[]) but much faster in cases where list.size()<=2.
      static <T extends java.lang.Comparable<? super T>>
      void
      sort​(java.util.List<T> list)
      Just like Collections.sort(java.util.List) but much faster in cases where list.size()<=2.
      static <T> void sort​(java.util.List<T> list, java.util.Comparator<? super T> comparator)
      Just like Collections.sort(java.util.List, java.util.Comparator) but much faster in cases where list.size()<=2.
      static <T> void sort​(T[] arrayToSort, java.util.Comparator<? super T> comparator)
      Just like Arrays.sort(Object[], java.util.Comparator) but much faster in cases where list.size()<=5.
      • Methods inherited from class java.lang.Object

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

      • safeEquals

        public static <T> boolean safeEquals​(T a,
                                             T b)
        Safely checks whether two Objects are either equal (a.equals(b)) or both null, without ever throwing a NullPointerException. This method has a type argument so that you can optionally ensure at compile time that the compared objects are of the same type T, e.g. Utils.safeEquals("foo", "bar"); (IntelliJ will incorrectly warn you that the argument is unnecessary; if you just wrote Utils.safeEquals(23, "foo") then this would not be a compile time error, but Utils.safeEquals(23, "foo") is). You can suppress IntelliJ's warning by using the annotation @SuppressWarnings({"RedundantTypeArguments"}).
        Parameters:
        a - an Object whose equality to b is to be assessed, or null.
        b - an Object to which a's equality is to be assessed, or null.
        Returns:
        true if either both a and b are null, or a != null and a.equals(b).
      • attemptClose

        public static boolean attemptClose​(java.io.Closeable closeable)
        Attempts to close a closeable, ignoring any failures (useful e.g. for closing streams inside a finally block, where there's nothing else we can do if the close fails). Does nothing if closeable is null or already closed.

        This method is not necessary since the introduction of the try-with-resources statement in Java 7. It is preferable to use that syntax instead.

        WARNING: Do not attempt to close output streams with this method and ignore the return value. Data may not have been flushed meaning the file isn't complete.

        Parameters:
        closeable - The closeable to close, or null to do nothing
        Returns:
        True if no IOException occurred. Also returns true if closeable is null. Returns false if an IOException occurred while closing
      • attemptCloseWhenFailingAnyway

        public static boolean attemptCloseWhenFailingAnyway​(java.io.Closeable closeable)
        Attempts to close a closeable, ignoring any failures (useful e.g. for closing streams inside a finally block, where there's nothing else we can do if the close fails). Does nothing if closeable is null or already closed.

        WARNING: Use this only to close output streams when an exception is already being thrown.
        Other usage on an output stream could hide errors caused by the stream not being flushed.

        Use attemptClose(Closeable) instead if the stream is not an output stream

        Parameters:
        closeable - The closeable to close, or null to do nothing
        Returns:
        True if no IOException occurred. Also returns true if closeable is null. Returns false if an IOException occurred while closing
        Since:
        API 4.1000 (Geneious 10.0.0)
      • sort

        public static <T extends java.lang.Comparable<? super T>> void sort​(java.util.List<T> list)
        Just like Collections.sort(java.util.List) but much faster in cases where list.size()<=2. This is because the Collections.sort implementation (as of Java 6 update 10) unnecessarily converts to an array (even on length 0 lists) before sorting.
        Parameters:
        list - the list to sort.
      • sort

        public static <T> void sort​(java.util.List<T> list,
                                    java.util.Comparator<? super T> comparator)
        Just like Collections.sort(java.util.List, java.util.Comparator) but much faster in cases where list.size()<=2. This is because the Collections.sort implementation (as of Java 6 update 10) unnecessarily converts to an array (even on length 0 lists) before sorting.
        Parameters:
        list - the list to sort.
        comparator - the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
      • sort

        public static void sort​(java.lang.Object[] arrayToSort)
        Just like Arrays.sort(Object[]) but much faster in cases where list.size()<=2. This is because the Arrays.sort implementation clones the array to be sorted even on empty arrays!
        Parameters:
        arrayToSort - the array to sort
      • sort

        public static <T> void sort​(T[] arrayToSort,
                                    java.util.Comparator<? super T> comparator)
        Just like Arrays.sort(Object[], java.util.Comparator) but much faster in cases where list.size()<=5. This is because the Arrays.sort implementation clones the array to be sorted even on empty arrays!
        Parameters:
        arrayToSort - the array to sort
        comparator - the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used.
      • reorder

        public static <T> void reorder​(java.util.List<T> list,
                                       int[] ordering)
        adjusts the ordering of elements in the array according to the ordering array. If ordering[i]==j, then the element in the returned array at index 'i' should be the element previously at index 'j' For example, if ordering = {0,1,3,4,2} Then: element at index 2 is moved to index 4 element at index 3 is moved to index 2 element at index 4 is moved to index 3
        Parameters:
        list - the list to reorder. May be null in which case this method does nothing.
        ordering - the reodering array as specified above. If this is less than the length of the list, then only the first ordering.length elements of list are reordered.
        Since:
        API 4.11 (Geneious 5.0)
      • reorder

        public static void reorder​(int[] list,
                                   int[] ordering)
        adjusts the ordering of elements in the array according to the ordering array. If ordering[i]==j, then the element in the returned array at index 'i' should be the element previously at index 'j' For example, if ordering = {0,1,3,4,2} Then: element at index 2 is moved to index 4 element at index 3 is moved to index 2 element at index 4 is moved to index 3
        Parameters:
        list - the list to reorder. May be null in which case this method does nothing.
        ordering - the reodering array as specified above. If this is less than the length of the list, then only the first ordering.length elements of list are reordered.
        Since:
        API 4.11 (Geneious 5.0)
      • reorder

        public static void reorder​(char[] list,
                                   int[] ordering)
        adjusts the ordering of elements in the array according to the ordering array. If ordering[i]==j, then the element in the returned array at index 'i' should be the element previously at index 'j' For example, if ordering = {0,1,3,4,2} Then: element at index 2 is moved to index 4 element at index 3 is moved to index 2 element at index 4 is moved to index 3
        Parameters:
        list - the list to reorder. May be null in which case this method does nothing.
        ordering - the reodering array as specified above. If this is less than the length of the list, then only the first ordering.length elements of list are reordered.
        Since:
        API 4.11 (Geneious 5.0)
      • reorder

        public static void reorder​(boolean[] list,
                                   int[] ordering)
        adjusts the ordering of elements in the array according to the ordering array. If ordering[i]==j, then the element in the returned array at index 'i' should be the element previously at index 'j' For example, if ordering = {0,1,3,4,2} Then: element at index 2 is moved to index 4 element at index 3 is moved to index 2 element at index 4 is moved to index 3
        Parameters:
        list - the list to reorder. May be null in which case this method does nothing.
        ordering - the reodering array as specified above. If this is less than the length of the list, then only the first ordering.length elements of list are reordered.
      • reorder

        public static void reorder​(java.util.BitSet bitSet,
                                   int[] ordering)
        adjusts the ordering of elements in the BitSet according to the ordering array. If ordering[i]==j, then the element in the returned array at index 'i' should be the element previously at index 'j' For example, if ordering = {0,1,3,4,2} Then: element at index 2 is moved to index 4 element at index 3 is moved to index 2 element at index 4 is moved to index 3
        Parameters:
        bitSet - the BitSet to reorder. May be null in which case this method does nothing.
        ordering - the reordering array as specified above. If this is less than the length of the BitSet, then only the first ordering.length elements of BitSet are reordered.
        Since:
        API 4.800 (Geneious 8.0.0)
      • reorder

        public static void reorder​(byte[] list,
                                   int[] ordering)
        adjusts the ordering of elements in the array according to the ordering array. If ordering[i]==j, then the element in the returned array at index 'i' should be the element previously at index 'j' For example, if ordering = {0,1,3,4,2} Then: element at index 2 is moved to index 4 element at index 3 is moved to index 2 element at index 4 is moved to index 3
        Parameters:
        list - the list to reorder. May be null in which case this method does nothing.
        ordering - the reodering array as specified above. If this is less than the length of the list, then only the first ordering.length elements of list are reordered.
      • printWithCLIFormatting

        public static void printWithCLIFormatting​(java.lang.String message)
        Prints a message to stnadard out using the standard CLI format (eg wrapped at ~120 characters)
        Parameters:
        message - The message to print with the same formatting as the CLI uses.
        Since:
        API 4.202220 (Geneious 2022.2.0)
      • println

        public static void println​(java.lang.String format,
                                   java.lang.Object... arguments)
        Prints a line to standard out, with a leading message displaying the time and a trailing message indicating the location in the source code of the calling method. The output line is formatted such that if viewed using the IntelliJ IDE, it is a clickable link that takes you to the line in the source code.
        Parameters:
        format - a format string as specified by Formatter
        arguments - optional arguments to apply to the format string.
      • rethrow

        public static void rethrow​(java.lang.Throwable throwable)
        rethrows this throwable (if it isn't null), wrapping it in a RuntimeException if necessary
        Parameters:
        throwable - the throwable to rethrow. If this is null, this method does nothing
        Since:
        API 4.900 (Geneious 9.0.0)