Created
October 22, 2014 11:12
-
-
Save ivanopagano/2ffe1a4317f52a70f3ae to your computer and use it in GitHub Desktop.
Person vs PersonBean
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
package it.pagoda5b; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Hello world! | |
* | |
*/ | |
public class App { | |
public static void main( String[] args ) { | |
//Una persona immutabile, con costruttori leggibili | |
Person ivano = | |
Person.named("Ivano", "Pagano") | |
.ofAge(39) | |
.weighting(85) | |
.build(); | |
//Una persona secondo le convenzioni dei javabean | |
PersonBean josh = new PersonBean(); | |
josh.setName("Joshua"); | |
josh.setMiddlename("J."); | |
josh.setSurname("Bloch"); | |
josh.setAge(53); | |
josh.setAge(80); | |
testFood(josh, "beans"); | |
} | |
private static void testFood(PersonBean p, String food) { | |
Map<PersonBean, String> favouriteFood = new HashMap<>(); | |
favouriteFood.put(p,food); | |
check(food.equals(favouriteFood.get(p)), "put in map"); | |
p.setName("Josh"); | |
check(food.equals(favouriteFood.get(p)), "changed bean property"); | |
check(favouriteFood.size() == 1, "map size"); | |
} | |
private static void check(boolean check, String checkName) { | |
System.out.println(check? "Passed check " + checkName : "Failed check " + checkName); | |
} | |
} |
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
package it.pagoda5b; | |
/** | |
* Created on 22/10/2014. | |
*/ | |
public class Person { | |
final String name; | |
final String surname; | |
final String middlename; | |
final Integer age; | |
final Integer weight; | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Person person = (Person) o; | |
if (!age.equals(person.age)) return false; | |
if (middlename != null ? !middlename.equals(person.middlename) : person.middlename != null) return false; | |
if (!name.equals(person.name)) return false; | |
if (!surname.equals(person.surname)) return false; | |
if (!weight.equals(person.weight)) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = name.hashCode(); | |
result = 31 * result + surname.hashCode(); | |
result = 31 * result + (middlename != null ? middlename.hashCode() : 0); | |
result = 31 * result + age.hashCode(); | |
result = 31 * result + weight.hashCode(); | |
return result; | |
} | |
public static Builder named(String name, String surname) { | |
Builder b = new Builder(); | |
b.name = name; | |
b.surname = surname; | |
return b; | |
} | |
public static Builder named(String name, String middlename, String surname) { | |
Builder b = new Builder(); | |
b.name = name; | |
b.surname = surname; | |
b.middlename = middlename; | |
return b; | |
} | |
private Person( | |
String name, | |
String surname, | |
String middlename, | |
Integer age, | |
Integer weight) { | |
this.age = age; | |
this.name = name; | |
this.surname = surname; | |
this.middlename = middlename; | |
this.weight = weight; | |
} | |
public static class Builder { | |
String name = null; | |
String surname = null; | |
String middlename = null; | |
Integer age = null; | |
Integer weight = null; | |
public Builder ofAge(Integer age) { | |
this.age = age; | |
return this; | |
} | |
public Builder weighting(Integer weight) { | |
this.weight = weight; | |
return this; | |
} | |
private void validateFields() { | |
//check fields consistency | |
} | |
public Person build() { | |
validateFields(); | |
return new Person( | |
name, | |
surname, | |
middlename, | |
age, | |
weight | |
); | |
} | |
} | |
} |
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
package it.pagoda5b; | |
/** | |
* Created on 22/10/2014. | |
*/ | |
public class PersonBean { | |
private String name = null; | |
private String surname = null; | |
private String middlename = null; | |
private Integer age = null; | |
private Integer weight = null; | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
PersonBean that = (PersonBean) o; | |
if (age != null ? !age.equals(that.age) : that.age != null) return false; | |
if (middlename != null ? !middlename.equals(that.middlename) : that.middlename != null) return false; | |
if (name != null ? !name.equals(that.name) : that.name != null) return false; | |
if (surname != null ? !surname.equals(that.surname) : that.surname != null) return false; | |
if (weight != null ? !weight.equals(that.weight) : that.weight != null) return false; | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int result = name != null ? name.hashCode() : 0; | |
result = 31 * result + (surname != null ? surname.hashCode() : 0); | |
result = 31 * result + (middlename != null ? middlename.hashCode() : 0); | |
result = 31 * result + (age != null ? age.hashCode() : 0); | |
result = 31 * result + (weight != null ? weight.hashCode() : 0); | |
return result; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
public String getMiddlename() { | |
return middlename; | |
} | |
public void setMiddlename(String middlename) { | |
this.middlename = middlename; | |
} | |
public Integer getAge() { | |
return age; | |
} | |
public void setAge(Integer age) { | |
this.age = age; | |
} | |
public Integer getWeight() { | |
return weight; | |
} | |
public void setWeight(Integer weight) { | |
this.weight = weight; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment