Package com.levigo.jadice.server.util
Class Util
- java.lang.Object
-
- com.levigo.jadice.server.util.Util
-
public final class Util extends Object
Useful utility methods.
-
-
Field Summary
Fields Modifier and Type Field Description static Class<?>[]
PRIMITIVE_TYPES
Java's primitive types:Number
String
Boolean
Character
Date
java.sql.Date
Color
URL
URI
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <O> Iterable<O>
asIterable(Enumeration<O> enumeration)
static long
copyAndClose(InputStream is, OutputStream os)
Copies all data from input to output and closes the streams afterwards.static long
copyStream(InputStream is, OutputStream os)
Copies all data from input to output.static boolean
deleteAllBelow(File dir)
Recursively delete everything below a given directory, but don't remove the directory itself.static boolean
deleteRecursively(File file)
Recursively delete aFile
or directory.static boolean
deleteRecursively(Collection<File> files)
Recursively delete severalFile
s or directories.static long
discardStream(InputStream inputStream)
Discards all data from a givenInputStream
but does not close it.static String
fileToURL(File file)
Create a correct file URL that LibreOffice can handle.static <K,V>
Map<V,K>invertMap(Map<K,V> map)
static boolean
isPrimitivObject(Object obj, boolean considerCollections)
Checks if a given object is an instance of aPRIMITIVE_TYPES
or a Collection / Array / Map assembled of primitive types.static String
join(Collection<String> values, String glue)
Joins the given Strings as one single string, separated by the value glue.static <K,V>
voidput(Map<K,Set<V>> multimap, K key, V value)
Utility method for putting values into a multimap.static com.levigo.jadice.document.io.SeekableInputStream
wrapSeekable(InputStream is)
Wraps a given InputStream into a seekable input stream (i.e.
-
-
-
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
- apath
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 streamos
- 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 streamos
- 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 givenInputStream
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. aMemoryInputStream
) 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 aFile
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 severalFile
s 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 aMap
from<K>
→<V>
to<V>
→<K>
. If the given map is not symmetric or containsnull
as key or value the result will differ from the expected result- Type Parameters:
K
- former key typeV
- 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 theSet
where the value is stored. So, the same value cannot be stored multiple times under a given key.- Type Parameters:
K
- key typeV
- value typ- Parameters:
multimap
- where to store the valuekey
- 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 aPRIMITIVE_TYPES
or a Collection / Array / Map assembled of primitive types.Caveat: If
considerCollections
istrue
, empty collections will result withtrue
as the generic type information is lost while the compilation.- Parameters:
obj
- the object to checkconsiderCollections
- iftrue
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 toString.split(String)
.Example:
join({"a","b","c","d"}, ", ") ⇒ "a, b, c, d"
- Parameters:
values
- the String literals to joinglue
- a value that is glued between the single literals, might also be empty ornull
- 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 anEnumeration
as a read-onlyIterable
so that it can be used in enhancedfor
-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
-
-