0

I have a serializable class, that I'd like to compare. It contains many fields and will probably change many times in the future. What are the downsides of implementing equals like this:

public class ProbierKlasse implements Serializable{
    private static final long serialVersionUID = -7996094306948821373L;

    private File dir = new File("dir");
    private File subDir = new File(dir, "subDir");
    private File file = new File(dir, "file");

    public static void main(String[] args) throws IOException {
        ProbierKlasse pk = new ProbierKlasse();
        ProbierKlasse pk1 = new ProbierKlasse();
        System.out.println(pk.equals(pk1)); //prints true

        pk1.subDir = new File(pk1.dir, "anotherDir");
        System.out.println(pk.equals(pk1)); //prints false
    }

    @Override
    public boolean equals(Object obj) {
        byte[] thisBytes = getByteArray(this);
        byte[] objBytes = getByteArray((ProbierKlasse)obj);

        return Arrays.equals(thisBytes, objBytes);
    }

    private byte[] getByteArray(Serializable obj){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos;
        try {
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return baos.toByteArray();
    }
}

I could think of performance as an issue, but in this case it is not so important. It has the big advantage, that I don't have to adjust it, if I add new fields.

Thanks in advance and please don't hate me for asking.

Nils-o-mat
  • 1,132
  • 17
  • 31

1 Answers1

1

So, you're basically writing your object in a byteArray then comparing these byte arrays.

Performance might be a downside, but to be honest, it depends on how many times and on how big are your objects. If it's two or three object every seconds, you should be alright, but if it's a few thousand per seconds, this might take some times. Also, if your object is referencing other objects, it will take up some more time.

Just be careful not to have a circular referencing (but I guess that as long as serializing works, the equals will works). For example: if A references B and B references A.

Anyway, if you want better automatic compare system, you shoud check "reflection in java". It's an interresting thing. Also, if you want a comparator that already works using this, apache has a really good comparator (the package is apche utils or something).

Check this: Is there a Java reflection utility to do a deep comparison of two objects? (it's for deep comparaison, but should also work for you) and this: Comparing field values using reflection

Community
  • 1
  • 1
Asoub
  • 2,273
  • 1
  • 20
  • 33
  • Thanks for your answer. I guess circular referencing is a good point. I know a little bit about reflection, but I didn't know about the apache-equals-builder. I will check it out. I will wait a little for additional answers, before accepting yours. – Nils-o-mat Sep 25 '15 at 14:50