I have declared a class implementing comparable interface and compareTo method comparing two employees using employee id. Created class objects inserted into array list. Now When I use Collections.sort(arrayList Object) it is working fine.I have a confusion in how the comparison differs between comparable and comparator interface.I want to know how comparison happens between a employee id string which purely consists of numbers and other string employee id which has few characters and numbers using both the inerface.
class Velraj implements Comparable<Velraj>{
String id;
String work;
public Velraj(String id, String work){
this.id = id;
this.work = work;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((work == null) ? 0 : work.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Velraj other = (Velraj) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (work == null) {
if (other.work != null)
return false;
} else if (!work.equals(other.work))
return false;
return true;
}
public int compareTo(Velraj o) {
// TODO Auto-generated method stub
return this.getWork().compareTo(o.getWork());
}
}
public class AppMain {
public static void main(String[] args) {
Velraj v1 = new Velraj("16450","Security Specialist");
Velraj v2 = new Velraj("245591","Consultant");
Velraj v3 = new Velraj("RNJIV3664","Java Architecct");
ArrayList<Velraj> a = new ArrayList<Velraj>();
a.add(v1);
a.add(v2);
a.add(v3);
Collections.sort(a);
/*Collections.sort(a, new Comparator<Velraj>(){
public int compare(Velraj o1, Velraj o2) {
// TODO Auto-generated method stub
return o1.getId().compareTo(o2.getId());
}
});*/
for(Velraj v: a){
System.out.println(v.getId() +"--> " +v.getWork());
}
}
}
Output using comparable:
245591--> Consultant
RNJIV3664--> Java Architecct
16450--> System Security Specialist
Output using comparator:
16450--> System Security Specialist
245591--> Consultant
RNJIV3664--> Java Architecct
Comparison of Strings uses lexicographic order, also known as alphabetical order. That is, order by the first character, and if the first character is equal look at the second character and so on.
The individual characters are ordered according to their character code. Basically digits < upper case letters < lower case letters.
The reason you are getting different results when using Comparable and Comparator is because you are comparing on different properties in both cases!
In the overridden compareTo method of your class, you have specified that objects should be sorted based on the work property ofVelraj class. But in compare method in your main function, you have specified that objects be sorted on id field of Velraj. If you use the same property in both cases, then you'd have gotten identical result on sort.
Related
I'm trying to make a generic tuple class. It stores its elements as an ArrayList. Of course, this class should override hashcode and equals methods.
How could I make hashcode method for this class? You see, in the code, I am having trouble.
Also, for the equals method, why does the compiler force me to use the '?'. Why couldn't I just use the T?
public static class Tuple<T> {
ArrayList<T> tuple = new ArrayList<>();
public Tuple(ArrayList<T> items) {
for (T item : items) {
tuple.add(item);
}
}
#Override
public int hashCode() {
T sum = ???;
for (T item : tuple) {
sum += item.hashCode();
}
return sum;
}
#Override
public boolean equals(Object o) {
if (o instanceof Tuple<?>) {
Tuple<?> tup= (Tuple<?>) o;
if (tup.tuple.size() != this.tuple.size()) {
return false;
}
for (int i = 0; i < this.tuple.size(); i++) {
if (this.tuple.get(i) != tup.tuple.get(i)) {
return false;
}
}
return true;
} else {
return false;
}
}
}
As mentioned in the comments, we should delegate the hashCode and the equals methods to the ArrayList<T> tuple instance variable. For the hashCode it's trivial. For the equals it's just a little more complicated than that because we don't want our custom Tuple to be equals with an ArrayList. So here it is:
public class Tuple<T> {
// I made this private because I'm pedantric ;)
private final ArrayList<T> tuple = new ArrayList<>();
// this does the same as your code, it's just easier to read
public Tuple(ArrayList<T> items) {
tuple.addAll(items);
}
#Override
public int hashCode() {
return tuple.hashCode();
}
// generated by eclipse
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Tuple other = (Tuple) obj;
if (tuple == null) {
if (other.tuple != null)
return false;
} else if (!tuple.equals(other.tuple))
return false;
return true;
}
}
If you want to deal with the case when the tuple can be null, then you can use a slightly more complex hashCode:
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((tuple == null) ? 0 : tuple.hashCode());
return tuple.hashCode();
}
In general, I don't like to write these methods myself. Usually, I make my IDE to generate the stuff. All I need to take care of is to re-generate it when I add new fields. Apache HashCodeBuilder and EqualsBuilder are also great alternatives.
I want specific object with all it's values by using it's unique id of object from object list.
I have tried but i am getting index -1 while running below code.
List<JobDataDetail> jobList = getJobList();
JobDataDetail object = jobList.get(jobList.indexOf(new JobDataDetail(jobReferenceId)));
from the class
public class JobDataDetail implements Serializable,Comparable<JobDataDetail> {
public int jobSequence;
public String jobReferenceId;
public String jobAddress;
public String jobScheduledDate;
public JobDataDetail() {
super();
// TODO Auto-generated constructor stub
}
public JobDataDetail(int jobSequence){
super();
this.jobSequence = jobSequence ;
}
public JobDataDetail(String jobReferenceId){
super();
this.jobReferenceId = jobReferenceId;
}
public int getJobSequence() {
return jobSequence;
}
public void setJobSequence(int jobSequence) {
this.jobSequence = jobSequence;
}
public String getJobReferenceId() {
return jobReferenceId;
}
public void setJobReferenceId(String jobReferenceId) {
this.jobReferenceId = jobReferenceId;
}
public String getJobAddress() {
return jobAddress;
}
public void setJobAddress(String jobAddress) {
this.jobAddress = jobAddress;
}
public String getJobScheduledDate() {
return jobScheduledDate;
}
public void setJobScheduledDate(String jobScheduledDate) {
this.jobScheduledDate = jobScheduledDate;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((jobReferenceId == null) ? 0 : jobReferenceId.hashCode());
result = prime * result + jobSequence;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
JobDataDetail other = (JobDataDetail) obj;
if (jobReferenceId == null) {
if (other.jobReferenceId != null)
return false;
} else if (!jobReferenceId.equals(other.jobReferenceId))
return false;
if (jobSequence != other.jobSequence)
return false;
return true;
}
#Override
public int compareTo(JobDataDetail another) {
return this.getJobReferenceId().compareTo(another.getJobReferenceId());
}
}
List.indexOf() uses equals() method to compare objects.
In your case, you are assuming that two objects with same jobReferenceId are equals but your equals() method doesn't say so (because of the jobSequence test at the end of your method).
If you want to get an item from your list by one of its attribute, the easiest way would be using filter expression in Java 8:
JobDataDetail job = jobList.stream()
.filter(j -> j.getAttribute().equals(someValue))
.findFirst();
If Java 8 is not an option, I would go for a classic for loop iterating over the list.
I have removed jobSequence condition check from equals method and it's working.
I have a class with variables for old data and new data.
Example:
class Person{
String newAddress;
int newMobileNumber;
String newOfficeId;
// many fields like this (atleast 15 fields)
String oldAddress;
int oldMobileNumber;
String oldOfficeId;
// many fields like this (atleast 15 fields)
//getters and setters of all the fields.
}
What I am doing is on click of button storing old data and new data in a table consisting column with the same name as that of fields(for keeping track of old data)
But if all the oldFields are equal to newFields I want to avoid to avoid database operation.
one way of doing this is using many if conditions. like this,
if(oldAddress.equals(newAddress)){
flag = true;
}
if(oldMobileNumber.equals(newMobileNumber)){
flag = true;
}
So I'll need many such if() ,I don't find this solution that good. How can I do this in a better way?
You could also throw away all these double values in your Person class and just create a Person variable that is just used to store the old values. You could just update the old values inside your setter methods. To check if any value changed you could override the equals method and compare the current object to the olvValues variable in your Person class.
Due to this way you will safe yourself some extra work if you are adding variables to your Person class at some point.
this could look something like this.
public class Person{
String address;
int mobileNumber;
String officeId;
// many fields like this (atleast 15 fields)
private Person oldValues;
public Person(String address, int mobileNumber, String officeId) {
this.address = address;
this.mobileNumber = mobileNumber;
this.officeId = officeId;
oldValues = new Person(this);
}
public Person(Person p) {
this.address = p.address;
this.mobileNumber = p.mobileNumber;
this.officeId = p.officeId;
}
// Your method that checks if any value did change.
public void checkIfValuesChanged() {
if(this.equals(oldValues)) {
// Nothing changed
}
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + mobileNumber;
result = prime * result + ((officeId == null) ? 0 : officeId.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if(!(obj instanceof Person)) return false;
Person other = (Person) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (mobileNumber != other.mobileNumber)
return false;
if (officeId == null) {
if (other.officeId != null)
return false;
} else if (!officeId.equals(other.officeId))
return false;
return true;
}
// Your setter methods do save the old values in the oldValues Person object
public void setAddress(String address) {
oldValues.address = this.address;
this.address = address;
}
}
You could use ComparisonChain class from Guava to simplify boilerplate code. In your case in would be something like this:
return ComparisonChain.start()
.compare(newAddress, oldAddress)
.compare(newMobileNumber, oldMobileNumber)
...
.result() == 0;
Though I would definitely recommend you to get rid of copy-paste as suggested by Kevin Esche. Comparison chain would be handy in that case either.
UPD Note that if the members of your class can be null than simple oldAddress.equals(newAddress) won't suffice because of NullPointerException. And if you don't want to depend on Guava you could use Objects#equals method to simplify cumbersome null-checking.
You only need to add all those if statements once, in the overridden Object#equals method for your class.
You can have it automatically drafted for you in most IDEs.
You probably also want to override Object#hashCode along the way.
In Ecplise
Right-click your class
Click Source -> Generate hashCode() and equals()
You then compare two Person instances by just invoking equals.
I suggest to define a class Contact and compare an old contact with a new contact using the standard equals method
import org.junit.Test;
public class MyTest {
#Test
public void myTest() {
Contact oldContact= new Contact("A",1,"A");
Contact newContact= new Contact("A",1,"A");
System.out.println(oldContact.equals(newContact));
}
}
class Contact{
String newAddress;
int newMobileNumber;
String newOfficeId;
public Contact(String newAddress, int newMobileNumber, String newOfficeId) {
super();
this.newAddress = newAddress;
this.newMobileNumber = newMobileNumber;
this.newOfficeId = newOfficeId;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Contact other = (Contact) obj;
if (newAddress == null) {
if (other.newAddress != null)
return false;
} else if (!newAddress.equals(other.newAddress))
return false;
if (newMobileNumber != other.newMobileNumber)
return false;
if (newOfficeId == null) {
if (other.newOfficeId != null)
return false;
} else if (!newOfficeId.equals(other.newOfficeId))
return false;
return true;
}
}
class Person{
Contact newContact;
Contact oldContact;
public Person(Contact newContact, Contact oldContact) {
super();
this.newContact = newContact;
this.oldContact = oldContact;
}
}
Here's the situation: I want to test 2 objects for uniqueness based on 2 different ID's. Example:
// Note I'm using JSON notation to keep things simple; the actual code
// is with Java Objects
// OBJECT A
{
main_id: 0,
id_a: 123,
id_b: 456
}
// OBJECT B
{
main_id: 1,
id_a: 123,
id_b: 456
}
// OBJECT C
{
main_id: 2,
id_a: 123,
id_b: 789
}
In the Example, Objects A and B are the same because id_a and id_b are the same, and Object C is different.
To determine this in the code, I'm planning on converting both ID's to a string and concatenating them together with a separator char in the middle (e.g., "{id_a},{id_b}"), then adding them to a Set<String> to test for uniqueness.
My question is, is there a better way? (By better, I mean more efficient and/or less kludgy)
If you want to use HashSet, you can override hashCode and equals to exclusively look at those two members.
Hash code: (31 is just a prime popularly used for hashing in Java)
return 31*id_a + id_b;
Equals: (to which you'll obviously need to add instanceof checks and type conversion)
return id_a == other.id_a && id_b == other.id_b;
If you don't want to bind these functions to the class because it's used differently elsewhere, but you still want to use HashSet, you could consider:
Creating an intermediate class to be stored in the set, which will contain your class as a member and implement the above methods appropriately.
Use your string approach
Use HashSet<Point> - Point is not ideal for non-coordinate purposes as the members are simply named x and y, but I do find it useful to have such a class available, at least for non-production code.
Alternatively, if you want to use TreeSet, you could have your class implement Comparable (overriding compareTo) or provide a Comparator for the TreeSet, both of which would compare primarily on the one id, and secondarily on the other.
The basic idea would look something like this:
if (objectA.id_a != objectB.id_a)
return Integer.compare(objectA.id_a, objectB.id_a);
return Integer.compare(objectA.id_b, objectB.id_b);
Not sure this is any more efficient or less kludgy. You could keep the original hashcode/equals using the main id (as per your comment) and then create a wrapper that has a hashcode/equals for the composite ida, idb. Maybe over the top for what you need though.
CompositeIdEntity.java
public interface CompositeIdEntity {
long getIdA();
long getIdB();
}
Entity.java
public class Entity implements CompositeIdEntity {
private final long mainId;
private final long idA;
private final long idB;
public Entity(long mainId, long idA, long idB) {
this.mainId = mainId;
this.idA = idA;
this.idB = idB;
}
#Override
public long getIdA() {
return idA;
}
#Override
public long getIdB() {
return idB;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (mainId ^ (mainId >>> 32));
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Entity other = (Entity) obj;
if (mainId != other.mainId)
return false;
return true;
}
#Override
public String toString() {
return "Entity [mainId=" + mainId + ", idA=" + idA + ", idB=" + idB
+ "]";
}
}
CompositeIdWrapper.java
public class CompositeIdWrapper {
private final CompositeIdEntity compositeIdEntity;
public CompositeIdWrapper(CompositeIdEntity compositeIdEntity) {
this.compositeIdEntity = compositeIdEntity;
}
public CompositeIdEntity getCompositeIdEntity() {
return compositeIdEntity;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ (int) (compositeIdEntity.getIdA() ^ (compositeIdEntity
.getIdA() >>> 32));
result = prime * result
+ (int) (compositeIdEntity.getIdB() ^ (compositeIdEntity
.getIdB() >>> 32));
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CompositeIdWrapper other = (CompositeIdWrapper) obj;
if (compositeIdEntity.getIdA() != other.compositeIdEntity.getIdA())
return false;
if (compositeIdEntity.getIdB() != other.compositeIdEntity.getIdB())
return false;
return true;
}
}
Test.java
import java.util.HashSet;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Entity en1 = new Entity(0, 123, 456);
Entity en2 = new Entity(1, 123, 456);
Entity en3 = new Entity(2, 123, 789);
Entity en4 = new Entity(2, 123, 456);
Entity en5 = new Entity(1, 123, 789);
// Set based on main id
Set<Entity> mainIdSet = new HashSet<>();
mainIdSet.add(en1);
mainIdSet.add(en2);
mainIdSet.add(en3);
mainIdSet.add(en4);
mainIdSet.add(en5);
System.out.println("Main id set:");
for (Entity entity : mainIdSet) {
System.out.println(entity);
}
// Set based on ida, idb
Set<CompositeIdWrapper> compositeIdSet = new HashSet<>();
compositeIdSet.add(new CompositeIdWrapper(en1));
compositeIdSet.add(new CompositeIdWrapper(en2));
compositeIdSet.add(new CompositeIdWrapper(en3));
compositeIdSet.add(new CompositeIdWrapper(en4));
compositeIdSet.add(new CompositeIdWrapper(en5));
System.out.println("Composite id set:");
for (CompositeIdWrapper wrapped : compositeIdSet) {
System.out.println(wrapped.getCompositeIdEntity());
}
}
}
Output
Main id set:
Entity [mainId=1, idA=123, idB=456]
Entity [mainId=2, idA=123, idB=789]
Entity [mainId=0, idA=123, idB=456]
Composite id set:
Entity [mainId=0, idA=123, idB=456]
Entity [mainId=2, idA=123, idB=789]
See this, Here I override the equals() and hashcode() to ensure uniqueness on "name" field of a Person object
public class SetObjectEquals {
Person p1 = new Person("harley");
Person p2 = new Person("harley");
public void method1() {
Set<Person> set = new HashSet<Person>();
set.add(p1);
set.add(p2);
System.out.println(set);
}
public static void main(String[] args) {
SetObjectEquals obj = new SetObjectEquals();
obj.method1();
}
}
class Person {
String name;
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
Person(String name) {
this.name = name;
}
}
I have a HashMap for storing objects:
private Map<T, U> fields = Collections.synchronizedMap(new HashMap<T, U>());
but, when trying to check existence of a key, containsKey method returns false.
equals and hashCode methods are implemented, but the key is not found.
When debugging a piece of code:
return fields.containsKey(bean) && fields.get(bean).isChecked();
I have:
bean.hashCode() = 1979946475
fields.keySet().iterator().next().hashCode() = 1979946475
bean.equals(fields.keySet().iterator().next())= true
fields.keySet().iterator().next().equals(bean) = true
but
fields.containsKey(bean) = false
What could cause such strange behavioure?
public class Address extends DtoImpl<Long, Long> implements Serializable{
<fields>
<getters and setters>
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + StringUtils.trimToEmpty(street).hashCode();
result = prime * result + StringUtils.trimToEmpty(town).hashCode();
result = prime * result + StringUtils.trimToEmpty(code).hashCode();
result = prime * result + ((country == null) ? 0 : country.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Address other = (Address) obj;
if (!StringUtils.trimToEmpty(street).equals(StringUtils.trimToEmpty(other.getStreet())))
return false;
if (!StringUtils.trimToEmpty(town).equals(StringUtils.trimToEmpty(other.getTown())))
return false;
if (!StringUtils.trimToEmpty(code).equals(StringUtils.trimToEmpty(other.getCode())))
return false;
if (country == null) {
if (other.country != null)
return false;
} else if (!country.equals(other.country))
return false;
return true;
}
}
You shall not modify the key after having inserted it in the map.
Edit : I found the extract of javadoc in Map :
Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.
Example with a simple wrapper class:
public static class MyWrapper {
private int i;
public MyWrapper(int i) {
this.i = i;
}
public void setI(int i) {
this.i = i;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return i == ((MyWrapper) o).i;
}
#Override
public int hashCode() {
return i;
}
}
and the test :
public static void main(String[] args) throws Exception {
Map<MyWrapper, String> map = new HashMap<MyWrapper, String>();
MyWrapper wrapper = new MyWrapper(1);
map.put(wrapper, "hello");
System.out.println(map.containsKey(wrapper));
wrapper.setI(2);
System.out.println(map.containsKey(wrapper));
}
Output :
true
false
Note : If you dont override hashcode() then you will get true only
As Arnaud Denoyelle points out, modifying a key can have this effect. The reason is that containsKey cares about the key's bucket in the hash map, while the iterator doesn't. If the first key in your map --disregarding buckets -- just happens to be the one you want, then you can get the behavior you're seeing. If there's only one entry in the map, this is of course guaranteed.
Imagine a simple, two-bucket map:
[0: empty] [1: yourKeyValue]
The iterator goes like this:
iterate over all of the elements in bucket 0: there are none
iterate over all the elements in bucket 1: just the one yourKeyValue
The containsKey method, however, goes like this:
keyToFind has a hashCode() == 0, so let me look in bucket 0 (and only there). Oh, it's empty -- return false.
In fact, even if the key stays in the same bucket, you'll still have this problem! If you look at the implementation of HashMap, you'll see that each key-value pair is stored along with the key's hash code. When the map wants to check the stored key against an incoming one, it uses both this hashCode and the key's equals:
((k = e.key) == key || (key != null && key.equals(k))))
This is a nice optimization, since it means that keys with different hashCodes that happen to collide into the same bucket will be seen as non-equal very cheaply (just an int comparison). But it also means that changing the key -- which will not change the stored e.key field -- will break the map.
Debugging the java source code I realized that the method containsKey checks two things on the searched key against every element in the key set:
hashCode and equals; and it does it in that order.
It means that if obj1.hashCode() != obj2.hashCode(), it returns false (without evaluating obj1.equals(obj2). But, if obj1.hashCode() == obj2.hashCode(), then it returns obj1.equals(obj2)
You have to be sure that both methods -may be you have to override them- evaluate to true for your defined criteria.
Here is SSCCE for your issue bellow. It works like a charm and it couldn't be else, because your hashCode and equals methods seem to be autogenerated by IDE and they look fine.
So, the keyword is when debugging. Debug itself can harm your data. For example somewhere in debug window you set expression which changes your fields object or bean object. After that your other expressions will give you unexpected result.
Try to add all this checks inside your method from where you got return statement and print out their results.
import org.apache.commons.lang.StringUtils;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Q21600344 {
public static void main(String[] args) {
MapClass<Address, Checkable> mapClass = new MapClass<>();
mapClass.put(new Address("a", "b", "c", "d"), new Checkable() {
#Override
public boolean isChecked() {
return true;
}
});
System.out.println(mapClass.isChecked(new Address("a", "b", "c", "d")));
}
}
interface Checkable {
boolean isChecked();
}
class MapClass<T, U extends Checkable> {
private Map<T, U> fields = Collections.synchronizedMap(new HashMap<T, U>());
public boolean isChecked(T bean) {
return fields.containsKey(bean) && fields.get(bean).isChecked();
}
public void put(T t, U u) {
fields.put(t, u);
}
}
class Address implements Serializable {
private String street;
private String town;
private String code;
private String country;
Address(String street, String town, String code, String country) {
this.street = street;
this.town = town;
this.code = code;
this.country = country;
}
String getStreet() {
return street;
}
String getTown() {
return town;
}
String getCode() {
return code;
}
String getCountry() {
return country;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + StringUtils.trimToEmpty(street).hashCode();
result = prime * result + StringUtils.trimToEmpty(town).hashCode();
result = prime * result + StringUtils.trimToEmpty(code).hashCode();
result = prime * result + ((country == null) ? 0 : country.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Address other = (Address) obj;
if (!StringUtils.trimToEmpty(street).equals(StringUtils.trimToEmpty(other.getStreet())))
return false;
if (!StringUtils.trimToEmpty(town).equals(StringUtils.trimToEmpty(other.getTown())))
return false;
if (!StringUtils.trimToEmpty(code).equals(StringUtils.trimToEmpty(other.getCode())))
return false;
if (country == null) {
if (other.country != null)
return false;
} else if (!country.equals(other.country))
return false;
return true;
}
}