Java equals method not behaving as expected - java

package restaurantclient;
public class Restaurant extends Store {
//Instance Variables
private int peopleServed;
private double averagePrice;
//Constructor with 3 parameters
public Restaurant(String storename, int peopleServed, double averagePrice) {
super(storename);
setPeopleServed(peopleServed);
setAveragePrice(averagePrice);
}
//Getters (Accessors)
public int getPeopleServed() {
return peopleServed;
}
public double getAveragePrice() {
return averagePrice;
}
//Setters (Mutators)
public void setPeopleServed(int peopleServed) {
this.peopleServed = peopleServed;
}
public void setAveragePrice(double averagePrice) {
this.averagePrice = averagePrice;
}
//toString Method [Must Override]
#Override
public String toString() {
String information = "Store name: " + (super.getName());
information += "\n" + "The number of people served: " + peopleServed;
information += "\n" + "The average price per person: $" + averagePrice;
return information;
}
//Equals Method
#Override
public boolean equals (Object other) {
if (this == other)
return true;
if (other == null)
return false;
if (!(other instanceof Restaurant))
return false;
Restaurant otherRestaurant = (Restaurant) other;
if (this.getName() == null) {
if (otherRestaurant.getName() != null)
return false;
} else if (!(this.getName().equals(otherRestaurant.getName())))
return false;
if (peopleServed == -1) {
if (otherRestaurant.peopleServed != -1)
return false;
} else if (peopleServed != (otherRestaurant.peopleServed))
return false;
if (averagePrice == -1) {
if (otherRestaurant.averagePrice != -1)
return false;
}
else if (averagePrice != (otherRestaurant.averagePrice))
return false;
return true;
}
public double getAverageTaxes() {
double total;
total = this.getPeopleServed() * this.getAveragePrice()
* super.CA_TAX_RATE;
return total;
}
}
package restaurantclient;
public class Store {
//Instance Variables
protected final double CA_TAX_RATE = 0.0884;
private String storename;
//Constructor
public Store(String storename) {
setName(storename);
}
//Getters (Accessors)
public String getName() {
return storename;
}
//Setters (Mutators)
public void setName(String storename) {
this.storename = storename;
}
//toString Method [Must Override]
#Override
public String toString() {
String directory = "Name of store: " + storename;
return directory;
}
//Equals Method
public boolean equals (Store storename) {
if (this == storename)
return true;
if (storename == null)
return false;
if (!(storename instanceof Store))
return false;
return true;
}
}
Above are the equals methods I'm calling. They are displaying the wrong answers: it should be in the first instance, "They are not equal" and in the second instance after setting everything equal to each other, it should display, "They are equal". I have tried very hard on this problem and many things have not worked. There are no overt errors it runs fine, but I am doing something wrong and some precise guidance would be a lot of help. Much of the vague hints have got me nowhere. I need something concrete, if this makes to you. Thanks again for the help. The following is the Client class:
package restaurantclient;
public class RestaurantClient {
public static void main(String[] args) {
Restaurant r1 = new Restaurant("McDonald's", 1000000, 8.00);
Restaurant r2 = new Restaurant("KFC", 500000, 6.00);
System.out.println(r1.toString());
System.out.println(r2.toString());
System.out.println();
r2.setAveragePrice(r1.getAveragePrice());
r2.setPeopleServed(r1.getPeopleServed());
System.out.println(r1.toString());
System.out.println(r2.toString());
if (r1.equals(r2)) {
System.out.println("The objects are equal.");
}
else {
System.out.println("The objects are not equal."); //SHOULD say "not equal" here EVERY TIME the second instance (next comment down) says "Equal"...this should never change.
System.out.println();
}
System.out.println();
r2.setName(r1.getName());
System.out.println(r1.toString());
System.out.println(r2.toString());
if (r1.equals(r2)) {
System.out.println("The objects are equal."); //Now that everything is equal, it should print "The Objects are Equal" but it doesn't. It's in lock-step with the previous instance. Changing some things like return true to return false might make both these instances "Are equal" and some might change them to "Not Equal" but they are never the way I want them, which is when 2 changes are made, they are not equal (first case) and when the third and final change is made (like this case here on this line) it should say "the obj are equal" but it doesn't.
}
else {
System.out.println("The objects are not equal.");
System.out.println();
}
System.out.println();
System.out.print("The avg. annual taxes paid by the restaurant is: $");
System.out.println(r1.getAverageTaxes());
}
}

The reason that I see is simple, you are not getting the same name.
In equals, you are comparing super.getName() with otherRestaurant.getName()
If the superclass of Restaurant have a different format or return an other variable, since you compare it to Restaurant.getName(), this will compare different value. Using this.getName() to compare the same variable (or format of variable) is safer. Even if Restaurant.getName() is only returning the super.getName(), this would be safer if you changed the method of Restaurant (because you prefer it an other way).
Here is an example :
Restaurant:
public String getName(){
return "A restaurant " + name;
}
Super class :
public String getName(){
return name;
}
Will result into comparing "A restaurant : KFC" with "KFV".
Using the same getter assure you to return the same "format".
Aslo, your logic is wrong. You want to check if one of the value is different, if it is, return false. And if you reach the end of the method, meaning there where no difference leading to a return false, you return true.
if (this.getName() == null) {
if (otherRestaurant.getName() != null)
return false;
} else if (!super.getName().equals(otherRestaurant.getName())) // added ! here
return false;
if (peopleServed == -1) {
if (otherRestaurant.peopleServed != -1)
return false;
} else if (peopleServed != (otherRestaurant.peopleServed)) // change to != here
return false;
if (averagePrice == -1) {
if (otherRestaurant.averagePrice != -1)
return false;
}
else if (averagePrice != (otherRestaurant.averagePrice)) // change to != here
return false;
//No differences, then it is equals.
return true;
Note :
This condition could be shorten
if (averagePrice == -1) {
if (otherRestaurant.averagePrice != -1)
return false;
}
else if (averagePrice != (otherRestaurant.averagePrice)) // change to != here
return false;
Since it is doing the same thing (comparing the values) :
if (averagePrice != (otherRestaurant.averagePrice))
return false;
Edit :
You are having a problem of overriding.
In Store:
public boolean equals(Store s){}
And in Restaurant
public boolean equals(Object o){}
Since you are calling the method with a Restaurant (subclass of Store), the JVM will use the Store.equals method since it match the type, Restaurant.equals is not overriding it, it override the method in Object. Change to Store.equals(Object o) to correct this.
The method equals comes from Object so it should be always receiving an Object to prevent any problem like this one, if you specify the type in a method, it will not override correctly the method (depending on the type)

Seems you are checking for equality and then returning false, when you should check for not equality to return false.
else if (!super.getName().equals(otherRestaurant.getName()))
return false;
else if (peopleServed != (otherRestaurant.peopleServed))
return false;
else if (averagePrice != (otherRestaurant.averagePrice))
return false;
Also as asked, any reason to uses super.getName() ?
And since peopleServed & averagePrice cannot be null, the -1 check is not needed as the expected result we be the same as the equality check
And finally, I'm guessing the end return should be true, as it means it's different instance of an object, but they have all the same attributs.

Within your equals() method , If super.name() equals otherRestaurant.name() shouldn't you return true, here:
else if (super.getName().equals(otherRestaurant.getName())) return false;

Ok, that one will work in any cases:
#Override
public boolean equals (Object other) {
if (this == other)
return true;
if (other == null)
return false;
if (!(other instanceof Restaurant))
return false;
Restaurant otherRestaurant = (Restaurant) other;
if (name == null) {
if (otherRestaurant.getName() != null)
return false;
} else if (name!=otherRestaurant.getName())
return false;
if (peopleServed == -1) {
if (otherRestaurant.peopleServed != -1)
return false;
} else if (peopleServed != otherRestaurant.peopleServed)
return false;
if (averagePrice == -1) {
if (otherRestaurant.averagePrice != -1)
return false;
}
else if (averagePrice != otherRestaurant.averagePrice)
return false;
return true;
}
check it and reply if it is ok

Related

In Java how would I reference a whole object within a its own class?

I have this in a class called Pokemon and what I would like to do is reference the current object as a whole and pass it through int .use. (I know that you can reference specific pieces of data based on the object using it so I would assume you could do it for the object as a whole) Any help would be greatly appreciated.
public boolean attack(int m,Pokemon p) {
boolean hit = true;
//check what move the use and take away a pp and return it
if (m == 1) {
hit = m1.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else if (m == 2) {
hit = m2.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else if (m == 3) {
hit = m3.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else if (m == 4) {
hit = m4.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else {
return true;
}
}
You use keyword this to refer to (the whole of) the current object.
As the JLS says it:
Keyword this denotes a value that is a reference to the object for which the instance method or default method was invoked.

Java comparable not working in List [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
public class Variable implements Comparable<Variable> {
String name;
String type;
public Variable(String name, String type){
this.name = name;
this.type = type;
}
public String getName(){
return name;
}
public String getType(){
return type;
}
public boolean equals(Object o){
if(o != null && (o instanceof Variable)) {
return ((Variable)o).getName().equals(name) &&
((Variable)o).getType().equals(type);
}
return false;
}
public int compareTo(Variable v){
if (type.compareTo(v.type) != 0) return type.compareTo(v.type);
return name.compareTo(v.name);
}
public String toString(){
return type+" "+name;
}
}
public class Operation implements Comparable<Operation>{
String name;
String type;
List<Variable> arguments;
public Operation(String name, String type){
this.name = name;
this.type = type;
arguments = new ArrayList<Variable>();
}
public void addArgument(Variable a){
arguments.add(a);
}
public String getName(){
return name;
}
public String getType(){
return type;
}
public List<Variable> getArguments(){
return arguments;
}
public int getArgumentNumber(){
return arguments.size();
}
public boolean equals(Object o){
if(o != null && (o instanceof Operation)) {
List<Variable> oa = ((Operation)o).getArguments();
Collection.sort(arguments);
Collection.sort(oa);
return ((Operation)o).getName().equals(name) &&
((Operation)o).getType().equals(type) &&
((Operation)o).getArguments().equals(arguments);
}
return false;
}
public int compareTo(Operation v){
if (type.compareTo(v.type) != 0) return type.compareTo(v.type);
else if(name.compareTo(v.name) != 0) return name.compareTo(v.name);
else{
if (arguments.size() < v.getArgumentNumber()) return -1;
else if(arguments.size() > v.getArgumentNumber()) return 1;
else
}
}
public String toString(){
String s = "";
if (arguments.isEmpty()) return type + " " + name + "(" + ")";
else {
for(Variable v:arguments){
s+= v.type + " ";
}
}
return type + " " + name + "(" + s.trim() + ")";
}
}
As you can see, in the equals function of Operation class
public boolean equals(Object o){
if(o != null && (o instanceof Operation)) {
List<Variable> oa = ((Operation)o).getArguments();
Collection.sort(arguments);
Collection.sort(oa);
return ((Operation)o).getName().equals(name) &&
((Operation)o).getType().equals(type) &&
((Operation)o).getArguments().equals(arguments);
}
return false;
}
I try to sort the list of Variable, but it give me error message "The method sort(List) is undefined for the type Collection", but I have already defined compareTo function in Variable class.
PS. anyone have idea that how should i define compareTo function in Operation class, should i sort first arguments and then compareTo each Variable?
Thanks.
You're using the wrong class to sort. Collection.sort does not exist, while Collections.sort does.
public boolean equals(Object o){
if(o != null && (o instanceof Operation)) {
List<Variable> oa = ((Operation)o).getArguments();
Collections.sort(arguments);
Collections.sort(oa);
return ((Operation)o).getName().equals(name) &&
((Operation)o).getType().equals(type) &&
((Operation)o).getArguments().equals(arguments);
}
return false;
}
Collections, as in plural, with an 's'.
http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#method.summary
PS. anyone have idea that how should i define compareTo function in
Operation class, should i sort first arguments and then compareTo each
Variable?
Since you rely on the order of arguments in your Operation class consider to store the Variables in arguments in a sorted order already.
This means eiter sort the List with every new added Variable or use a Set instead of the List. So you don't have to sort in your equals and compareTo methods.
To your question regarding the compareTo method:
if the size of arguments is the same iterate over both arguments lists comparing the same indices of the Variables via compareTo and return the first result which is != 0. (Or '0' if all elements are equal)
One possible solution:
public int compareTo(Operation v) {
int res = type.compareTo(v.type);
if (res != 0) return res;
res = name.compareTo(v.name);
if (res != 0) return res;
if (arguments.size() != v.arguments.size()) return arguments.size() - v.arguments.size();
for (int i = 0; i < arguments.size(); ++i) {
res = arguments.get(i).compareTo(v.arguments.get(i));
if (res != 0) return res;
}
return 0;
}

Compact equals and hashcode

I have a bean with 4 attributes:
user
institutionId
groupId
postingDate
I use Eclipse to generate equals and hashcode but the resulting code is not pretty. Is there a compact way to do the same? Assuming I want equals & hashcode to use all the attributes or a subset of them.
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((groupId == null) ? 0 : groupId.hashCode());
result = prime * result + ((institutionId == null) ? 0 : institutionId.hashCode());
result = prime * result + ((postingDate == null) ? 0 : postingDate.hashCode());
result = prime * result + ((user == null) ? 0 : user.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;
ManGroupKey other = (ManGroupKey) obj;
if (groupId == null) {
if (other.groupId != null)
return false;
} else if (!groupId.equals(other.groupId))
return false;
if (institutionId == null) {
if (other.institutionId != null)
return false;
} else if (!institutionId.equals(other.institutionId))
return false;
if (postingDate == null) {
if (other.postingDate != null)
return false;
} else if (!postingDate.equals(other.postingDate))
return false;
if (user == null) {
if (other.user != null)
return false;
} else if (!user.equals(other.user))
return false;
return true;
}
In Java 7
public int hashCode() {
return Objects.hash(groupId, institutionId, postingDate, user);
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
// cast to correct class
Target o = (Target)obj;
return Objects.equals(groupId, o.groupId) &&
Objects.equals(institutionId, o.institutionId) &&
Objects.equals(postingDate, o.postingDate) &&
Objects.equals(user, o.user);
}
You could compact the code down, but the odds are far higher that you would introduce bugs than that you would do anything useful. All the parts of the equals and hash code method are there for a reason.
If it's bothering you most IDEs have a folding editor, just click the little yellow box (usually) and all the contents of the method get hidden away.
Instead of using the eclipse generated code, you can use Apache-common-langs(http://commons.apache.org/proper/commons-lang/) class HashCodeBuilder and EqualsBuilder to do this:
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this);
}
hashCode:
Either:
#Override
public int hashCode() {
return Objects.hash(user, institutionId, groupId, postingDate);
}
Or:
#Override
public int hashCode() {
int result = 17;
result = 31 * result + Objects.hashCode(user);
result = 31 * result + Objects.hashCode(institutionId);
result = 31 * result + Objects.hashCode(groupId);
result = 31 * result + Objects.hashCode(postingDate);
return result;
}
Equals:
public boolean equals(Object obj){
if (obj == this){
return true;
}
if (! (obj instanceof ManGroupKey)){
return false;
}
ManGroupKey other = (ManGroupKey) obj;
return Objects.equals(user, other.user)
&& Objects.equals(institutionId, other.institutionId)
&& Objects.equals(groupId, other.groupId)
&& Objects.equals(postingDate, other.postingDate);
}
You can at least remove one level of nesting by removing the other.x != null check.
Comparing a value in this way: x.equals(y) will always return false when y is null.
Aside from that: the .equals() method is a good example where a bit of reflection can be handy, possible extracted out into a generic utility method. All you have to do is run through the different fields and see if they're equal in the two objects, that can be done in a few lines.
Obviously that is only feasible when you actually want to compare each field (or you'll have to add some additions to it which let you choose the fields).
I think the library, that can suite you is apache common. It provides EqualsBuilder and HashCodeBuilder classes, that do exactly what you are looking for.
Consider this question for details: Apache Commons equals/hashCode builder
Here are some code snippets:
public class Bean{
private String name;
private int length;
private List<Bean> children;
#Override
public int hashCode(){
return new HashCodeBuilder()
.append(name)
.append(length)
.append(children)
.toHashCode();
}
#Override
public boolean equals(final Object obj){
if(obj instanceof Bean){
final Bean other = (Bean) obj;
return new EqualsBuilder()
.append(name, other.name)
.append(length, other.length)
.append(children, other.children)
.isEquals();
} else{
return false;
}
}
}

How to avoid many if-else with many condition

I need some help with designing the logic of my problem.
Model Bean
package com.ashish.model;
public class Model {
public Integer a,b,c,d;
public String f,g,h,i,j;
}
Service Class
package com.ashish.service;
import com.ashish.model.Model;
public class Service {
public StringBuilder query = null;
public Service(){
query = new StringBuilder("Select * from A where ");
}
public String build(Model m){
if(m.a != null&&m.b==null&&m.c==null&&m.d==null&m.e==null&&m.f==null&&m.g==null&&m.h==null&&m.i==null&&m.j==null)
query.append("a="+m.a);
if(m.a == null&&m.b!=null&&m.c==null&&m.d==null&m.e==null&&m.f==null&&m.g==null&&m.h==null&&m.i==null&&m.j==null)
query.append("b="+m.b);
if(m.a == null&&m.b==null&&m.c!=null&&m.d==null&m.e==null&&m.f==null&&m.g==null&&m.h==null&&m.i==null&&m.j==null)
query.append("c="+m.c);
if(m.a == null&&m.b==null&&m.c==null&&m.d!=null&m.e==null&&m.f==null&&m.g==null&&m.h==null&&m.i==null&&m.j==null)
query.append("d="+m.d);
if(m.a == null&&m.b==null&&m.c==null&&m.d==null&m.e!=null&&m.f==null&&m.g==null&&m.h==null&&m.i==null&&m.j==null)
query.append("e="+m.e);
if(m.a == null&&m.b==null&&m.c==null&&m.d==null&m.e==null&&m.f!=null&&m.g==null&&m.h==null&&m.i==null&&m.j==null)
query.append("f="+m.f);
if(m.a == null&&m.b==null&&m.c==null&&m.d==null&m.e==null&&m.f==null&&m.g!=null&&m.h==null&&m.i==null&&m.j==null)
query.append("g="+m.g);
if(m.a == null&&m.b==null&&m.c==null&&m.d==null&m.e==null&&m.f==null&&m.g==null&&m.h!=null&&m.i==null&&m.j==null)
query.append("h="+m.h);
if(m.a == null&&m.b==null&&m.c==null&&m.d==null&m.e==null&&m.f==null&&m.g==null&&m.h==null&&m.i!=null&&m.j==null)
query.append("i="+m.i);
if(m.a == null&&m.b==null&&m.c==null&&m.d==null&m.e==null&&m.f==null&&m.g==null&&m.h==null&&m.i==null&&m.j!=null)
query.append("j="+m.j);
if(m.a != null&&m.b!=null&&m.c==null&&m.d==null&m.e==null&&m.f==null&&m.g==null&&m.h==null&&m.i==null&&m.j==null)
query.append("a="+m.a);query.append(" b="+m.b);
if(m.a != null&&m.b==null&&m.c!=null&&m.d==null&m.e==null&&m.f==null&&m.g==null&&m.h==null&&m.i==null&&m.j==null)
query.append("a="+m.a);query.append(" c="+m.c);
if(m.a != null&&m.b==null&&m.c==null&&m.d!=null&m.e==null&&m.f==null&&m.g==null&&m.h==null&&m.i==null&&m.j==null)
query.append("a="+m.a);query.append(" d="+m.d);
// ... 512 lines in this pattern
return query.toString();
return query.toString();
}
}
I want to write public String build(Model m) in such a way so that I would not have to write 512 if-else condition.
Conditions:
All instance variables of Model class can have two value ( null, not null)
They all can be null or they all can be not null.
There would be total 512 combinations ( since every instance variable have two state and there are 9 instance variable so total number of condition would be 2^9 )
Order of the instance variable does not matter.
My project use Java 6 so I can not use switch on String.
I have looked into various pattern but none of them is meeting my requirement.
Thanks for looking
A private helper method as follows should do it -
private void appendIfNotNull(String fieldOp, String val) {
if(val != null) {
query.append(fieldOp).append(val);
}
}
Then just call it in the build method -
public String build(Model m) {
appendIfNotNull("a=", m.a); //no null check, just need to repeat this for all fields
Maybe you want to try to use Java Reflection to read all fields of your Model and read them. You don't need to know the field name to read it. So it would be fully dynamic and generic, even if you extend your Model class.
Class modelClass = Class.forName(Model.class.getName());
Field[] fields = circleClass.getFields(); //includes all fields declared in you model class
for (Field f : fields) {
System.out.println("field " + f.getName() + " has value: " + f.get(<YOUR_MODEL_INSTANCE>));
}
Example code adapted from:
- http://forgetfulprogrammer.wordpress.com/2011/06/13/java-reflection-class-getfields-and-class-getdeclaredfields/
Will this code make sense?
interface ToStringer {
void appendTo(StringBuilder sb);
}
class NullToStringer implements ToStringer {
public void appendTo(StringBuilder sb) {
// Do nothing
}
}
class IntegerToStringer implements ToStringer {
private String fieldName;
private Integer val;
public IntegerToStringer(String fieldName, Integer val) {
this.fieldName = fieldName;
this.val = val;
}
public void appendTo(StringBuilder sb) {
sb.append(field).append(" = ").append(val);
}
}
public class ToStringFactory {
public ToStringer getToStringer(String fieldName, Integer val) {
if (val == null) {
return new NullToStringer();
} else {
return new IntegerToStringer(fieldName, val);
}
}
public ToStringer getToStringer(String fieldName, String val) {
...
}
}
public String build(Model m){
ArrayList<ToStringInstance> list = ...;
list.add(ToStringFactory.getToStringer("f", m.f));
list.add(ToStringFactory.getToStringer("g", m.g));
list.add(ToStringFactory.getToStringer("h", m.h));
StringBuilder sb = ...;
for (ToStringInstance tsi : list) {
tsi.appendTo(sb);
}
return sb.toString();
}
I am not sure what logic you are trying to implement, but the general approach: creating interface, concrete implementaion of printing values, using NullValue pattern to hide null problem and using factory to control objects creation should do the trick.
By using this approach you can avoid problems with 2^9 combinations by avoiding multiple if-else statements.
Update. Just came to my mind. You can use reflection. Iterate through all fields, get value of each, print it if it is no null. Maybe this will be enough.
It seems like you want to append to the query for each element that is not null. This can be done quite simply with an auxiliary method or two:
public class Service {
public StringBuilder query = null;
public Service(){
query = new StringBuilder("Select * from A where ");
}
public String build(Model m) {
boolean added = first;
first &= !maybeAdd("a", m.a, first);
first &= !maybeAdd("b", m.b, first);
. . . // all the rest of the fields of m
}
/**
* Add an equality test to an SQL query if the value is not {#code null}.
* #param key the field name for the query
* #param value the value to test for equality
* #param first flag indicating that no conditions have been added
* #return {#code true} if the value was appended; {#code false} otherwise.
*/
private boolean maybeAdd(String key, Object value, boolean first) {
if (value != null) {
if (!first) {
query.append(" AND ");
}
query.append(key).append('=').append(value);
return true;
}
return false;
}
}
Note that if all fields of the model are null, your query will not be correctly formed. You might want to include the appropriate logic in the maybeAdd method to compensate for that.
As I mentioned in my comment, you don't need a different if for every combination. You just need to append the values that are not null, and ignore the ones that are. Let me know if this works for you.
public String build(Model m) {
// use this to know when to add " AND " to separate existing values
boolean appended = false;
if (m.a != null) {
query.append("a=" + m.a);
appended = true;
}
if (m.b != null) {
if (appended) {
query.append(" AND ");
}
query.append("b=" + m.b);
appended = true;
}
if (m.c != null) {
if (appended) {
query.append(" AND ");
}
query.append("c=" + m.c);
appended = true;
}
if (m.d != null) {
if (appended) {
query.append(" AND ");
}
query.append("d=" + m.d);
appended = true;
}
if (m.e != null) {
if (appended) {
query.append(" AND ");
}
query.append("e=" + m.e);
appended = true;
}
if (m.f != null) {
if (appended) {
query.append(" AND ");
}
query.append("f=" + m.f);
appended = true;
}
if (m.g != null) {
if (appended) {
query.append(" AND ");
}
query.append("g=" + m.g);
appended = true;
}
if (m.h != null) {
if (appended) {
query.append(" AND ");
}
query.append("h=" + m.h);
appended = true;
}
if (m.i != null) {
if (appended) {
query.append(" AND ");
}
query.append("i=" + m.i);
appended = true;
}
if (m.j != null) {
if (appended) {
query.append(" AND ");
}
query.append("j=" + m.j);
appended = true;
}
return query.toString();
}
I don't know why you need two if statements for this:
if( m.a == null) {
query.append("m=null");
} else {
query.append("m="+m.a);
}
if( m.b == null) {
query.append("m=null");
} else {
query.append("m="+m.b);
}

Override the equals method

I'm currently revising for an exam. On a past paper there was the question,
Override the equals method in the following class. The method shall check for
content equality of the whole state.
class Employee
{
String firstName;
int age;
}
[2 marks]
I did some fiddling for the right answer and have come up so far with this. Is there a simpler way to answer the question and is this right? Many thanks for help.
public class Employee
{
int age;
public boolean equals(Object obj)
{
if(this == obj)
{
return true; //Reference equality.
}
if(!(obj instanceof Employee))
{
return false; // not the same type.
}
Employee other = (Employee) obj;
return firstName == other.firstName;
return age == other.age;
}
}
Use
return (((this.firstName == null || other.firstName == null)
&& this.firstName == other.firstName)
||
this.firstName.equals(other.firstName))
&& age == other.age;
This handles null cases too.
One thing to note, and I wouldn't imagine you would get docked for this in an exam...
It's usually poor practice to do an instanceof when the class isn't final. The reason for that is that equals() must be symmetric. Accepting subclasses (who might also implement equals with their own new aspects) could cause it to not be symmetric.
Example (I think the example is the same used in Effective Java 2ed):
class Point {
protected int x, y;
//equals method uses instanceof Point and checks x and y values are the same
}
class ColorPoint extends Point {
protected Color color;
//equals method checks that it's a ColorPoint, that super.equals is true,
//then checks the Color
}
new Point(1, 2).equals(new ColorPoint(1, 2, Color.red)); //true
new ColorPoint(1, 2, Color.red).equals(new Point(1, 2)); //false
It's a very subtle point that even most of the answerers here didn't take into account. But it's the reason that most generators of equals (such as the one in your favourite IDE) tend to do exact class comparison:
if ( this.getClass() != other.getClass() ) {
return false;
}
When the equals method uses instanceof it's usually a good move to document that subclasses must follow the exact same specification.
A couple of points:
You need to check if obj is null.
To compare String contents in Java, use equals(), i.e. firstName.equals(other.firstName). Check to see if firstName is null first.
Here's an improved implementation:
public boolean equals(Object obj)
{
if(obj == null)
{
return false;
}
if(this == obj)
{
return true; //Reference equality.
}
if(this.getClass() != obj.getClass())
{
return false;
}
Employee other = (Employee) obj;
if(firstName == null)
{
if(other.firstName != null)
{
return false;
}
}
else if(!firstName.equals(other.firstName))
{
return false;
}
return age == other.age;
}
EDIT: Updated type comparison to make equals() symmetric in accordance with #Mark Peters' answer.
The String firstName should be compared with .equals(), NOT ==. The == compare is ok for the primitive int age field.
What if both firstNames are identical, yet age is unequal? Shouldn't this fail?
Something like return (firstName.equals(obj.firstName)) && (age == obj.age);
Of course, that doesn't consider the case when this.firstName is null, which would result in a NullPointerException being thrown.
Are the Employees considered equal if both have null firstNames? What if one is null and the other not? Assuming both must be null, or both must be String.equals(), you could use:
return ((null == firstName && null == obj.firstName)
|| (null != firstName && firstName.equals(obj.firstName)))
&& (age == obj.age);
instead of your 2 return statements. The rest looks ok.
public boolean equals(Object o){
if(this==o){ //same instance, no need to check more
return true;
}
if(o instanceof Employee){ //when null this will yield false
Employee other = (Employee) o;
return (this.name == other.name || (this.name != null && this.name.equals(other.name)) && this.age == other.age;
}
return false;
}
To put together in one answer all the parts already mentioned:
public boolean equals(Object obj) {
if(this == obj) {
return true; //Reference equality.
}
if(obj == null || !(obj instanceof Employee))
{
return false; // not the same type.
}
Employee other = (Employee) obj;
return (firstName.equals(other.firstName) && age == other.age);
}
Your last line, the age comparison, is unreachable; you shouldn't use == to compare Strings; and you need to account for null values.
Since the general movement seems to be towards laying it all out for you, here's Eclipse's implementation:
public class Employee {
private final String firstName;
private final int age;
public Employee(final String firstName, final int age) {
super();
this.firstName = firstName;
this.age = age;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
return result;
}
#Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Employee other = (Employee) obj;
if (age != other.age) {
return false;
}
if (firstName == null) {
if (other.firstName != null) {
return false;
}
} else if (!firstName.equals(other.firstName)) {
return false;
}
return true;
}
}
And a battery of tests:
import org.junit.Test;
public class EmployeeTest {
#Test
public void testEmployeeEquals() {
final Employee nullNameEmp = new Employee(null, -1);
final Employee empA1 = new Employee("a", 1);
final Employee empA1Clone = new Employee("a", 1);
final Employee empA2 = new Employee("a", 2);
final Employee empB1 = new Employee("b", 1);
final Employee empB2 = new Employee("b", 2);
final Employee subEmp = new Employee("a", 1) {
};
assert !nullNameEmp.equals(empA1);
assert !nullNameEmp.equals(empA1Clone);
assert !nullNameEmp.equals(empA2);
assert !nullNameEmp.equals(empB1);
assert !nullNameEmp.equals(empB2);
assert !nullNameEmp.equals(subEmp);
assert !nullNameEmp.equals(null);
assert !empA1.equals(nullNameEmp);
assert empA1.equals(empA1Clone);
assert !empA1.equals(empA2);
assert !empA1.equals(empB1);
assert !empA1.equals(empB2);
assert !empA1.equals(subEmp);
assert !empA1.equals(null);
assert !empA2.equals(nullNameEmp);
assert !empA2.equals(empA1);
assert !nullNameEmp.equals(empA1Clone);
assert !empA2.equals(empB1);
assert !empA2.equals(empB2);
assert !empA2.equals(subEmp);
assert !empA2.equals(null);
assert !empB1.equals(nullNameEmp);
assert !empB1.equals(empA1);
assert !empB1.equals(empA1Clone);
assert !empB1.equals(empA2);
assert !empB1.equals(empB2);
assert !empB1.equals(subEmp);
assert !empB1.equals(null);
assert !empB2.equals(nullNameEmp);
assert !empB2.equals(empA1);
assert !empB2.equals(empA1Clone);
assert !empB2.equals(empA2);
assert !empB2.equals(empB1);
assert !empB2.equals(subEmp);
assert !empB2.equals(null);
assert !subEmp.equals(nullNameEmp);
assert !subEmp.equals(empA1);
assert !subEmp.equals(empA1Clone);
assert !subEmp.equals(empA2);
assert !subEmp.equals(empB1);
assert !subEmp.equals(empB2);
assert !subEmp.equals(null);
assert nullNameEmp.equals(nullNameEmp);
assert empA1.equals(empA1);
assert empA1Clone.equals(empA1Clone);
assert empA2.equals(empA2);
assert empB1.equals(empB1);
assert empB2.equals(empB2);
assert subEmp.equals(subEmp);
}
}

Categories

Resources