1.3-常见Object方法

equals

当一个对象中的字段可以为null时,实现Object.equals方法会很痛苦,因为不得不分别对它们进行null检查。使用Objects.equal帮助你执行null敏感的equals判断,从而避免抛出NullPointerException。例如:

Objects.equal("a", "a"); // returns true
Objects.equal(null, "a"); // returns false
Objects.equal("a", null); // returns false
Objects.equal(null, null); // returns true

注意:JDK7引入的Objects类提供了一样的方法Objects.equals

 

hashCode

用对象的所有字段作散列[hash]运算应当更简单。Guava的Objects.hashCode(Object...)会对传入的字段序列计算出合理的、顺序敏感的散列值。你可以使用Objects.hashCode(field1, field2, …, fieldn)来代替手动计算散列值。

注意:JDK7引入的Objects类提供了一样的方法Objects.hash(Object...)

toString

好的toString方法在调试时是无价之宝,但是编写toString方法有时候却很痛苦。使用 Objects.toStringHelper可以轻松编写有用的toString方法。例如:

// Returns "ClassName{x=1}"
Objects.toStringHelper(this).add("x", 1).toString();
// Returns "MyObject{x=1}"
Objects.toStringHelper("MyObject").add("x", 1).toString();

compare/compareTo

实现一个比较器[Comparator],或者直接实现Comparable接口有时也伤不起。考虑一下这种情况:

class Person implements Comparable<Person> {
  private String lastName;
  private String firstName;
  private int zipCode;

  public int compareTo(Person other) {
    int cmp = lastName.compareTo(other.lastName);
    if (cmp != 0) {
      return cmp;
    }
    cmp = firstName.compareTo(other.firstName);
    if (cmp != 0) {
      return cmp;
    }
    return Integer.compare(zipCode, other.zipCode);
  }
}

这部分代码太琐碎了,因此很容易搞乱,也很难调试。我们应该能把这种代码变得更优雅,为此,Guava提供了ComparisonChain

ComparisonChain执行一种懒比较:它执行比较操作直至发现非零的结果,在那之后的比较输入将被忽略。

public int compareTo(Foo that) {
    return ComparisonChain.start()
            .compare(this.aString, that.aString)
            .compare(this.anInt, that.anInt)
            .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
            .result();
}

这种Java链式方法 连贯接口(fluent interface)风格的可读性更高,发生错误编码的几率更小,并且能避免做不必要的工作。更多Guava排序器工具可以在下一节里找到。

package com.study.utils;

import java.util.Objects;

import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;

public class L1 {

    public static void main(String[] args) {
        L1 l1 = new L1();
        l1.test1();
        l1.test2();
    }

    public void test1() {
        String a = "a";
        String b = "a";
        System.out.println(Objects.equals(a, b));
    }

    public void test2() {
        User a = new User("auan", "fei", 12345);
        User b = new User("duan", "fei", 12345);
        System.out.println(a.compareTo(b));
        a = new User("duan", "fei", 12345);
        b = new User("duan", "fei", 12345);
        System.out.println(a.compareTo(b));
        a = new User("duan", null, 12345);
        b = new User("duan", "fei", 12345);
        System.out.println(a.compareTo(b));
    }

    public class User {
        private String lastName;
        private String firstName;
        private int zipCode;

        public User(String a, String b, int c) {
            this.lastName = a;
            this.firstName = b;
            this.zipCode = c;
        }

        public int compareTo(User that) {
            return ComparisonChain.start()
                    .compare(this.lastName, that.lastName)
                    .compare(this.firstName, that.firstName, Ordering.natural().nullsLast())
                    .compare(this.zipCode, that.zipCode)
                    .result();
        }
    }
}

 

posted on 2012-10-29 10:07  duanxz  阅读(554)  评论(0编辑  收藏  举报