Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 8
Next »
- Using the Apache commons lang is the recommended way to do object comparisons (e.g. equals) in Sakai tools
- Adding the commons-lang dependency to your project.xml
- Add the following to the dependencies block of your project.xml file to include the commons-lang:
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${sakai.commons.lang.version}</version>
</dependency>
- The commons-lang package is included in shared/lib by Sakai itself so you do not need to package it in your tool war file (hence there is no <war.bundle>true</war.bundle>)
- Do not specify a version, use the version that Sakai is placing in the shared/lib by using the property (sakai.commons.lang.version) as shown above
- Using the lang functions in your object or object implementation code - Apache commons-lang API
Note: All code below should go in the object or object implementation
- Add the required imports
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
- Create an equals function using the EqualsBuilder
public boolean equals(Object obj) {
if (null == obj) return false;
if (obj instanceof org.sakaiproject.tool.mytool.MyObject == false) {
return false;
}
if (this == obj) { return true; }
org.sakaiproject.tool.mytool.MyObject castObj = (org.sakaiproject.tool.mytool.MyObject) obj;
return new EqualsBuilder()
.append(this.getId(), castObj.getId())
.isEquals();
}
- This simple example only compares the id values of the 2 objects, however you could compare many values by doing something like this:
return new EqualsBuilder()
.append(this.field1, castObj.getField1())
.append(this.field2, castObj.getField2())
.append(this.field3, castObj.getField3())
.isEquals();
- Create a hashCode function using the HashCodeBuilder
public int hashCode() {
// pick 2 hard-coded, odd, >0 ints as args
return new HashCodeBuilder(1, 31)
.append(this.id)
.toHashCode();
}
- Like the previous example, this only uses the id for creating the hashCode but you can easy append more properties
- Create a compareTo function using the CompareToBuilder
public int compareTo(Object obj) {
org.sakaiproject.tool.mytool.MyObject castObj = (org.sakaiproject.tool.mytool.MyObject) obj;
return new CompareToBuilder()
.append(this.getId(), castObj.getId())
.toComparison();
}
- Like the previous examples, this only uses the id for comparison but you can easy append more properties
- Create a toString function using the ToStringBuilder
public String toString() {
return new ToStringBuilder(this)
.append(this.id)
.toString();
}
- Like the previous examples, this only uses the id for comparison but you can easy append more properties
- Your object should now be comparable to objects of the same type that have the same data
- A sample of an object which demostrates use of the commons-lang builders: TaskImpl.java