public final class Utils extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
Utils.Function<A,B>
Represents a function that can be applied to objects of
A and
returns objects of B . |
Modifier and Type | Field and Description |
---|---|
private static double |
EPSILON |
private static char[] |
HEX_ARRAY |
private static java.util.regex.Pattern |
HTTP_PREFFIX_PATTERN |
private static int |
MILLIS_OF_DAY |
private static int |
MILLIS_OF_HOUR |
private static int |
MILLIS_OF_MINUTE |
private static int |
MILLIS_OF_SECOND |
static java.lang.String |
URL_CHARS |
static java.util.regex.Pattern |
WHITE_SPACES_PATTERN |
Modifier | Constructor and Description |
---|---|
private |
Utils() |
Modifier and Type | Method and Description |
---|---|
static <T> T[] |
addInArrayCopy(T[] array,
T item)
Adds the given item at the end of a new copy of given array.
|
static <T> T |
cast(java.lang.Object o,
java.lang.Class<T> klass)
Cast an object savely.
|
static void |
close(java.io.Closeable c)
Utility method for closing a
Closeable object. |
static void |
close(java.util.zip.ZipFile zip)
Utility method for closing a
ZipFile . |
static java.lang.Integer |
color_float2int(java.lang.Float val)
convert float range 0 <= x <= 1 to integer range 0..255
when dealing with colors and color alpha value
|
static java.lang.Float |
color_int2float(java.lang.Integer val)
convert integer range 0..255 to float range 0 <= x <= 1
when dealing with colors and color alpha value
|
static java.awt.Color |
complement(java.awt.Color clr) |
static char[] |
copyArray(char[] array)
Copies the given array.
|
static int[] |
copyArray(int[] array)
Copies the given array.
|
static <T> T[] |
copyArray(T[] array)
Copies the given array.
|
static void |
copyDirectory(java.io.File in,
java.io.File out)
Recursive directory copy function
|
static java.nio.file.Path |
copyFile(java.io.File in,
java.io.File out)
Simple file copy function that will overwrite the target file.
|
static int |
copyStream(java.io.InputStream source,
java.io.OutputStream destination)
Copy data from source stream to output stream.
|
static boolean |
copyToClipboard(java.lang.String s)
Copies the string
s to system clipboard. |
static boolean |
deleteDirectory(java.io.File path)
Deletes a directory recursively.
|
static void |
ensure(boolean condition,
java.lang.String message,
java.lang.Object... data)
Ensures a logical condition is met.
|
static boolean |
equalsEpsilon(double a,
double b)
Determines if the two given double values are equal (their delta being smaller than a fixed epsilon)
|
static java.lang.String |
execOutput(java.util.List<java.lang.String> command)
Runs an external command and returns the standard output.
|
static <T> boolean |
exists(java.lang.Iterable<? extends T> collection,
Predicate<? super T> predicate)
Tests whether
predicate applies to at least one elements from collection . |
static <T> boolean |
exists(java.lang.Iterable<T> collection,
java.lang.Class<? extends T> klass) |
static java.net.URL |
fileToURL(java.io.File f)
Converts the given file to its URL.
|
static <T> java.util.Collection<T> |
filter(java.util.Collection<? extends T> collection,
Predicate<? super T> predicate) |
static <S,T extends S> |
filteredCollection(java.util.Collection<S> collection,
java.lang.Class<T> klass)
Filter a collection by (sub)class.
|
static <T> T |
find(java.lang.Iterable<? extends T> collection,
Predicate<? super T> predicate) |
static <T> T |
find(java.lang.Iterable<? super T> collection,
java.lang.Class<? extends T> klass) |
static <T> T |
firstNonNull(T... items)
Returns the first element from
items which is non-null, or null if all elements are null. |
static java.lang.String |
fixURLQuery(java.lang.String url)
Fixes URL with illegal characters in the query (and fragment) part by
percent encoding those characters.
|
static <T> boolean |
forAll(java.lang.Iterable<? extends T> collection,
Predicate<? super T> predicate)
Tests whether
predicate applies to all elements from collection . |
static BZip2CompressorInputStream |
getBZip2InputStream(java.io.InputStream in)
Returns a Bzip2 input stream wrapping given input stream.
|
static java.lang.String |
getClipboardContent()
Extracts clipboard content as string.
|
static java.lang.String |
getDurationString(long elapsedTime)
Returns a simple human readable (hours, minutes, seconds) string for a given duration in milliseconds.
|
static java.util.zip.GZIPInputStream |
getGZipInputStream(java.io.InputStream in)
Returns a Gzip input stream wrapping given input stream.
|
static java.io.File |
getJosmTempDir()
Returns the JOSM temp directory.
|
static java.util.List<java.lang.String> |
getMatches(java.util.regex.Matcher m)
Returns a list of capture groups if
Matcher.matches() , or null . |
static java.lang.String |
getPositionListString(java.util.List<java.lang.Integer> positionList)
Returns a human readable representation of a list of positions.
|
static java.lang.Throwable |
getRootCause(java.lang.Throwable t)
Returns the root cause of a throwable object.
|
static java.util.zip.ZipInputStream |
getZipInputStream(java.io.InputStream in)
Returns a Zip input stream wrapping given input stream.
|
static <T> int |
indexOf(java.lang.Iterable<? extends T> collection,
Predicate<? super T> predicate) |
static boolean |
isLocalUrl(java.lang.String url)
Determines if the given URL denotes a file on a local filesystem.
|
static java.lang.String |
join(java.lang.String sep,
java.util.Collection<?> values)
Joins a list of strings (or objects that can be converted to string via
Object.toString()) into a single string with fields separated by sep.
|
static java.lang.String |
joinAsHtmlUnorderedList(java.lang.Iterable<?> values)
Converts the given iterable collection as an unordered HTML list.
|
static int |
max(int a,
int b,
int c,
int d)
Returns the greater of four
int values. |
static java.lang.String |
md5Hex(java.lang.String data)
Calculate MD5 hash of a string and output in hexadecimal format.
|
static int |
min(int a,
int b,
int c)
Returns the minimum of three values.
|
static int |
mod(int a,
int n)
return the modulus in the range [0, n)
|
static Pair<java.lang.Integer,java.util.concurrent.ExecutorService> |
newThreadPool(java.lang.String pref)
Returns a pair containing the number of threads (n), and a thread pool (if n > 1) to perform
multi-thread computation in the context of the given preference key.
|
static java.net.HttpURLConnection |
openHttpConnection(java.net.URL httpURL)
Opens a HTTP connection to the given URL and sets the User-Agent property to JOSM's one.
|
static java.net.HttpURLConnection |
openHttpConnection(java.net.URL httpURL,
boolean keepAlive)
Opens a HTTP connection to the given URL, sets the User-Agent property to JOSM's one and optionnaly disables Keep-Alive.
|
static java.io.InputStream |
openURL(java.net.URL url)
Opens a connection to the given URL and sets the User-Agent property to JOSM's one.
|
static java.io.InputStream |
openURLAndDecompress(java.net.URL url,
boolean decompress)
Opens a connection to the given URL, sets the User-Agent property to JOSM's one, and decompresses stream if necessary.
|
static java.io.BufferedReader |
openURLReader(java.net.URL url)
Opens a connection to the given URL and sets the User-Agent property to JOSM's one.
|
static java.io.BufferedReader |
openURLReaderAndDecompress(java.net.URL url,
boolean decompress)
Opens a connection to the given URL and sets the User-Agent property to JOSM's one.
|
static java.net.URLConnection |
setupURLConnection(java.net.URLConnection connection)
Setups the given URL connection to match JOSM needs by setting its User-Agent and timeout properties.
|
static java.lang.String |
shortenString(java.lang.String s,
int maxLength)
If the string
s is longer than maxLength , the string is cut and "..." is appended. |
static java.lang.String |
strip(java.lang.String str)
An alternative to
String.trim() to effectively remove all leading and trailing white characters, including Unicode ones. |
static java.lang.String |
toHexString(byte[] bytes)
Converts a byte array to a string of hexadecimal characters.
|
static <T> java.util.List<T> |
topologicalSort(MultiMap<T,T> dependencies)
Topological sort.
|
static java.lang.String |
toString(java.awt.Color c)
convert Color to String
(Color.toString() omits alpha value)
|
static <A,B> java.util.Collection<B> |
transform(java.util.Collection<? extends A> c,
Utils.Function<A,B> f)
Transforms the collection
c into an unmodifiable collection and
applies the Utils.Function f on each element upon access. |
static <A,B> java.util.List<B> |
transform(java.util.List<? extends A> l,
Utils.Function<A,B> f)
Transforms the list
l into an unmodifiable list and
applies the Utils.Function f on each element upon access. |
static java.lang.String |
updateSystemProperty(java.lang.String key,
java.lang.String value)
Updates a given system property.
|
public static final java.util.regex.Pattern WHITE_SPACES_PATTERN
private static final int MILLIS_OF_SECOND
private static final int MILLIS_OF_MINUTE
private static final int MILLIS_OF_HOUR
private static final int MILLIS_OF_DAY
public static final java.lang.String URL_CHARS
private static final double EPSILON
private static final char[] HEX_ARRAY
private static final java.util.regex.Pattern HTTP_PREFFIX_PATTERN
private Utils()
public static <T> boolean exists(java.lang.Iterable<? extends T> collection, Predicate<? super T> predicate)
predicate
applies to at least one elements from collection
.public static <T> boolean forAll(java.lang.Iterable<? extends T> collection, Predicate<? super T> predicate)
predicate
applies to all elements from collection
.public static <T> boolean exists(java.lang.Iterable<T> collection, java.lang.Class<? extends T> klass)
public static <T> T find(java.lang.Iterable<? extends T> collection, Predicate<? super T> predicate)
public static <T> T find(java.lang.Iterable<? super T> collection, java.lang.Class<? extends T> klass)
public static <T> java.util.Collection<T> filter(java.util.Collection<? extends T> collection, Predicate<? super T> predicate)
@SafeVarargs public static <T> T firstNonNull(T... items)
items
which is non-null, or null if all elements are null.items
- the items to look forpublic static <S,T extends S> SubclassFilteredCollection<S,T> filteredCollection(java.util.Collection<S> collection, java.lang.Class<T> klass)
public static <T> int indexOf(java.lang.Iterable<? extends T> collection, Predicate<? super T> predicate)
public static int min(int a, int b, int c)
a
- an argument.b
- another argument.c
- another argument.a
, b
and c
.public static int max(int a, int b, int c, int d)
int
values. That is, the
result is the argument closer to the value of
Integer.MAX_VALUE
. If the arguments have the same value,
the result is that same value.a
- an argument.b
- another argument.c
- another argument.d
- another argument.a
, b
, c
and d
.public static void ensure(boolean condition, java.lang.String message, java.lang.Object... data)
condition
- the condition to be metmessage
- Formatted error message to raise if condition is not metdata
- Message parameters, optionaljava.lang.AssertionError
- if the condition is not metpublic static int mod(int a, int n)
public static java.lang.String join(java.lang.String sep, java.util.Collection<?> values)
sep
- the separatorvalues
- collection of objects, null is converted to the
empty stringpublic static java.lang.String joinAsHtmlUnorderedList(java.lang.Iterable<?> values)
values
- The iterable collectionpublic static java.lang.String toString(java.awt.Color c)
public static java.lang.Integer color_float2int(java.lang.Float val)
public static java.lang.Float color_int2float(java.lang.Integer val)
public static java.awt.Color complement(java.awt.Color clr)
public static <T> T[] copyArray(T[] array)
Arrays.copyOf(T[], int)
, this method is null-safe.array
- The array to copynull
if array
is nullpublic static char[] copyArray(char[] array)
Arrays.copyOf(T[], int)
, this method is null-safe.array
- The array to copynull
if array
is nullpublic static int[] copyArray(int[] array)
Arrays.copyOf(T[], int)
, this method is null-safe.array
- The array to copynull
if array
is nullpublic static java.nio.file.Path copyFile(java.io.File in, java.io.File out) throws java.io.IOException
in
- The source fileout
- The destination filejava.io.IOException
- If any I/O error occursjava.lang.IllegalArgumentException
- If in
or out
is null
public static void copyDirectory(java.io.File in, java.io.File out) throws java.io.IOException
in
- The source directoryout
- The destination directoryjava.io.IOException
- If any I/O error ooccursjava.lang.IllegalArgumentException
- If in
or out
is null
public static int copyStream(java.io.InputStream source, java.io.OutputStream destination) throws java.io.IOException
source
- source streamdestination
- target streamjava.io.IOException
- if any I/O error occurspublic static boolean deleteDirectory(java.io.File path)
path
- The directory to deletetrue
if and only if the file or directory is
successfully deleted; false
otherwisepublic static void close(java.io.Closeable c)
Utility method for closing a Closeable
object.
c
- the closeable object. May be null.public static void close(java.util.zip.ZipFile zip)
Utility method for closing a ZipFile
.
zip
- the zip file. May be null.public static java.net.URL fileToURL(java.io.File f)
f
- The file to get URL fromnull
if not possible.public static boolean equalsEpsilon(double a, double b)
a
- The first double value to compareb
- The second double value to comparetrue
if abs(a - b) <= 1e-11
, false
otherwisepublic static boolean copyToClipboard(java.lang.String s)
s
to system clipboard.s
- string to be copied to clipboard.public static java.lang.String getClipboardContent()
null
otherwise.public static java.lang.String md5Hex(java.lang.String data)
data
- arbitrary Stringpublic static java.lang.String toHexString(byte[] bytes)
bytes
- the byte arraypublic static <T> java.util.List<T> topologicalSort(MultiMap<T,T> dependencies)
dependencies
- contains mappings (key -> value). In the final list of sorted objects, the key will come
after the value. (In other words, the key depends on the value(s).)
There must not be cyclic dependencies.public static <A,B> java.util.Collection<B> transform(java.util.Collection<? extends A> c, Utils.Function<A,B> f)
c
into an unmodifiable collection and
applies the Utils.Function
f
on each element upon access.A
- class of input collectionB
- class of transformed collectionc
- a collectionf
- a function that transforms objects of A
to objects of B
public static <A,B> java.util.List<B> transform(java.util.List<? extends A> l, Utils.Function<A,B> f)
l
into an unmodifiable list and
applies the Utils.Function
f
on each element upon access.A
- class of input collectionB
- class of transformed collectionl
- a collectionf
- a function that transforms objects of A
to objects of B
public static java.net.HttpURLConnection openHttpConnection(java.net.URL httpURL) throws java.io.IOException
httpURL
- The HTTP url to open (must use http:// or https://)java.io.IOException
- if an I/O exception occurs.public static java.io.InputStream openURL(java.net.URL url) throws java.io.IOException
url
- The url to openjava.io.IOException
- if an I/O exception occurs.public static java.io.InputStream openURLAndDecompress(java.net.URL url, boolean decompress) throws java.io.IOException
url
- The url to opendecompress
- whether to wrap steam in a GZIPInputStream
or BZip2CompressorInputStream
if the Content-Type
header is set accordingly.java.io.IOException
- if an I/O exception occurs.public static BZip2CompressorInputStream getBZip2InputStream(java.io.InputStream in) throws java.io.IOException
in
- The raw input streamnull
if in
is null
java.io.IOException
- if the given input stream does not contain valid BZ2 headerpublic static java.util.zip.GZIPInputStream getGZipInputStream(java.io.InputStream in) throws java.io.IOException
in
- The raw input streamnull
if in
is null
java.io.IOException
- if an I/O error has occurredpublic static java.util.zip.ZipInputStream getZipInputStream(java.io.InputStream in) throws java.io.IOException
in
- The raw input streamnull
if in
is null
java.io.IOException
- if an I/O error has occurredpublic static java.net.URLConnection setupURLConnection(java.net.URLConnection connection)
connection
- The connection to setupconnection
, with updated propertiespublic static java.io.BufferedReader openURLReader(java.net.URL url) throws java.io.IOException
url
- The url to openjava.io.IOException
- if an I/O exception occurs.public static java.io.BufferedReader openURLReaderAndDecompress(java.net.URL url, boolean decompress) throws java.io.IOException
url
- The url to opendecompress
- whether to wrap steam in a GZIPInputStream
or BZip2CompressorInputStream
if the Content-Type
header is set accordingly.java.io.IOException
- if an I/O exception occurs.public static java.net.HttpURLConnection openHttpConnection(java.net.URL httpURL, boolean keepAlive) throws java.io.IOException
httpURL
- The HTTP url to open (must use http:// or https://)keepAlive
- whether not to set header Connection=close
java.io.IOException
- if an I/O exception occurs.public static java.lang.String strip(java.lang.String str)
String.trim()
to effectively remove all leading and trailing white characters, including Unicode ones.str
- The string to stripstr
, without leading and trailing characters, according to
Character.isWhitespace(char)
and Character.isSpaceChar(char)
.public static java.lang.String execOutput(java.util.List<java.lang.String> command) throws java.io.IOException
command
- the command with argumentsjava.io.IOException
- when there was an error, e.g. command does not existpublic static java.io.File getJosmTempDir()
<java.io.tmpdir>/JOSM
), or null
if java.io.tmpdir
is not definedpublic static java.lang.String getDurationString(long elapsedTime)
elapsedTime
- The duration in millisecondsjava.lang.IllegalArgumentException
- if elapsedTime is < 0public static java.lang.String getPositionListString(java.util.List<java.lang.Integer> positionList)
For instance, [1,5,2,6,7
yields "1-2,5-7
positionList
- a list of positionspublic static java.util.List<java.lang.String> getMatches(java.util.regex.Matcher m)
Matcher.matches()
, or null
.
The first element (index 0) is the complete match.
Further elements correspond to the parts in parentheses of the regular expression.m
- the matcherMatcher.matches()
, or null
.public static <T> T cast(java.lang.Object o, java.lang.Class<T> klass)
T
- the target typeo
- the object to castklass
- the target class (same as T)o
is null or the type o
is not
a subclass of klass
. The casted value otherwise.public static java.lang.Throwable getRootCause(java.lang.Throwable t)
t
- The object to get root cause fort
public static <T> T[] addInArrayCopy(T[] array, T item)
array
- The source arrayitem
- The item to addarray
containing item
as additional last elementpublic static java.lang.String shortenString(java.lang.String s, int maxLength)
s
is longer than maxLength
, the string is cut and "..." is appended.s
- String to shortenmaxLength
- maximum number of characters to keep (not including the "...")public static java.lang.String fixURLQuery(java.lang.String url)
url
- the URL that should be fixedpublic static boolean isLocalUrl(java.lang.String url)
url
- The URL to testtrue
if the url points to a local filepublic static Pair<java.lang.Integer,java.util.concurrent.ExecutorService> newThreadPool(java.lang.String pref)
pref
- The preference keypublic static java.lang.String updateSystemProperty(java.lang.String key, java.lang.String value)
key
- The property keyvalue
- The property valuenull
if it did not have one.