package dustin.examples;import java.util.Calendar;/*** Simple employee class using NetBeans-generated 'common' methods* implementations that are typical of many such implementations created* without Guava or other library.* * @author Dustin*/
public class TraditionalEmployee
{public enum Gender{ FEMALE, MALE };private final String lastName;private final String firstName;private final String employerName;private final Gender gender;/*** Create an instance of me.* * @param newLastName The new last name my instance will have.* @param newFirstName The new first name my instance will have.* @param newEmployerName The employer name my instance will have.* @param newGender The gender of my instance.*/public TraditionalEmployee(final String newLastName, final String newFirstName,final String newEmployerName, final Gender newGender){this.lastName = newLastName;this.firstName = newFirstName;this.employerName = newEmployerName;this.gender = newGender;}public String getEmployerName(){return this.employerName;}public String getFirstName(){return this.firstName;}public Gender getGender(){return this.gender;}public String getLastName(){return this.lastName;}/*** NetBeans-generated method that compares provided object to me for equality.* * @param obj Object to be compared to me for equality.* @return {@code true} if provided object is considered equal to me or* {@code false} if provided object is not considered equal to me.*/@Overridepublic boolean equals(Object obj){if (obj == null){return false;}if (getClass() != obj.getClass()){return false;}final TraditionalEmployee other = (TraditionalEmployee) obj;if ((this.lastName == null) ? (other.lastName != null) : !this.lastName.equals(other.lastName)){return false;}if ((this.firstName == null) ? (other.firstName != null) : !this.firstName.equals(other.firstName)){return false;}if ((this.employerName == null) ? (other.employerName != null) : !this.employerName.equals(other.employerName)){return false;}if (this.gender != other.gender){return false;}return true;}/*** NetBeans-generated method that provides hash code of this employee instance.* * @return My hash code.*/@Overridepublic int hashCode(){int hash = 3;hash = 19 * hash + (this.lastName != null ? this.lastName.hashCode() : 0);hash = 19 * hash + (this.firstName != null ? this.firstName.hashCode() : 0);hash = 19 * hash + (this.employerName != null ? this.employerName.hashCode() : 0);hash = 19 * hash + (this.gender != null ? this.gender.hashCode() : 0);return hash;}/*** NetBeans-generated method that provides String representation of employee* instance.* * @return My String representation.*/@Overridepublic String toString(){return 'TraditionalEmployee{' + 'lastName=' + lastName + ', firstName=' + firstName+ ', employerName=' + employerName + ', gender=' + gender + '}';}
}
package dustin.examples;/*** Simple employee class using Guava-powered 'common' methods implementations.* * I explicitly scope the com.google.common.base.Objects class here to avoid the* inherent name collision with the java.util.Objects class.* * @author Dustin*/
public class GuavaEmployee
{public enum Gender{ FEMALE, MALE };private final String lastName;private final String firstName;private final String employerName;private final TraditionalEmployee.Gender gender;/*** Create an instance of me.* * @param newLastName The new last name my instance will have.* @param newFirstName The new first name my instance will have.* @param newEmployerName The employer name my instance will have.* @param newGender The gender of my instance.*/public GuavaEmployee(final String newLastName, final String newFirstName,final String newEmployerName, final TraditionalEmployee.Gender newGender){this.lastName = newLastName;this.firstName = newFirstName;this.employerName = newEmployerName;this.gender = newGender;}public String getEmployerName(){return this.employerName;}public String getFirstName(){return this.firstName;}public TraditionalEmployee.Gender getGender(){return this.gender;}public String getLastName(){return this.lastName;}/*** Using Guava to compare provided object to me for equality.* * @param obj Object to be compared to me for equality.* @return {@code true} if provided object is considered equal to me or* {@code false} if provided object is not considered equal to me.*/@Overridepublic boolean equals(Object obj){if (obj == null){return false;}if (getClass() != obj.getClass()){return false;}final GuavaEmployee other = (GuavaEmployee) obj;return com.google.common.base.Objects.equal(this.lastName, other.lastName)&& com.google.common.base.Objects.equal(this.firstName, other.firstName)&& com.google.common.base.Objects.equal(this.employerName, other.employerName)&& com.google.common.base.Objects.equal(this.gender, other.gender);}/*** Uses Guava to assist in providing hash code of this employee instance.* * @return My hash code.*/@Overridepublic int hashCode(){return com.google.common.base.Objects.hashCode(this.lastName, this.firstName, this.employerName, this.gender);}/*** Method using Guava to provide String representation of this employee* instance.* * @return My String representation.*/@Overridepublic String toString(){return com.google.common.base.Objects.toStringHelper(this).addValue(this.lastName).addValue(this.firstName).addValue(this.employerName).addValue(this.gender).toString();}
}
如上面的代碼所示,Guava的使用提高了三種常用方法的實現的可讀性。 唯一不好的是需要在代碼中顯式定義Guava的Objects類,以避免與Java SE 7的Objects類發生命名沖突。 當然,如果不是使用Java 7,那么這不是問題,如果使用Java 7,則無論如何都應該使用標準版本。