dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #24528
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12040: Modified equals on BaseIdentifiableObject and BaseNameableObject to use isAssignableFrom for clas...
------------------------------------------------------------
revno: 12040
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-09-11 16:41:15 +0200
message:
Modified equals on BaseIdentifiableObject and BaseNameableObject to use isAssignableFrom for class comparison and get-methods for property comparison, in order to handle proxied objects coming from Hibernate.
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java
dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/BaseAnalyticalObjectTest.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java 2013-09-11 13:02:54 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseIdentifiableObject.java 2013-09-11 14:41:15 +0000
@@ -336,13 +336,16 @@
@Override
public int hashCode()
{
- int result = uid != null ? uid.hashCode() : 0;
- result = 31 * result + (code != null ? code.hashCode() : 0);
- result = 31 * result + (name != null ? name.hashCode() : 0);
+ int result = getUid() != null ? getUid().hashCode() : 0;
+ result = 31 * result + (getCode() != null ? getCode().hashCode() : 0);
+ result = 31 * result + (getName() != null ? getName().hashCode() : 0);
return result;
}
+ /**
+ * Class check uses isAssignableFrom and get-methods to handle proxied objects.
+ */
@Override
public boolean equals( Object o )
{
@@ -351,24 +354,29 @@
return true;
}
- if ( o == null || getClass() != o.getClass() )
- {
- return false;
- }
-
- final BaseIdentifiableObject that = (BaseIdentifiableObject) o;
-
- if ( uid != null ? !uid.equals( that.uid ) : that.uid != null )
- {
- return false;
- }
-
- if ( code != null ? !code.equals( that.code ) : that.code != null )
- {
- return false;
- }
-
- if ( name != null ? !name.equals( that.name ) : that.name != null )
+ if ( o == null )
+ {
+ return false;
+ }
+
+ if ( !getClass().isAssignableFrom( o.getClass() ) )
+ {
+ return false;
+ }
+
+ final BaseIdentifiableObject other = (BaseIdentifiableObject) o;
+
+ if ( getUid() != null ? !getUid().equals( other.getUid() ) : other.getUid() != null )
+ {
+ return false;
+ }
+
+ if ( getCode() != null ? !getCode().equals( other.getCode() ) : other.getCode() != null )
+ {
+ return false;
+ }
+
+ if ( getName() != null ? !getName().equals( other.getName() ) : other.getName() != null )
{
return false;
}
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java 2013-09-11 13:02:54 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/BaseNameableObject.java 2013-09-11 14:41:15 +0000
@@ -97,11 +97,14 @@
public int hashCode()
{
int result = super.hashCode();
- result = 31 * result + (shortName != null ? shortName.hashCode() : 0);
- result = 31 * result + (description != null ? description.hashCode() : 0);
+ result = 31 * result + (getShortName() != null ? getShortName().hashCode() : 0);
+ result = 31 * result + (getDescription() != null ? getDescription().hashCode() : 0);
return result;
}
+ /**
+ * Class check uses isAssignableFrom and get-methods to handle proxied objects.
+ */
@Override
public boolean equals( Object o )
{
@@ -110,7 +113,12 @@
return true;
}
- if ( o == null || getClass() != o.getClass() )
+ if ( o == null )
+ {
+ return false;
+ }
+
+ if ( !getClass().isAssignableFrom( o.getClass() ) )
{
return false;
}
@@ -120,14 +128,14 @@
return false;
}
- BaseNameableObject that = (BaseNameableObject) o;
+ final BaseNameableObject other = (BaseNameableObject) o;
- if ( shortName != null ? !shortName.equals( that.shortName ) : that.shortName != null )
+ if ( getShortName() != null ? !getShortName().equals( other.getShortName() ) : other.getShortName() != null )
{
return false;
}
- if ( description != null ? !description.equals( that.description ) : that.description != null )
+ if ( getDescription() != null ? !getDescription().equals( other.getDescription() ) : other.getDescription() != null )
{
return false;
}
=== modified file 'dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/BaseAnalyticalObjectTest.java'
--- dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/BaseAnalyticalObjectTest.java 2013-08-23 15:56:19 +0000
+++ dhis-2/dhis-api/src/test/java/org/hisp/dhis/common/BaseAnalyticalObjectTest.java 2013-09-11 14:41:15 +0000
@@ -33,7 +33,9 @@
import java.util.List;
import java.util.Map;
+import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementGroup;
+import org.hisp.dhis.dataset.DataSet;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -110,4 +112,40 @@
assertEquals( "b1", BaseAnalyticalObject.getIdentifer( new ArrayList<NameableObject>(), row ) );
assertEquals( "b1", BaseAnalyticalObject.getIdentifer( null, row ) );
}
+
+ @Test
+ public void testEquals()
+ {
+ DataElement deA = new DataElement();
+ deA.setUid( "A" );
+ deA.setCode( "A" );
+ deA.setName( "A" );
+
+ DataElement deB = new DataElement();
+ deB.setUid( "B" );
+ deB.setCode( "B" );
+ deB.setName( "B" );
+
+ DataElement deC = new DataElement();
+ deC.setUid( "A" );
+ deC.setCode( "A" );
+ deC.setName( "A" );
+
+ DataSet dsA = new DataSet();
+ dsA.setUid( "A" );
+ dsA.setCode( "A" );
+ dsA.setName( "A" );
+
+ DataSet dsD = new DataSet();
+ dsD.setUid( "D" );
+ dsD.setCode( "D" );
+ dsD.setName( "D" );
+
+ assertTrue( deA.equals( deC ) );
+
+ assertFalse( deA.equals( deB ) );
+ assertFalse( deA.equals( dsA ) );
+ assertFalse( deA.equals( dsD ) );
+ assertFalse( dsA.equals( dsD ) );
+ }
}