Class Util


  • public final class Util
    extends Object
    Useful utility methods.
    • Field Detail

      • PRIMITIVE_TYPES

        public static final Class<?>[] PRIMITIVE_TYPES
        Java's primitive types:
        • Number
        • String
        • Boolean
        • Character
        • Date
        • java.sql.Date
        • Color
        • URL
        • URI
    • Method Detail

      • fileToURL

        public static String fileToURL​(File file)
                                throws IOException
        Create a correct file URL that LibreOffice can handle. This is necessary to be platform independent.
        Parameters:
        file - a path to convert
        Returns:
        a valid URL representation of the provided path
        Throws:
        IOException - if URL conversion fails
      • copyStream

        public static long copyStream​(InputStream is,
                                      OutputStream os)
                               throws IOException
        Copies all data from input to output. Does not close the streams afterwards.
        Parameters:
        is - the source stream
        os - the target stream
        Returns:
        number of bytes copied
        Throws:
        IOException - if an I/O error occurs
      • copyAndClose

        public static long copyAndClose​(InputStream is,
                                        OutputStream os)
                                 throws IOException
        Copies all data from input to output and closes the streams afterwards.
        Parameters:
        is - the source stream
        os - the target stream
        Returns:
        number of bytes copied
        Throws:
        IOException - if an I/O error occurs
      • discardStream

        public static long discardStream​(InputStream inputStream)
                                  throws IOException
        Discards all data from a given InputStream but does not close it. This method can used as a /dev/null device.
        Parameters:
        inputStream - The stream to discard
        Returns:
        number of bytes discarded
        Throws:
        IOException - if an I/O error occurs.
      • wrapSeekable

        public static com.levigo.jadice.document.io.SeekableInputStream wrapSeekable​(InputStream is)
                                                                              throws IOException
        Wraps a given InputStream into a seekable input stream (i.e. a MemoryInputStream) if it is not already seekable.
        Parameters:
        is - The stream to wrap
        Returns:
        the wrapped stream
        Throws:
        IOException - if an I/O error occurs
        Since:
        jadice server 5.4.2.0
      • deleteRecursively

        public static boolean deleteRecursively​(File file)
        Recursively delete a File or directory.
        Parameters:
        file - The file or directory to delete
        Returns:
        true iff the file or directory and all of its content is successfully deleted
        See Also:
        File.delete()
      • deleteRecursively

        public static boolean deleteRecursively​(Collection<File> files)
        Recursively delete several Files or directories.
        Parameters:
        files - The files or directories to delete
        Returns:
        true iff all files or directories and all of its contents are successfully deleted
        See Also:
        File.delete()
      • deleteAllBelow

        public static boolean deleteAllBelow​(File dir)
        Recursively delete everything below a given directory, but don't remove the directory itself.
        Parameters:
        dir - The directory to delete
        Returns:
        true iff all of the directory's content is successfully deleted
        See Also:
        File.delete()
      • invertMap

        public static <K,​V> Map<V,​K> invertMap​(Map<K,​V> map)
        Inverts a Map from <K><V> to <V><K>. If the given map is not symmetric or contains null as key or value the result will differ from the expected result
        Type Parameters:
        K - former key type
        V - former value type
        Parameters:
        map - the map to invert
        Returns:
        the inverted map
      • put

        public static <K,​V> void put​(Map<K,​Set<V>> multimap,
                                           K key,
                                           V value)
        Utility method for putting values into a multimap.

        Implementation note: If no entry is found so under the given key, a HashSet the will be created as the Set where the value is stored. So, the same value cannot be stored multiple times under a given key.

        Type Parameters:
        K - key type
        V - value typ
        Parameters:
        multimap - where to store the value
        key - key with which the specified value is to be associated.
        value - value to be associated with the specified key.
      • isPrimitivObject

        public static boolean isPrimitivObject​(Object obj,
                                               boolean considerCollections)
        Checks if a given object is an instance of a PRIMITIVE_TYPES or a Collection / Array / Map assembled of primitive types.

        Caveat: If considerCollections is true, empty collections will result with true as the generic type information is lost while the compilation.

        Parameters:
        obj - the object to check
        considerCollections - if true Collections / Arrays / Maps are checked recursively
        Returns:
        true iff the given object is a primitive object (or assembled of them)
      • join

        public static String join​(Collection<String> values,
                                  String glue)
        Joins the given Strings as one single string, separated by the value glue. This is the inverse operation to String.split(String).

        Example: join({"a","b","c","d"}, ", ") ⇒ "a, b, c, d"

        Parameters:
        values - the String literals to join
        glue - a value that is glued between the single literals, might also be empty or null
        Returns:
        the joined string
        Since:
        jadice server 4.3.1.0
        See Also:
        String.split(String)
      • asIterable

        public static <O> Iterable<O> asIterable​(Enumeration<O> enumeration)
        Wraps an Enumeration as a read-only Iterable so that it can be used in enhanced for-loops. This wrapping is not thread-safe.
        Type Parameters:
        O - The enumeration's objects type
        Parameters:
        enumeration - the (old-fashioned) Enumeration
        Returns:
        A wrapping (new-fashioned) Iterable
        Since:
        jadice server 4.3.1.4