How do you compare different object types with java-object-diff

Viewed 913

I'm using the java-object-diff https://github.com/SQiShER/java-object-diff and trying to compare two objects that implement the same interface. As an example I set up a test with apples and oranges. I know you shouldn't compare apples and oranges but I have some use cases where I need to do this.

I've tried casting. I also tried implementing this with an abstract superclass of Fruit with Apple and Orange classes that extend it. No dice. I've been hunting around in the API docs and the test classes for a way to do this but no luck so far. Anybody out there familiar with this API that could point me in the right direction would be great.

Here's the code:

public interface Fruit {

    public final static String SIZE_SMALL = "Small";
    public final static String SIZE_LARGE = "Large";
    public final static String CONDITION_RIPE = "Ripe";
    public final static String CONDITION_NOT_RIPE = "Not Ripe";

    public String getSize();
    public String getCondition();
    public Double getWeight();
    public Date getSellBy();
}

public class Apple implements Fruit {

    private String size;
    private String condition;
    private Double weight;
    private Date sellBy;

    public Apple (String size, String condition, Double weight, Date sellBy){
        this.size= size;
        this.condition = condition;
        this.weight = weight;
        this.sellBy = sellBy;
    }

    public String getSize() {
        return size;
    }
    public void setSize(String size) {
        this.size= size;
    }

    public String getCondition() {
        return condition;
    }
    public void setCondition(String condition) {
        condition = condition;
    }

    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double weight) {
        this.weight = weight;
    }

    public Date getSellBy() {
        return sellBy;
    }
    public void setSellBy(Date sellBy) {
        this.sellBy = sellBy;
    }

}

public class Orange implements Fruit {

    private String size;
    private String condition;
    private Double weight;
    private Date sellBy;

    public Orange (String size, String condition, Double weight, Date sellBy){
        this.size = size;
        this.condition = condition;
        this.weight = weight;
        this.sellBy = sellBy;
    }

    public String getSize() {
        return size;
    }
    public void setSize(String size) {
        this.size = size;
    }

    public String getCondition() {
        return condition;
    }
    public void setCondition(String condition) {
        condition = condition;
    }

    public Double getWeight() {
        return weight;
    }
    public void setWeight(Double weight) {
        this.weight = weight;
    }

    public Date getSellBy() {
        return sellBy;
    }
    public void setSellBy(Date sellBy) {
        this.sellBy = sellBy;
    }

}

Below are two test cases:

Test case 1

 @Test public void testCompareAppleAndOrangeIface() {
     Apple apple = new Apple(Fruit.SIZE_LARGE,Fruit.CONDITION_RIPE, 0.3, new Date());   //(String size, String condition, Double weight, Date sellBy)
     Orange orange = new Orange(Fruit.SIZE_SMALL,Fruit.CONDITION_RIPE, 0.2, new Date());

     DiffNode diff = ObjectDifferBuilder.buildDefault().compare(apple, orange);
     LOGGER.info("\n\nComparing apples and oranges IFace...");
     LOGGER.info("diff.hasChanges() " + diff.hasChanges());
     LOGGER.info("diff.childCount() " + diff.childCount());

     diff.visit(new DiffNode.Visitor()
     {
         public void node(DiffNode node, Visit visit)
         {
             final Object baseValue = node.canonicalGet(apple);
             final Object workingValue = node.canonicalGet(orange);
             final String message = node.getPath() + " changed from " + 
                     baseValue + " to " + workingValue;
             LOGGER.info(message);
         }
     });
 }

Results:

Comparing apples and oranges IFace...
[main] INFO xxx.compare.test.CompareTest - diff.hasChanges() true
[main] INFO xxx.compare.test.CompareTest - diff.childCount() 0
[main] INFO xxx.compare.test.CompareTest - / changed from xxx.compare.test.Apple@368102c8 to xxx.compare.test.Orange@6996db8
[main] INFO xxx.compare.test.CompareTest - 

Test case 2

 @Test public void testCompareFruitIface() {
     LOGGER.info("\n\nComparing fruit Iface...");

     Fruit fruitApple = new Apple(Fruit.SIZE_LARGE,Fruit.CONDITION_RIPE, 0.3, new Date());  //(String size, String condition, Double weight, Date sellBy)
     Fruit fruitOrange  = new Orange(Fruit.SIZE_SMALL,Fruit.CONDITION_RIPE, 0.2, new Date());

     DiffNode diff = ObjectDifferBuilder.buildDefault().compare(fruitApple, fruitOrange);

     LOGGER.info("diff.hasChanges() " + diff.hasChanges());
     LOGGER.info("diff.childCount() " + diff.childCount());

     diff.visit(new DiffNode.Visitor()
     {
         public void node(DiffNode node, Visit visit)
         {
             // Also tried this with node.get(fruitApple), same results
             final Object baseValue = node.canonicalGet(fruitApple);
             final Object workingValue = node.canonicalGet(fruitOrange);
             final String message = node.getPath() + " changed from " + 
                     baseValue + " to " + workingValue;
             LOGGER.info(message);
         }
     });
 }

Results:

Comparing fruit Iface...
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO de.danielbechler.diff.access.Instances - Detected instances of different types [class xxx.compare.test.Apple, class xxx.compare.test.Orange]. These objects will only be compared via equals method.
[main] INFO xxx.compare.test.CompareTest - diff.hasChanges() true
[main] INFO xxx.compare.test.CompareTest - diff.childCount() 0
[main] INFO xxx.compare.test.CompareTest - / changed from xxx.compare.test.Apple@1963006a to xxx.compare.test.Orange@7fbe847c
[main] INFO xxx.compare.test.CompareTest -
0 Answers
Related