SQiShER / java-object-diff

Library to diff and merge Java objects with ease
http://sqisher.github.io/java-object-diff/
Apache License 2.0
945 stars 178 forks source link

DiffProvider for byte[] #152

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi, I'm testing your library and found out that if a bean has byte[] it throws IllegalStateException. How can add a proper differ for this object type? I couldn't figure out a way of doing that reading your API. My differ will only base64 and compare the values to check whether has changed or not.

Thanks in advance.

ghost commented 8 years ago

sorry, duplicated.

SQiShER commented 8 years ago

Don't worry about it. Did the other issue give you enough pointers to solve your problem?

ghost commented 8 years ago

Yes it indeed did.

ObjectDifferBuilder.startBuilding().differs().register((differDispatcher, nodeQueryService) -> new ByteArrayDiffer()).build();

private static final class ByteArrayDiffer implements Differ {

@Override
public boolean accepts(Class<?> aClass) {
    return byte[].class.equals(aClass);
}

@Override
public DiffNode compare(DiffNode diffNode, Instances instances) {
    final DiffNode beanNode = new DiffNode(diffNode, instances.getSourceAccessor(), instances.getType());

    if(instances.areNull() || instances.areSame())
        beanNode.setState(DiffNode.State.UNTOUCHED);
    else if(instances.hasBeenAdded())
        beanNode.setState(DiffNode.State.ADDED);
    else if(instances.hasBeenRemoved())
        beanNode.setState(DiffNode.State.REMOVED);
    else
        beanNode.setState(DiffNode.State.CHANGED);

    return beanNode;
}

}

I just need to check whether it has changed so the code worked like a charm!

Thanks!

On Jan 27, 2016, at 18:17, Daniel Bechler notifications@github.com wrote:

Don't worry about it. Did the other issue give you enough pointers to solve your problem?

— Reply to this email directly or view it on GitHub https://github.com/SQiShER/java-object-diff/issues/152#issuecomment-175834502.

SQiShER commented 8 years ago

Awesome! :smiley: