开发者

Having null safe checks using org.apache.commons.ObjectUtil class for custom objects

Ive been searching for a robust way to compare my objects, I came across the ObjectUtils , and had the immidiate doubt that would it be able to com开发者_开发技巧pare it efficiently or not as I do not know how it works internally and documentation on apache org site about this is scarce.

Can someone please help me with this??

EDIT:

When I say compare , all I really need to to compare for equality of MYOBJ, where MYOBJ is a custom object I have defined , which has various variables in it(all these vars are primitive data types like int,long,float String which can be compares in a straightforward manner if they are not null), but this might change in the future.

I was not sure would BeanUtils.areEqual method be able to handle such a case and what if I include my own datatypes(non -primitives) inside this MYOBJ.

Thanks


Your Question is very vague, I don't really know what you are talking about, so I'll go in several Directions.

"compare my Objects" can mean several things. In Java, this usually means comparing them for sorting, i.e. through the Comparable / Comparator interfaces. While ObjectUtils does provide a null-safe compare method, it won't help you beyond that. What you need is either a custom Comparator or for your objects to implement Comparable. CompareToBuilder can help you with both, to a certain extent:

public class SomeBean implements Comparable<SomeBean>{

    private String foo;
    private int bar;
    private List<String> baz;

    public int compareTo(SomeBean other) {
        return new CompareToBuilder()
        .append(foo, other.foo)
        .append(bar, other.bar)
        .append(baz, other.baz)
        .toComparison();
    }

}

If, on the other hand, you want to compare the properties of different object types, then you are looking in the totally wrong direction. Have a look at Commons / BeanUtils instead. Sample code:

public class BeanUtilsTester {

    public static class Foo{
        private String foo="foo";
        public String getFoo() {return foo;}
        public void setFoo(String foo) {this.foo = foo;}
        private Integer bar=123;
        public Integer getBar() {return bar;}
        public void setBar(Integer bar) {this.bar = bar;}
        private List<String> squoggle=Arrays.asList("abc","def");
        public List<String> getSquoggle() {return squoggle;}
        public void setSquoggle(List<String> squoggle) {this.squoggle = squoggle;}
    }

    public static class Bar{
        private String foo="bar";
        public String getFoo() {return foo;}
        public void setFoo(String foo) {this.foo = foo;}
        private Integer bar=456;
        public Integer getBar() {return bar;}
        public void setBar(Integer bar) {this.bar = bar;}
        private String[] fiddle=new String[]{"abc","def"};
        public String[] getFiddle() {return fiddle;}
        public void setFiddle(String[] fiddle) {this.fiddle = fiddle;}
    }

    public static void main(String[] args) throws Exception{
        Foo foo = new Foo();
        Bar bar = new Bar();
        Map<String,Object> fooProps = BeanUtils.describe(foo);
        Map<String,Object> barProps = BeanUtils.describe(bar);
        fooProps.keySet().retainAll(barProps.keySet());
        BeanUtils.populate(bar, fooProps);
        assertEquals(foo.getFoo(),bar.getFoo());
        assertEquals(foo.getBar(), bar.getBar());
    }

}

And if you just want to implement equals() correctly, look at EqualsBuilder:

@Override
public boolean equals(Object obj) {
    if (obj instanceof SomeBean) {
        SomeBean other = (SomeBean) obj;
        return new EqualsBuilder()
                .append(foo, other.foo)
                .append(bar, other.bar)
                .append(baz, other.baz)
                .isEquals();
    }
    return false;
}


ObjectUtils will work just fine with comparing user defined objects. Of course you'll need to implement the Comparable interface in any object that you'd like to compare using the library functions.


Just made a simple test

User user1 = new User();
User user2 = new User();
Assert.assertEquals(true,ObjectUtils.equals(user1, user2)); 

The test here displays false. It's not equal.

User user1 = new User();
User user2 = user1;
Assert.assertEquals(true,ObjectUtils.equals(user1, user2));

The previous test displays true.

I believe you want to test similarly like the first test without changing the user defined class. If I am right, you can't use this library to do comparison.

However if you implement the compare() method in the every bean that you want to compare, I believe you are able to use this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜