-1

I have such a problem, that I fill in objects with data in two different way in my app. So sometimes can happen, that one has some null fields ant the second one does not. I would like to compare these two object in a way that when one object has a field with null value, this filed is excluded from comparison. Has anybody have some idea how to do it in practice? The best way for me would be some pseudoEquals() method template which I coud use to generete the code just like you can do with equals() and hashCode(), ... generators in InteliJ. Or to use some matcher, but I did not find one :/

class A {
T f1;
T f2;
T f3;
}

A a1 = new A(value1, value2, value3);
A a2 = new A(value1, null, value3);

a1.pseudoEquals(a2) => true

So I would like to have generator template in InteliJ to be able to generate something like this:

public boolean pseudoEquals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (f1 == null && f2 == null && f3 == null) return false;

        A that = (A) o;
        if (that.f1 == null && that.f2 == null && that.f3 == null) return false;   

        if ((f1 != null && that.f1 != null) && !f1.equals(that.f1)) return false;
        if ((f2 != null && that.f2 != null) && !f2.equals(that.f2)) return false;
        if ((f3 != null && that.f3 != null) && !f3.equals(that.f3)) return false;

        return true;
    }

Could anyone show me the way how to create such a template, or even better does anyone has one? :) Thanks.

  • Possible duplicate of [How to check if object is null in Java?](http://stackoverflow.com/questions/15800738/how-to-check-if-object-is-null-in-java) – santosh gore Mar 23 '17 at 07:53
  • @santoshgore - it is not duplicate of the question which you added. Ok, but why you need this code as template? Second thing, it should be implemented as equal() method of object that the Java knows how to use it explicitly. – h__ Mar 23 '17 at 07:57
  • @h__ - it is basic of java.in question he ask how to exclude null fields when comparing two objects.it is same as check null value of filed in java. – santosh gore Mar 23 '17 at 08:01
  • Possible duplicate of [Is it possible to change IntelliJ's code generation template for equals() and hashCode()](http://stackoverflow.com/questions/18362721/is-it-possible-to-change-intellijs-code-generation-template-for-equals-and-ha) – radoh Mar 23 '17 at 08:04
  • In the duplicate question I posted, you'll find that you can add a custom template for `equals()` method. I haven't tried it, but perhaps you can even change the method signature in the template... – radoh Mar 23 '17 at 08:04
  • @radoh - I can create new template for equals I can even rename the method with my own name, but when I do so, it oweride my 'true' equals method, which I need also. I would need my standalone generator. – user7017392 Mar 23 '17 at 09:26

1 Answers1

0

First, why do you want to do this? You may want to think twice about whether your pseudoEquals() is the best solution to the problem you are really trying to solve. You may also want to share your thoughts, there are many brilliant minds around to give you suggestions.

If you mean what you say, isn’t it just a matter of a little auxiliary class?

public class MyObjects {
    public static boolean equalIfNotNull(Object attr1, Object attr2) {
        return attr1 == null || attr2 == null || attr1.equals(attr2);
    }
}

Now in your pseudoEquals() you can do

        return MyObjects.equalIfNotNull(f1, that.f1)
                && MyObjects.equalIfNotNull(f2, that.f2)
                && MyObjects.equalIfNotNull(f3, that.f3);

Edit: More thoughts:

  • You may make it more elegant with varargs and method references: MyObjects.pseudoEqual(this, that, A::getF1, A::getF2, A::getF3).
  • You may use some reflection magic to detect all the instance fields of your class and perform the logic you require.

Of course, if you prefer, you can write a plugin for IntelliJ IDEA that will produce the code for you. Could be overkill?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thank you. I would prefer the generator, I have a lot of DTO, sometimes with a lot of data members. So I'm going to try it that way. – user7017392 Mar 23 '17 at 09:31