Check object empty - java

Basically how do you check if an object is null or empty. What I mean is that if I have an object instantiated but all its values or fields are null, the how do I check in code if it is empty?
I have tried;
if (doc != null){
.... do something
But it doesn't seem to work.

You can't do it directly, you should provide your own way to check this. Eg.
class MyClass {
Object attr1, attr2, attr3;
public boolean isValid() {
return attr1 != null && attr2 != null && attr3 != null;
}
}
Or make all fields final and initialize them in constructors so that you can be sure that everything is initialized.

import org.apache.commons.lang3.ObjectUtils;
if(ObjectUtils.isEmpty(yourObject)){
//your block here
}

This can be done with java reflection,This method returns false if any one attribute value is present for the object ,hope it helps some one
public boolean isEmpty() {
for (Field field : this.getClass().getDeclaredFields()) {
try {
field.setAccessible(true);
if (field.get(this)!=null) {
return false;
}
} catch (Exception e) {
System.out.println("Exception occured in processing");
}
}
return true;
}

You should check it against null.
If you want to check if object x is null or not, you can do:
if(x != null)
But if it is not null, it can have properties which are null or empty. You will check those explicitly:
if(x.getProperty() != null)
For "empty" check, it depends on what type is involved. For a Java String, you usually do:
if(str != null && !str.isEmpty())
As you haven't mentioned about any specific problem with this, difficult to tell.

I suggest you add separate overloaded method and add them to your projects Utility/Utilities class.
To check for Collection be empty or null
public static boolean isEmpty(Collection obj) {
return obj == null || obj.isEmpty();
}
or use Apache Commons CollectionUtils.isEmpty()
To check if Map is empty or null
public static boolean isEmpty(Map<?, ?> value) {
return value == null || value.isEmpty();
}
or use Apache Commons MapUtils.isEmpty()
To check for String empty or null
public static boolean isEmpty(String string) {
return string == null || string.trim().isEmpty();
}
or use Apache Commons StringUtils.isBlank()
To check an object is null is easy but to verify if it's empty is tricky as object can have many private or inherited variables and nested objects which should all be empty. For that All need to be verified or some isEmpty() method be in all objects which would verify the objects emptiness.

If your Object contains Objects then check if they are null, if it have primitives check for their default values.
for Instance:
Person Object
name Property with getter and setter
to check if name is not initialized.
Person p = new Person();
if(p.getName()!=null)

I have a way, you guys tell me how good it is.
Create a new object of the class and compare it with your object (which you want to check for emptiness).
To be correctly able to do it :
Override the hashCode() and equals() methods of your model class and also of the classes, objects of whose are members of your class, for example :
Person class (primary model class) :
public class Person {
private int age;
private String firstName;
private String lastName;
private Address address;
//getters and setters
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + age;
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.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 (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (age != other.age)
return false;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
#Override
public String toString() {
return "Person [age=" + age + ", firstName=" + firstName + ", lastName=" + lastName + ", address=" + address
+ "]";
}
}
Address class (used inside Person class) :
public class Address {
private String line1;
private String line2;
//getters and setters
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((line1 == null) ? 0 : line1.hashCode());
result = prime * result + ((line2 == null) ? 0 : line2.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 (line1 == null) {
if (other.line1 != null)
return false;
} else if (!line1.equals(other.line1))
return false;
if (line2 == null) {
if (other.line2 != null)
return false;
} else if (!line2.equals(other.line2))
return false;
return true;
}
#Override
public String toString() {
return "Address [line1=" + line1 + ", line2=" + line2 + "]";
}
}
Now in the main class :
Person person1 = new Person();
person1.setAge(20);
Person person2 = new Person();
Person person3 = new Person();
if(person1.equals(person2)) --> this will be false
if(person2.equals(person3)) --> this will be true
I hope this is the best way instead of putting if conditions on each and every member variables.
Let me know !

for simple (It's worked in my project).
if null check not mandatory for some fields then exclude it from toString() method as in my above code, I have removed school.
public class Student {
private String name;
private String school;
private Integer roll;
private String section;
//getters and setters
#Override
public String toString() {
return "Student [name=" + name + ", roll=" + roll + ", section=" + section + "]";
}
}
public class StudentRunner {
public static void main(String[] args) {
Student s = new Student();
s.setName("ved");
s.setRoll(12);
s.setSection("A");
s.setSchool(null);//school set null and it removed from toString() method
if(s.toString().contains("null")) {
System.out.println("null value contains");
//do your work here or throw exception
} else {
System.out.println("no null value");
}
}
}
output : no null value

let suppose ,
data = {};
if( if(!$.isEmptyObject(data)))
{
document.write("Object is empty);
}
else{
document.write("Object is not empty);
}
It worked for me and its an easy way to check if object is empty or not

Related

Simplify Overridden equals and hashcode methods

I have a person class with overridden hashcode and equals, looks something like this :
class PersonKey {
private String personUID;
private String ssnUID;
private String countryCode;
public PersonKey (
String personUID, String ssnUID, String countryCode) {
this.personUID= personUID;
this.ssnUID= countryCode;
this.countryCode= countryCode;
}
.....
}
This class already had personUID and ssnUID fields and I am introducing countryCode as a new field.
This class already had a complicated hashcode and equals method, with the inclusion of countryCode the logic got even more complicated. Is there a way I can reduce the Cyclomatic Complexity using inheritance or wrapper class?
HashCode :
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result;
if (personUID != null) result += personUID.hashCode();
else result += ((ssnUID == null) ? 0 : ssnUID.hashCode());
if ((personUID != null || ssnUID != null)
&& (countryCode != null)) {
result += countryCode.hashCode();
}
return result;
}
Equals :
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
PersonKey other = (PersonKey) obj;
if (!getOuterType().equals(other.getOuterType())) {
return false;
}
// For the purposes of mapping any "empty" Person (null
// personUID and ssnUID) is considered to be unique so return
// false
if ((ssnUID == null) && (personUID == null)) {
return false;
}
// Only evaluate ssnUID if the personUID is null
if (personUID == null) {
// If the current ssnUID matches the personUID
// from the other key the keys will be assumed to match
if (ssnUID.equals(other.personUID)) {
if (StringUtils.isBlank(countryCode) && StringUtils.isBlank(other.countryCode)) {
return true;
} else {
if (countryCode.equals(other.contryCode)) return true;
}
} else if (other.personUID != null) {
return false;
}
if (ssnUID.equals(other.ssnUID)) {
if (StringUtils.isBlank(v) && StringUtils.isBlank(other.countryCode)) {
return true;
} else {
if (countryCode.equals(other.countryCode)) return true;
}
}
}
// If the current personUID matches the personUID
// the keys will
// be considered a match regardless of the value of the ssnUID.
else if (personUID .equals(other.personUID)) {
if (StringUtils.isBlank(contryCode) && StringUtils.isBlank(other.contryCode)) {
return true;
} else {
if (countryCode.equals(other.countryCode)) return true;
}
}
// If the current personUID matches the ssnUID
// from the other key, and the other keys personUID is null
// the keys will be considered a match
else if ((other.personUID == null)
&& (personUID.equals(other.ssnUID))) {
if (StringUtils.isBlank(contryCode) && StringUtils.isBlank(other.contryCode)) {
return true;
} else {
if (countryCode.equals(other.countryCode)) return true;
}
}
return false;
}
You can use Lombok's #EqualsAndHashCode would be the fastest option.
Example and documentation can be found here: https://projectlombok.org/features/EqualsAndHashCode
If you create a new method in your class:
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
class PersonKey {
private String personUID;
private String ssnUID;
private String countryCode;
...
#EqualsAndHashCode.Include
public String uidOrSsn() {
return personUID != null ? personUID : ssnUID;
}
}
This way you're telling Lombok to only create the equals and hashCode for the uidOrSsn() method declared. Whichever the non-null value of personUID or ssnUID, then it will be generated.

overriding equal and hashcode in Java where this.obj is null

Hi I am trying to overriding the equal and hashcode method in for my custom class .
here is my custom class
public class property
{
public String type = null;
public int value;
public int id;
public String name = null;
public String course = null;
public property(String type, String course, int value, int id)
{
this.value = value;
this.type = type;
this.course = course;
this.id = id;
}
#Override
public boolean equals(Object obj) {
if(this == obj)
return true;
if((obj == null) || (obj.getClass() != this.getClass()))
return false;
property sig = (property) obj;
if(sig != null) {
if(sig.type.equals("EST1")) {
if (sig.name != null && sig.type != null && sig.course != null) {
Log.d("property", "Other " + sig.course + "AHHAHAHA " + course + "shahahaaha " + sig.id + "babababaab " + id);
if (sig.course.equals(this.course) && sig.value == this.value && sig.type.equals(this.type) && sig.id == (this.id)) {
Log.d("property", "EST1");
return true;
}
}
}
return false;
}
The problem is that "this.id, this.type" is always null or 0 I do not understand why...while sig.id, sig.type have values. Just to let you know it is only code snippet, not full code so it might have typos and not logically correct..
Your code works well in my side, so I guess your test program is somewhat different (or complicated?) from mine.
I tried following for test,
property p = new property("a","b", 100, 200);
System.out.println("id:"+p.id);
System.out.println("type:"+p.type);
System.out.println(p.equals(p) +" should be true..");
And I got,
id:200
type:a
true should be true..

Comparing objects and its member variable

Suppose I have two objects of same class for example:
public class Example {
String name;
String rollNo;
String address;
String phoneNo;
String city;
}
Example obj1 = new Example();
obj1.name = "Name";
obj1.rollNo = "10";
obj1.address = "Address";
obj1.phoneNo = "Phone Number";
obj1.city = "City";
Example obj2 = new Example();
obj2.name = "Name";
obj2.rollNo = "10";
obj2.address = "Address";
obj2.phoneNo = "Phone Number";
obj2.city = "City";
Here I want to compare obj1 with obj2 the catch is I don't want to do this with if condition i.e. getting each member variable of obj1 then comparing it with obj2 variable.
equals method of java.lang.Object class will not work here as it compares object reference.
My question is, is there any Java API which will compare two objects and its member variables.
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Example other = (Example) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (phoneNo == null) {
if (other.phoneNo != null)
return false;
} else if (!phoneNo.equals(other.phoneNo))
return false;
if (rollNo == null) {
if (other.rollNo != null)
return false;
} else if (!rollNo.equals(other.rollNo))
return false;
return true;
}
Paste this equals function into your exmaple class and then compare the objects like this:
if(obj1.equals(obj2)) { //will return true now
}
In Java, there is three alternatives to compare objects :
By overriding the Object.equals() method.
Using the Generic java.util.Comparator Interface
Using the java.lang.Comparable interface
Check this question, and this link for more details.

Method that adds elements to set always throwing custom exception

Ok, so I've got a method which adds elements to a list but it is always throwing my custom exception, no matter what, even when there are no elements in the Set I made.
private Set<Plan> planSet = new HashSet<Plan>();
public Plan createPlan(String name) throws DuplicatePlan{
Plan plan = new Plan(name);
if(!planSet.contains(plan)){
planSet.add(plan);
} else {
throw(new DuplicatePlan("Error, duplicate plan"));
}
return plan;
}
I'm thinking that my equals() and hashCode() methods are causing this. Currently I'm using the default overridden Eclipse hashCode() and equals(), this is what I've got there:
#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;
}
Plan other = (Plan) obj;
if (name == null) {
if (other.name != null){
return false;
}
} else if (!name.equals(other.name)){
return false;
}
return true;
}
This is what Plan does:
private String name;
private Set<Tables> tablesSet;
public Plan(String name){
this.name = name ;
}
Here's what's supposed to happen if a user sets the same name in the TextField:
newPlan.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent action){
if(!newPlan.getText().isEmpty()){
try {
String name = planName.getText();
plan.createPLan(name);
esquema = esquemas.createPlan(planName.getText());
optionsPlans.getItems().add(plan.getName());
} catch (DuplicatePlan e) {
dialog.errorDialog(planError, duplicate);
}
} else {
dialog.errorDialog(empty, emptySpace);
}
}
});
Had to use Answer because it was too long for comment.
This here looks suspicious to me:
String name = planName.getText();
plan.createPLan(name);
esquema = esquemas.createPlan(planName.getText());
I.e. what's up with createPLan and createPlan? Copy & paste error? Or are you calling the same method twice (which would explain the behavior)?

Calling equals method of superclass in Java

I have an issue where I am trying to use test the equals method of a superclass using objects of a subclass:
My superclass is:
public class Excluded_DateTime implements Serializable {
private Date fromDate;
private Time fromTime;
private Date toDate;
private Time toTime;
private Valid active;
And the subclass differs by having an identifier as a key:
public class Classifier_Excluded_DateTime implements Serializable {
private Integer classifierExcludedDateTimeNo;
private Excluded_DateTime classifierExcludedDateTime;
So I want to test the equality of Classifier_Excluded_DateTime objects without using the field classifierExcludedDateTimeNo.
But what I am finding is that the equals method of the superclass is never called.
The NetBeans generated equals and hashCode methods of the superclass are as follows:
#Override
public int hashCode() {
int hash = 7;
hash = 23 * hash + (this.fromDate != null ? this.fromDate.hashCode() : 0);
hash = 23 * hash + (this.fromTime != null ? this.fromTime.hashCode() : 0);
hash = 23 * hash + (this.toDate != null ? this.toDate.hashCode() : 0);
hash = 23 * hash + (this.toTime != null ? this.toTime.hashCode() : 0);
hash = 23 * hash + (this.active != null ? this.active.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Excluded_DateTime other = (Excluded_DateTime) obj;
if (this.fromDate != other.fromDate && (this.fromDate == null || !this.fromDate.equals(other.fromDate))) {
return false;
}
if (this.fromTime != other.fromTime && (this.fromTime == null || !this.fromTime.equals(other.fromTime))) {
return false;
}
if (this.toDate != other.toDate && (this.toDate == null || !this.toDate.equals(other.toDate))) {
return false;
}
if (this.toTime != other.toTime && (this.toTime == null || !this.toTime.equals(other.toTime))) {
return false;
}
if (this.active != other.active) {
return false;
}
return true;
}
And those of the subclass as follows:
#Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + (this.getClassifierExcludedDateTimeNo() != null ? this.getClassifierExcludedDateTimeNo().hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
if (! super.equals(obj)) {
return false;
}
final Classifier_Excluded_DateTime other = (Classifier_Excluded_DateTime) obj;
if (this.getClassifierExcludedDateTimeNo() != other.getClassifierExcludedDateTimeNo() && (this.getClassifierExcludedDateTimeNo() == null || !this.classifierExcludedDateTimeNo.equals(other.ClassifierExcludedDateTimeNo))) {
return false;
}
return true;
}
Can I do what I'm trying to do by amending these methods of either class?
I am trying to test whether two instance of Classifier_Excluded_DateTime are the same without including the field classifierExcludedDateTimeNo in the comparison.
You are not declaring Classifier_Excluded_DateTime as a subclass of Excluded_DateTime. To do that, you would need to say:
public class Classifier_Excluded_DateTime extends Excluded_DateTime {
Right now, it seems more like you are treating Excluded_DateTime as a kind of delegate of Classifier_Excluded_DateTime, as you have a member instance variable inside of Classifier_Excluded_DateTime that is of type Excluded_DateTime (the place where you say private Excluded_DateTime classifierExcludedDateTime;). I don't think that's your intent, since you are talking about subclasses and superclasses in your post.
(Update: removed error where I thought there was not an explicit super.equals() call).

Categories

Resources