com.myjavatools.lib.foundation
Class Objects

java.lang.Object
  extended by com.myjavatools.lib.foundation.Objects

public abstract class Objects
extends java.lang.Object

Objects is a utility class that contains various unsorted static methods for foundation package

Since:
5.0
Version:
5.0, 6.0 05/04/06
See Also:
Maps, Function

Constructor Summary
Objects()
           
 
Method Summary
static
<T> void
addAll(java.util.Collection<T> collection, java.lang.Iterable<T> toAdd)
          Adds all elements of iterable to a collection.
static boolean equal(java.lang.Object x, java.lang.Object y)
          The method that everyone needs and nobody wants to make public.
static
<T> int
indexOf(T what, java.util.List<T> list, int fromIndex)
          Gets the index of the next element of a list that equals to specified object
static
<T> int
indexOf(T what, T[] array)
          Gets the index of the first element of an array that equals to specified object
static
<T> int
indexOf(T what, T[] array, int fromIndex)
          Gets the index of the next element of an array that equals to specified object
static
<T> boolean
isEmpty(T data)
          In a "creative", anti-scientific way checks whether a string or a container is empty.
static
<T> T
oneOf(T... arglist)
          Chooses the first non-empty object out of objects in parameter list.
static
<T> java.util.Set<T>
toSet(T... elements)
          Deprecated. since 5.0; the usefulness of this method is doubtful
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Objects

public Objects()
Method Detail

indexOf

public static <T> int indexOf(T what,
                              T[] array)
Gets the index of the first element of an array that equals to specified object

Parameters:
what - T the object to look for in array
array - T[] 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;

  • equal

    public static boolean equal(java.lang.Object x,
                                java.lang.Object y)
    The method that everyone needs and nobody wants to make public.

    Parameters:
    x - Object
    y - Object
    Returns:
    boolean x == y || x != null && x.equals(y)

    indexOf

    public static <T> int indexOf(T what,
                                  T[] array,
                                  int fromIndex)
    Gets the index of the next element of an array that equals to specified object

    Parameters:
    what - T the object to look for in array
    array - T[] array of objects to look for what
    fromIndex - int 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 <T> int indexOf(T what,
                                  java.util.List<T> list,
                                  int fromIndex)
    Gets the index of the next element of a list that equals to specified object

    Parameters:
    what - T the object to look for in list
    list - List<T> list of objects to look for what
    fromIndex - int 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;

  • toSet

    public static <T> java.util.Set<T> toSet(T... elements)
    Deprecated. since 5.0; the usefulness of this method is doubtful

    makes a Set from an array of elements

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

    Examples:
  • toSet(new String[] {"1", "2", "3").size() return 3;
  • toSet(3, 1, 4, 1, 5, 9, 2, 6, 5).size() returns 7.

  • isEmpty

    public static <T> boolean isEmpty(T data)
    In a "creative", anti-scientific 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[] {0}) returns false.

  • oneOf

    public static <T> T oneOf(T... arglist)
    Chooses the first non-empty object out of objects in parameter list.

    Parameters:
    arglist - the first candidate ...
    Returns:
    the first one of the list of candidates that is not empty, converted to String

    Examples:
  • oneOf(null, "xyz") returns "xyz";
  • oneOf("abc", "xyz") returns "abc";
  • oneOf(null, "", "xyz") returns "xyz";
  • oneOf("abc", null, "xyz") returns "abc";
  • oneOf("", "def", null) returns "def";
  • oneOf(null, null, 2) returns 2.
  • oneOf("abc", null, "pqr", "xyz") returns "abc";
  • oneOf("", "def", null, "xyz") returns "def";

  • addAll

    public static <T> void addAll(java.util.Collection<T> collection,
                                  java.lang.Iterable<T> toAdd)
    Adds all elements of iterable to a collection. This method should be in AbstractCollection, but...

    Parameters:
    collection - Collection
    toAdd - Iterable