Implemented the Comparable interface

Former-commit-id: 07d5fd34d76c296cfc2c1f049b285bbd9b05189a
This commit is contained in:
Jeremy Long
2013-04-11 06:05:58 -04:00
parent 9e79e9efb6
commit d7744537ae
3 changed files with 39 additions and 3 deletions

View File

@@ -23,7 +23,7 @@ package org.owasp.dependencycheck.dependency;
*
* @author Jeremy Long (jeremy.long@gmail.com)
*/
public class Evidence {
public class Evidence implements Comparable<Evidence> {
/**
* The confidence that the evidence is "high" quality.
@@ -222,4 +222,29 @@ public class Evidence {
private boolean testEquality(String l, String r) {
return l == null ? r == null : l.equalsIgnoreCase(r);
}
/**
* Implementation of the comparable interface.
* @param o the evidence being compared
* @return an integer indicating the ordering of the two objects
*/
public int compareTo(Evidence o) {
if (source.equals(o.source)) {
if (name.equals(o.name)) {
if (value.equals(o.value)) {
if (confidence.equals(o.confidence)) {
return 0; //they are equal
} else {
return confidence.compareTo(o.confidence);
}
} else {
return value.compareToIgnoreCase(o.value);
}
} else {
return name.compareToIgnoreCase(o.name);
}
} else {
return source.compareToIgnoreCase(o.source);
}
}
}

View File

@@ -21,6 +21,7 @@ package org.owasp.dependencycheck.dependency;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import org.owasp.dependencycheck.utils.Filter;
/**
@@ -103,7 +104,7 @@ public class EvidenceCollection implements Iterable<Evidence> {
* Creates a new EvidenceCollection.
*/
public EvidenceCollection() {
list = new HashSet<Evidence>();
list = new TreeSet<Evidence>();
weightedStrings = new HashSet<String>();
}

View File

@@ -22,7 +22,7 @@ package org.owasp.dependencycheck.dependency;
*
* @author Jeremy Long (jeremy.long@gmail.com)
*/
public class Identifier {
public class Identifier implements Comparable<Identifier> {
/**
* Constructs a new Identifier with the specified data.
@@ -165,4 +165,14 @@ public class Identifier {
return hash;
}
/**
* Implementation of the comparator interface. This compares the
* value of the identifier only.
*
* @param o the object being compared
* @return an integer indicating the ordering
*/
public int compareTo(Identifier o) {
return this.value.compareTo(o.value);
}
}