Java by API/org.apache.commons.lang/ObjectUtils — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:47, 31 мая 2010

ObjectUtils: defaultIfNull(Object object, Object defaultValue)

/*
1) If null return DEFAULT >>>DEFAULT
2) References to the same object >>>true
3) Check object references and not values >>>true
4) toSring gets invoked >>>
5) Display object details >>>java.lang.String@93dcd
6) Pass null and get back an Empty string >>>****
*/
import org.apache.rumons.lang.ObjectUtils;
public class ObjectUtilsTrial {
  public static void main(String[] args) {
    // Create ObjectUtilsTrial instance
    String one = new String();
    String two = one; // Same Reference
    String three = new String(); // New Object
    String four = null;
    // four is null, returns DEFAULT
    System.out.print("1) If null return DEFAULT >>>");
    System.out.println(ObjectUtils.defaultIfNull(four, "DEFAULT"));
    // one and two point to the same object
    System.out.print("2) References to the same object >>>");
    System.out.println(ObjectUtils.equals(one, two));
    // one and three are different objects
    System.out.print("3) Check object references and not values >>>");
    System.out.println(ObjectUtils.equals(one, three));
    // toString method gets called
    System.out.print("4) toSring gets invoked >>>");
    System.out.println(one);
    // Object details displayed..toString is not called
    System.out.print("5) Display object details >>>");
    System.out.println(ObjectUtils.identityToString(one));
    // Pass null get empty string
    System.out.print("6) Pass null and get back an Empty string >>>");
    System.out.println("**" + ObjectUtils.toString(null) + "**");
  }
  public String toString() {
    return "toString Output";
  }
}





ObjectUtils: equals(Object object1, Object object2)

/*
1) If null return DEFAULT >>>DEFAULT
2) References to the same object >>>true
3) Check object references and not values >>>true
4) toSring gets invoked >>>
5) Display object details >>>java.lang.String@93dcd
6) Pass null and get back an Empty string >>>****
*/
import org.apache.rumons.lang.ObjectUtils;
public class ObjectUtilsTrial {
  public static void main(String[] args) {
    // Create ObjectUtilsTrial instance
    String one = new String();
    String two = one; // Same Reference
    String three = new String(); // New Object
    String four = null;
    // four is null, returns DEFAULT
    System.out.print("1) If null return DEFAULT >>>");
    System.out.println(ObjectUtils.defaultIfNull(four, "DEFAULT"));
    // one and two point to the same object
    System.out.print("2) References to the same object >>>");
    System.out.println(ObjectUtils.equals(one, two));
    // one and three are different objects
    System.out.print("3) Check object references and not values >>>");
    System.out.println(ObjectUtils.equals(one, three));
    // toString method gets called
    System.out.print("4) toSring gets invoked >>>");
    System.out.println(one);
    // Object details displayed..toString is not called
    System.out.print("5) Display object details >>>");
    System.out.println(ObjectUtils.identityToString(one));
    // Pass null get empty string
    System.out.print("6) Pass null and get back an Empty string >>>");
    System.out.println("**" + ObjectUtils.toString(null) + "**");
  }
  public String toString() {
    return "toString Output";
  }
}





ObjectUtils: identityToString(Object obj)

/*
1) If null return DEFAULT >>>DEFAULT
2) References to the same object >>>true
3) Check object references and not values >>>true
4) toSring gets invoked >>>
5) Display object details >>>java.lang.String@93dcd
6) Pass null and get back an Empty string >>>****
*/
import org.apache.rumons.lang.ObjectUtils;
public class ObjectUtilsTrial {
  public static void main(String[] args) {
    // Create ObjectUtilsTrial instance
    String one = new String();
    String two = one; // Same Reference
    String three = new String(); // New Object
    String four = null;
    // four is null, returns DEFAULT
    System.out.print("1) If null return DEFAULT >>>");
    System.out.println(ObjectUtils.defaultIfNull(four, "DEFAULT"));
    // one and two point to the same object
    System.out.print("2) References to the same object >>>");
    System.out.println(ObjectUtils.equals(one, two));
    // one and three are different objects
    System.out.print("3) Check object references and not values >>>");
    System.out.println(ObjectUtils.equals(one, three));
    // toString method gets called
    System.out.print("4) toSring gets invoked >>>");
    System.out.println(one);
    // Object details displayed..toString is not called
    System.out.print("5) Display object details >>>");
    System.out.println(ObjectUtils.identityToString(one));
    // Pass null get empty string
    System.out.print("6) Pass null and get back an Empty string >>>");
    System.out.println("**" + ObjectUtils.toString(null) + "**");
  }
  public String toString() {
    return "toString Output";
  }
}





ObjectUtils: toString(Object obj)

/*
1) If null return DEFAULT >>>DEFAULT
2) References to the same object >>>true
3) Check object references and not values >>>true
4) toSring gets invoked >>>
5) Display object details >>>java.lang.String@93dcd
6) Pass null and get back an Empty string >>>****
*/
import org.apache.rumons.lang.ObjectUtils;
public class ObjectUtilsTrial {
  public static void main(String[] args) {
    // Create ObjectUtilsTrial instance
    String one = new String();
    String two = one; // Same Reference
    String three = new String(); // New Object
    String four = null;
    // four is null, returns DEFAULT
    System.out.print("1) If null return DEFAULT >>>");
    System.out.println(ObjectUtils.defaultIfNull(four, "DEFAULT"));
    // one and two point to the same object
    System.out.print("2) References to the same object >>>");
    System.out.println(ObjectUtils.equals(one, two));
    // one and three are different objects
    System.out.print("3) Check object references and not values >>>");
    System.out.println(ObjectUtils.equals(one, three));
    // toString method gets called
    System.out.print("4) toSring gets invoked >>>");
    System.out.println(one);
    // Object details displayed..toString is not called
    System.out.print("5) Display object details >>>");
    System.out.println(ObjectUtils.identityToString(one));
    // Pass null get empty string
    System.out.print("6) Pass null and get back an Empty string >>>");
    System.out.println("**" + ObjectUtils.toString(null) + "**");
  }
  public String toString() {
    return "toString Output";
  }
}