com.myjavatools.lib
Class Objects

java.lang.Object
  extended bycom.myjavatools.lib.Objects
Direct Known Subclasses:
Strings

public class Objects
extends java.lang.Object


Method Summary
static java.util.Map asMap(java.lang.Object[] pairs)
          Deprecated.  
static java.util.List cat(java.util.Collection first, java.util.Collection second)
          Concatenates two collections into one List
static java.util.Map compose(java.util.Map f, java.util.Map g)
          composes two Maps (like functions, if you know what I mean)
static long crc32(byte[] data)
          Calculates crc32 on a byte array
static long crc32(byte[] data, int off, int len)
          Calculates crc32 on a byte array
static java.util.List getList(int capacity)
          List factory
static java.util.Map getMap()
          Map factory
static java.util.Map getMap(int capacity)
          Map factory
static java.util.Set getSet(int capacity)
          Set factory
static int indexOf(java.lang.Object what, java.util.List list)
          Gets the index of the first element of an list that equals to specified object
static int indexOf(java.lang.Object what, java.util.List list, int fromIndex)
          Gets the index of the next element of a list that equals to specified object
static int indexOf(java.lang.Object what, java.lang.Object[] array)
          Gets the index of the first element of an array that equals to specified object
static int indexOf(java.lang.Object what, java.lang.Object[] array, int fromIndex)
          Gets the index of the next element of an array that equals to specified object
static java.util.Map inverse(java.util.Map f)
          Inverses a Map
static boolean isEmpty(java.lang.Object data)
          In a "creative" way checks whether a string or a container is empty.
static java.util.Collection map(java.util.Map m, java.util.Collection domain)
          Maps a Collection using Map.
static java.util.List map(java.util.Map m, java.util.List domain)
          Maps a List using Map.
static java.lang.Object[] map(java.util.Map m, java.lang.Object[] domain)
          Maps an array using Map.
static java.lang.Object[] map(java.util.Map m, java.lang.Object[] domain, java.lang.Object defaultValue)
          Maps an array using Map, with a default value.
static java.lang.Object[] map(java.util.Map m, java.lang.Object[] domain, java.lang.Object[] defaultValues)
          Maps an array using Map, with default values.
static java.util.HashMap reallocate(java.util.Map map)
          Reallocates Map to a new HashMap, to improve hash map access.
static byte[] toBytes(char[] from)
          Converts char array to byte array (per-element casting)
static byte[] toBytes(long from)
          Converts long to byte array (lower bytes first)
static char[] toChars(byte[] from)
          Converts byte array to char array (per-element casting)
static java.util.Map toMap(java.lang.Object[] pairs)
          makes a Map from key-value pairs
static java.util.Map toMap(java.lang.Object key, java.lang.Object value)
          makes a singleton Map from a key-value pair
static java.util.Map toMap(java.lang.Object key1, java.lang.Object value1, java.lang.Object key2, java.lang.Object value2)
          makes a Map from two key-value pairs
static java.util.Map toMap(java.lang.Object key1, java.lang.Object value1, java.lang.Object key2, java.lang.Object value2, java.lang.Object key3, java.lang.Object value3)
          makes a Map from three key-value pairs
static java.util.Set toSet(java.lang.Object[] objects)
          makes a Set from an array of objects
static java.util.Set union(java.util.Collection first, java.util.Collection second)
          Creates set union of two collections
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

map

public static final java.lang.Object[] map(java.util.Map m,
                                           java.lang.Object[] domain)
Maps an array using Map. If Map m is considered as a map from its keys to its values, then for each x in domain m(x) is calculated and stored into resulting array

Parameters:
m - the map
domain - the domain array
Returns:
an array with the same number of elements as domain, and with elements being values that correspond to the map's keys.

Example:

Suppose we have the following: Map m = new HashMap();
m.put("a", "ValueOfA"); m.put("b", "ValueOfB"); m.put("c", "ValueOfC");

Then map(m, new String[] {"b", "x", "b"}) returns new String[] {"ValueOfB", null, "ValueOfB"}.


map

public static final java.util.List map(java.util.Map m,
                                       java.util.List domain)
Maps a List using Map. If Map m is considered as a map from its keys to its values, then for each x in domain m(x) is calculated and stored into resulting List

Parameters:
m - the map
domain - the domain list
Returns:
a list with the same number of elements as domain, and with elements being values that correspond to the map's keys.

Example:

Suppose we have the following: Map m = new HashMap();
m.put("a", "ValueOfA"); m.put("b", "ValueOfB"); m.put("c", "ValueOfC");

Then map(m, Arrays.asList(new String[] {"b", "x", "b"})) returns a List that contains "ValueOfB", null, and "ValueOfB".


map

public static final java.util.Collection map(java.util.Map m,
                                             java.util.Collection domain)
Maps a Collection using Map. If Map m is considered as a map from its keys to its values, then for each x in domain m(x) is calculated and stored into resulting List

Parameters:
m - the map
domain - the domain list
Returns:
a collection with the same number of elements as domain, and with elements being values that correspond to the map's keys.

Example:

Suppose we have the following: Map m = new HashMap();
m.put("a", "ValueOfA"); m.put("b", "ValueOfB"); m.put("c", "ValueOfC");

Then map(m, Arrays.asList(new String[] {"b", "x", "b"})) returns a Collection that contains "ValueOfB", null, and "ValueOfB".


map

public static final java.lang.Object[] map(java.util.Map m,
                                           java.lang.Object[] domain,
                                           java.lang.Object defaultValue)
Maps an array using Map, with a default value. If Map m is considered as a map from its keys to its values, then for each x in domain m(x) is calculated and stored into resulting array

Parameters:
m - the map
domain - the domain array
defaultValue - the value that is returned when an element of domain is not in the list of m keys
Returns:
an array with the same number of elements as domain, and with elements being values that correspond to the map's keys.

Example:

Suppose we have the following: Map m = new HashMap();
m.add("a", "ValueOfA"); m.add("b", "ValueOfB", m.add("c", "ValueOfC");

Then map(m, new String[] {"b", "x", "b"}, "Johnny Doe")
returns new String[] {"ValueOfB", "Johnny Doe", "ValueOfB"}.


map

public static final java.lang.Object[] map(java.util.Map m,
                                           java.lang.Object[] domain,
                                           java.lang.Object[] defaultValues)
Maps an array using Map, with default values. If Map m is considered as a map from its keys to its values, then for each x in domain m(x) is calculated and stored into resulting array

Parameters:
m - the map
domain - the domain array
defaultValues - the values that are returned when an element of domain is not in the list of m keys
Returns:
an array with the same number of elements as domain, and with elements being values that correspond to the map's keys.

Example:

Suppose we have the following: Map m = new HashMap();
m.add("a", "ValueOfA"); m.add("b", "ValueOfB", m.add("c", "ValueOfC");

Then map(m, new String[] {"b", "x", "b"}, new String[] {"Johnny A", "Johnny Bee", "Johnny Doe"})
returns new String[] {"ValueOfB", "Johnny Bee", "ValueOfB"}.


compose

public static java.util.Map compose(java.util.Map f,
                                    java.util.Map g)
composes two Maps (like functions, if you know what I mean)

Parameters:
f - Map first Map
g - Map second Map
Returns:
Map composition, x -> g(f(x));

Example:

Suppose we have the following:
Map f = toMap(new String[] {"1", "one", "2", "two", "3", "three"});
Map g = toMap(new String[] {"one", "uno", "two", "dos", "three", "tres"});

Then compose(f, g)
returns the same map as produced by
toMap(new String[] {"1", "uno", "2", "dos", "3", "tres"});


inverse

public static java.util.Map inverse(java.util.Map f)
                             throws java.lang.InstantiationException
Inverses a Map

Parameters:
f - Map to inverse, must be monomorphic (one-to-one)
Returns:
Map inverse to f

Example:

Suppose we have the following:
Map f = toMap(new String[] {"1", "one", "2", "two", "3", "three"});

Then inverse(f)
returns the same map as produced by
toMap(new String[] {"one", "1", "two", "2", "three", "3"});

Throws:
java.lang.InstantiationException - in case f is not one-to-one

asMap

public static final java.util.Map asMap(java.lang.Object[] pairs)
Deprecated.  

Create a Map from an array of key-value pairs

Parameters:
pairs - the source array
Returns:
a new Map
See Also:


Example:
  • asMap(new String[] {"1", "one", "2", "two", "3", "three"}) returns a map with three keys ("1", "2", "3"), and guess which values.

  • isEmpty

    public static final boolean isEmpty(java.lang.Object data)
    In a "creative" way checks whether a string or a container is empty.
    Accepts a Collection, a Map, an array, a String.

    Parameters:
    data - a Collection or a Map or an array or a string to check
    Returns:
    true if data is empty

    Examples:
  • isEmpty(""), isEmpty(null), isEmpty(new HashMap()) all return true;
  • isEmpty(" "), isEmpty(new int[] {1}) returns false.

  • toBytes

    public static final byte[] toBytes(char[] from)
    Converts char array to byte array (per-element casting)

    Parameters:
    from - char array
    Returns:
    byte array

    Example:
  • toBytes(new char[] {0x0123, 0x4567, 0x89ab, 0xcdef}) returns {0x23, 0x67, (byte)0xab, (byte)0xef}.

  • toChars

    public static final char[] toChars(byte[] from)
    Converts byte array to char array (per-element casting)

    Parameters:
    from - byte array
    Returns:
    char array

    Example:
  • toChars(new byte[] {0x23, 0x67, (byte)0xab, (byte)0xef}) returns new char[] {0x23, 0x67, 0xab, 0xef}.

  • crc32

    public static final long crc32(byte[] data)
    Calculates crc32 on a byte array

    Parameters:
    data - source bytes
    Returns:
    its crc32

    Example:
  • crc32(new byte[] {1, 2, 3}) returns 1438416925.

  • crc32

    public static final long crc32(byte[] data,
                                   int off,
                                   int len)
    Calculates crc32 on a byte array

    Parameters:
    data - source bytes
    off - offset in the array
    len - length of the area to crc
    Returns:
    its crc32

    Example:
  • crc32(new byte[] {0, 1, 2, 3, 4}, 1, 3) returns 1438416925.

  • toBytes

    public static final byte[] toBytes(long from)
    Converts long to byte array (lower bytes first)

    Parameters:
    from - the long value
    Returns:
    byte array

    Example:
  • toBytes(0x0123456789abcdefl) returns {(byte)0xef, (byte)0xcd, (byte)0xab, (byte)0x89, 0x67, 0x45, 0x23, 0x01}.

  • indexOf

    public static int indexOf(java.lang.Object what,
                              java.lang.Object[] array)
    Gets the index of the first element of an array that equals to specified object

    Parameters:
    what - the object to look for in array
    array - array of objects to look for what
    Returns:
    index of the object in array, or -1 if none found

    Examples:
  • indexOf("abc", new String[] {"123", "abc", "xyz"}) returns 1;
  • indexOf(null, new String[] {"123", "abc", null}) returns 2;

  • indexOf

    public static int indexOf(java.lang.Object what,
                              java.lang.Object[] array,
                              int fromIndex)
    Gets the index of the next element of an array that equals to specified object

    Parameters:
    what - the object to look for in array
    array - array of objects to look for what
    fromIndex - start search from this position
    Returns:
    index of the object in array, or -1 if none found

    Examples:
  • indexOf("abc", new String[] {"abc", "abc", "xyz", 1}) returns 1;
  • indexOf(null, new String[] {"123", "abc", null}}, 1) returns 2;

  • indexOf

    public static int indexOf(java.lang.Object what,
                              java.util.List list)
    Gets the index of the first element of an list that equals to specified object

    Parameters:
    what - the object to look for in list
    list - list of objects to look for what
    Returns:
    index of the object in list, or -1 if none found

    Examples:
  • List l = new ArrayList(); l.add("123"); l.add("abc"), l.add("xyz"); indexOf("abc", list, 1} returns 1;
  • List l = new ArrayList(); l.add("123"); l.add("abc"), l.add(null);
  • indexOf(null, list) returns 2;

  • indexOf

    public static int indexOf(java.lang.Object what,
                              java.util.List list,
                              int fromIndex)
    Gets the index of the next element of a list that equals to specified object

    Parameters:
    what - the object to look for in list
    list - list of objects to look for what
    fromIndex - start search from this position
    Returns:
    index of the object in list, or -1 if none found

    Examples:
  • List l = new ArrayList(); l.add("abc"); l.add("abc"), l.add("xyz"); indexOf("abc", list, 1} returns 1;
  • List l = new ArrayList(); l.add("abc"); l.add("abc"), l.add(null); indexOf(null, list, 1} returns 2;

  • getMap

    public static java.util.Map getMap()
    Map factory

    Returns:
    Map - a new empty Map

    getMap

    public static java.util.Map getMap(int capacity)
    Map factory

    Parameters:
    capacity - the number of keys
    Returns:
    Map - a new empty Map of specified capacity

    getSet

    public static java.util.Set getSet(int capacity)
    Set factory

    Parameters:
    capacity - the number of expected elements
    Returns:
    Set - a new empty Set of specified capacity

    getList

    public static java.util.List getList(int capacity)
    List factory

    Parameters:
    capacity - the number of expected elements
    Returns:
    List - a new empty List of specified capacity

    toMap

    public static java.util.Map toMap(java.lang.Object key,
                                      java.lang.Object value)
    makes a singleton Map from a key-value pair

    Parameters:
    key - Object
    value - Object
    Returns:
    Map that has just one key and its value

    Example:
  • toMap("the key", "This is the value").get("the key"); returns "This is the value";

  • toMap

    public static java.util.Map toMap(java.lang.Object key1,
                                      java.lang.Object value1,
                                      java.lang.Object key2,
                                      java.lang.Object value2)
    makes a Map from two key-value pairs

    Parameters:
    key1 - Object first key
    value1 - Object first value
    key2 - Object second key
    value2 - Object second value
    Returns:
    Map that has these two keys and values

    Example:
  • toMap("2", "kaksi", "3", "kolmi").get("3"); returns "kolmi";

  • toMap

    public static java.util.Map toMap(java.lang.Object key1,
                                      java.lang.Object value1,
                                      java.lang.Object key2,
                                      java.lang.Object value2,
                                      java.lang.Object key3,
                                      java.lang.Object value3)
    makes a Map from three key-value pairs

    Parameters:
    key1 - Object first key
    value1 - Object first value
    key2 - Object second key
    value2 - Object second value
    key3 - Object third key
    value3 - Object third value
    Returns:
    Map the map that contains these three keys and values

    Example:
  • toMap("1", "un", "2", "deux", "3", "troix").get("2"); returns "deux";

  • toMap

    public static java.util.Map toMap(java.lang.Object[] pairs)
    makes a Map from key-value pairs

    Parameters:
    pairs - Object[] odd elements of the array are keys, and even elements are values
    Returns:
    Map the map that maps odd elements from pairs array to even elements; if pairs is null, returns null

    Example:
  • toMap(new String[] {"1", "un", "2", "deux", "3", "troix"}).get("2"); returns "deux";

  • toSet

    public static java.util.Set toSet(java.lang.Object[] objects)
    makes a Set from an array of objects

    Parameters:
    objects - Object[] objects to fill the collection
    Returns:
    Set the set that contains all the objects from array if objects array is null, returns null

    Example:
  • toSet(new String[] {"1", "2", "3").size(); returns 3;

  • reallocate

    public static java.util.HashMap reallocate(java.util.Map map)
    Reallocates Map to a new HashMap, to improve hash map access. Unfortunately, in 1.4.2 the hashmaps created all have 2^n size - have to wait for 1.5 to do something about it. Prime numbers would be much better.

    Parameters:
    map - Map
    Returns:
    HashMap

    union

    public static java.util.Set union(java.util.Collection first,
                                      java.util.Collection second)
    Creates set union of two collections

    Parameters:
    first - Collection
    second - Collections
    Returns:
    Set that is a union of first and second as sets. nulls are tolerated and ignored

    Example:
  • union(Arrays.asList(new String[] {"a", "b", "c"}), Arrays.asList(new String[] {"b", "a", "d"}) returns the same list as new HashSet(Arrays.asList(new String[] {"a", "b", "c", "d"})).

  • cat

    public static java.util.List cat(java.util.Collection first,
                                     java.util.Collection second)
    Concatenates two collections into one List

    Parameters:
    first - Collection
    second - Collection
    Returns:
    List that is a list of elements from the first collection followed by elements from the second collection. nulls are tolerated and ignored

    Example:
  • cat(Arrays.asList(new String[] {"a", "b", "c"}), Arrays.asList(new String[] {"b", "a", "d"})) returns the same list as new ArrayList(Arrays.asList(new String[] {"a", "b", "c", "b", "a", "d"})).