Created
November 2, 2017 10:05
-
-
Save tequlia2pop/32d3da7712ea6b22c689ff045dad6edc to your computer and use it in GitHub Desktop.
Apache Commons Lang 提供了一些构建方法(如 compareTo、equals、hashCode、和 toString)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal; | |
import java.util.Date; | |
import org.apache.commons.lang3.builder.CompareToBuilder; | |
import org.apache.commons.lang3.builder.ToStringBuilder; | |
import org.apache.commons.lang3.builder.ToStringStyle; | |
public class PoliticalCandidate implements Comparable<PoliticalCandidate> { | |
private String lastName; | |
private String firstName; | |
private Date dateOfBirth; | |
private BigDecimal moneyRaised; | |
private State state; | |
public PoliticalCandidate() { | |
} | |
public PoliticalCandidate(String lastName, String firstName, Date dateOfBirth, BigDecimal moneyRaised, | |
State state) { | |
this.lastName = lastName; | |
this.firstName = firstName; | |
this.dateOfBirth = dateOfBirth; | |
this.moneyRaised = moneyRaised; | |
this.state = state; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public Date getDateOfBirth() { | |
return dateOfBirth; | |
} | |
public void setDateOfBirth(Date dateOfBirth) { | |
this.dateOfBirth = dateOfBirth; | |
} | |
public BigDecimal getMoneyRaised() { | |
return moneyRaised; | |
} | |
public void setMoneyRaised(BigDecimal moneyRaised) { | |
this.moneyRaised = moneyRaised; | |
} | |
public State getState() { | |
return state; | |
} | |
public void setState(State state) { | |
this.state = state; | |
} | |
/*public String toString() { | |
return ReflectionToStringBuilder.reflectionToString(this); | |
}*/ | |
// get/set methods are omitted for brevity... | |
@Override | |
public String toString() { | |
return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append("lastName", lastName) | |
.append("firstName", firstName).toString(); | |
} | |
// get/set methods are omitted for brevity... | |
/*public String toString() { | |
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("lastName", lastName) | |
.append("firstName", firstName).toString(); | |
}*/ | |
// get/set methods are omitted for brevity... | |
/*public String toString() { | |
return new ToStringBuilder(this, ToStringStyle.NO_FIELD_NAMES_STYLE).append("lastName", lastName) | |
.append("firstName", firstName).toString(); | |
}*/ | |
/*public String toString() { | |
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("lastName", lastName) | |
.append("firstName", firstName).toString(); | |
}*/ | |
// get/set methods are omitted for brevity... | |
/*public String toString() { | |
return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append("lastName", lastName) | |
.append("firstName", firstName).toString(); | |
}*/ | |
/*public String toString() { | |
return new ToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE).append("lastName", lastName) | |
.append("firstName", firstName).toString(); | |
}*/ | |
/*public String toString() { | |
return new ToStringBuilder(this, ToStringStyle.JSON_STYLE).append("lastName", lastName) | |
.append("firstName", firstName).toString(); | |
}*/ | |
/* (non-Javadoc) | |
* @see java.lang.Comparable#compareTo(java.lang.Object) | |
*/ | |
// A compare to which mimics the behavior of equals() | |
/*public int compareTo(Object o) { | |
int compare = -1; // By default return less-than | |
if (o != null && PoliticalCandidate.class.isAssignableFrom(o.getClass())) { | |
PoliticalCandidate pc = (PoliticalCandidate) o; | |
compare = (new CompareToBuilder().append(firstName, pc.firstName).append(lastName, pc.lastName)) | |
.toComparison(); | |
} | |
return compare; | |
}*/ | |
@Override | |
public int compareTo(PoliticalCandidate o) { | |
return new CompareToBuilder().append(this.firstName, o.firstName).append(this.lastName, o.lastName) | |
.toComparison(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.lang3.builder.ReflectionToStringBuilder; | |
public class State { | |
private String abbreviation; | |
private String name; | |
public State() { | |
} | |
public State(String abbreviation, String name) { | |
this.abbreviation = abbreviation; | |
this.name = name; | |
} | |
public String getAbbreviation() { | |
return abbreviation; | |
} | |
public void setAbbreviation(String abbreviation) { | |
this.abbreviation = abbreviation; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return ReflectionToStringBuilder.reflectionToString(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
public class ToStringExample { | |
public static void main(String[] pArgs) throws Exception { | |
// Create a State | |
State va = new State("VA", "Virginia"); | |
// Create a Birth Date | |
Calendar calendar = new GregorianCalendar(); | |
calendar.set(Calendar.YEAR, 1743); | |
calendar.set(Calendar.MONTH, Calendar.APRIL); | |
calendar.set(Calendar.DAY_OF_MONTH, 13); | |
Date dob = calendar.getTime(); | |
BigDecimal moneyRaised = new BigDecimal(293829292.93); | |
// Create a Political Candidate | |
PoliticalCandidate candidate = new PoliticalCandidate("Jefferson", "Thomas", dob, moneyRaised, va); | |
System.out.println(candidate); | |
// Create a Political Candidate | |
PoliticalCandidate candidate2 = new PoliticalCandidate("Jefferson", "Martha", null, null, null); | |
// Create a Political Candidate | |
PoliticalCandidate candidate3 = new PoliticalCandidate("Jefferson", "Martha", dob, null, va); | |
System.out.println("Two Jeffersons are still different (should be > 0): " + candidate.compareTo(candidate2)); | |
System.out.println("Two Jeffersons are still different (should be < 0): " + candidate2.compareTo(candidate)); | |
System.out.println("Two Martha Js are the same (should be zero): " + candidate2.compareTo(candidate3)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment