org.openstreetmap.josm.tools
Class Utils

java.lang.Object
  extended by org.openstreetmap.josm.tools.Utils

public class Utils
extends java.lang.Object

Basic utils, that can be useful in different parts of the program.


Nested Class Summary
static interface Utils.Function<A,B>
          Represents a function that can be applied to objects of A and returns objects of B.
 
Field Summary
private static double EPSILION
           
 
Constructor Summary
Utils()
           
 
Method Summary
static void close(java.io.InputStream is)
          Utility method for closing an input stream.
static void close(java.io.OutputStream os)
          Utility method for closing an output stream.
static void close(java.io.Reader reader)
          Utility method for closing a reader.
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 back
static java.awt.Color complement(java.awt.Color clr)
           
static int copyStream(java.io.InputStream source, java.io.OutputStream destination)
           
static boolean copyToClipboard(java.lang.String s)
          Copies the string s to system clipboard.
static boolean deleteDirectory(java.io.File path)
           
static void ensure(boolean condition, java.lang.String message, java.lang.Object... data)
           
static
<T> boolean
equal(T a, T b)
          for convenience: test whether 2 objects are either both null or a.equals(b)
static boolean equalsEpsilon(double a, double b)
           
static
<T> boolean
exists(java.lang.Iterable<? extends T> collection, Predicate<? super T> predicate)
           
static
<T> boolean
exists(java.lang.Iterable<T> collection, java.lang.Class<? extends T> klass)
           
static
<T> java.util.Collection<T>
filter(java.util.Collection<? extends T> collection, Predicate<? super T> predicate)
           
static
<S,T extends S>
SubclassFilteredCollection<S,T>
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)
           
static java.lang.String getClipboardContent()
          Extracts clipboard content as string.
static
<T> int
indexOf(java.lang.Iterable<? extends T> collection, Predicate<? super T> predicate)
           
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.util.Collection<?> values)
           
static int max(int a, int b, int c, int d)
           
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)
          Get minimum of 3 values
static int mod(int a, int n)
          return the modulus in the range [0, n)
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EPSILION

private static final double EPSILION
See Also:
Constant Field Values
Constructor Detail

Utils

public Utils()
Method Detail

exists

public static <T> boolean exists(java.lang.Iterable<? extends T> collection,
                                 Predicate<? super T> predicate)

exists

public static <T> boolean exists(java.lang.Iterable<T> collection,
                                 java.lang.Class<? extends T> klass)

find

public static <T> T find(java.lang.Iterable<? extends T> collection,
                         Predicate<? super T> predicate)

find

public static <T> T find(java.lang.Iterable<? super T> collection,
                         java.lang.Class<? extends T> klass)

filter

public static <T> java.util.Collection<T> filter(java.util.Collection<? extends T> collection,
                                                 Predicate<? super T> predicate)

firstNonNull

public static <T> T firstNonNull(T... items)

filteredCollection

public static <S,T extends S> SubclassFilteredCollection<S,T> filteredCollection(java.util.Collection<S> collection,
                                                                                 java.lang.Class<T> klass)
Filter a collection by (sub)class. This is an efficient read-only implementation.


indexOf

public static <T> int indexOf(java.lang.Iterable<? extends T> collection,
                              Predicate<? super T> predicate)

min

public static int min(int a,
                      int b,
                      int c)
Get minimum of 3 values


max

public static int max(int a,
                      int b,
                      int c,
                      int d)

equal

public static <T> boolean equal(T a,
                                T b)
for convenience: test whether 2 objects are either both null or a.equals(b)


ensure

public static void ensure(boolean condition,
                          java.lang.String message,
                          java.lang.Object... data)

mod

public static int mod(int a,
                      int n)
return the modulus in the range [0, n)


join

public 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.

Parameters:
sep - the separator
values - collection of objects, null is converted to the empty string
Returns:
null if values is null. The joined string otherwise.

joinAsHtmlUnorderedList

public static java.lang.String joinAsHtmlUnorderedList(java.util.Collection<?> values)

toString

public static java.lang.String toString(java.awt.Color c)
convert Color to String (Color.toString() omits alpha value)


color_float2int

public 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

Returns:
null if val is null, the corresponding int if val is in the range 0...1. If val is outside that range, return 255

color_int2float

public static java.lang.Float color_int2float(java.lang.Integer val)
convert back


complement

public static java.awt.Color complement(java.awt.Color clr)

copyStream

public static int copyStream(java.io.InputStream source,
                             java.io.OutputStream destination)
                      throws java.io.IOException
Throws:
java.io.IOException

deleteDirectory

public static boolean deleteDirectory(java.io.File path)

close

public static void close(java.io.InputStream is)

Utility method for closing an input stream.

Parameters:
is - the input stream. May be null.

close

public static void close(java.io.OutputStream os)

Utility method for closing an output stream.

Parameters:
os - the output stream. May be null.

close

public static void close(java.io.Reader reader)

Utility method for closing a reader.

Parameters:
reader - the reader. May be null.

equalsEpsilon

public static boolean equalsEpsilon(double a,
                                    double b)

copyToClipboard

public static boolean copyToClipboard(java.lang.String s)
Copies the string s to system clipboard.

Parameters:
s - string to be copied to clipboard.
Returns:
true if succeeded, false otherwise.

getClipboardContent

public static java.lang.String getClipboardContent()
Extracts clipboard content as string.

Returns:
string clipboard contents if available, null otherwise.

md5Hex

public static java.lang.String md5Hex(java.lang.String data)
Calculate MD5 hash of a string and output in hexadecimal format. Output has length 32 with characters in range [0-9a-f]


toHexString

public static java.lang.String toHexString(byte[] bytes)
Converts a byte array to a string of hexadecimal characters. Preserves leading zeros, so the size of the output string is always twice the number of input bytes.


topologicalSort

public static <T> java.util.List<T> topologicalSort(MultiMap<T,T> dependencies)
Topological sort.

Parameters:
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.
Returns:
the list of sorted objects

transform

public 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.

Type Parameters:
A - class of input collection
B - class of transformed collection
Parameters:
c - a collection
f - a function that transforms objects of A to objects of B
Returns:
the transformed unmodifiable collection


JOSM