* sorting now falls back to the IP, so equal matches are still sorted according to their IPs

git-svn-id: https://ipscan.svn.sourceforge.net/svnroot/ipscan/trunk@399 375186e5-ef17-0410-b0b6-91563547dcda
This commit is contained in:
angryziber 2008-04-13 12:22:17 +00:00
parent 434c5f3053
commit a24c2a1a11
2 changed files with 29 additions and 6 deletions

View File

@ -20,15 +20,16 @@ public class ScanningResultComparator implements Comparator<ScanningResult> {
Object val1 = r1.getValues().get(index);
Object val2 = r2.getValues().get(index);
if (val1 == val2) {
return 0;
}
if (val1 == null)
val1 = NotAvailable.VALUE;
if (val2 == null)
val2 = NotAvailable.VALUE;
int result;
if (val1 == val2) {
result = 0;
}
else
if (val1.getClass() == val2.getClass() && !(val1 instanceof String) && val1 instanceof Comparable) {
// both are the same type and Comparable
result = ((Comparable)val1).compareTo(val2);
@ -45,6 +46,11 @@ public class ScanningResultComparator implements Comparator<ScanningResult> {
}
}
if (result == 0 && index != 0) {
// if values are equal, order them according to the IPs
result = ((Comparable)r1.getValues().get(0)).compareTo(r2.getValues().get(0));
}
return result * (ascending ? 1 : -1);
}

View File

@ -12,6 +12,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import net.azib.ipscan.core.values.InetAddressHolder;
import net.azib.ipscan.core.values.IntegerWithUnit;
import net.azib.ipscan.core.values.NotAvailable;
import net.azib.ipscan.core.values.NotScanned;
@ -67,7 +68,7 @@ public class ScanningResultComparatorTest {
assertTrue(comparator.compare(res("a", "z"), res("z", "a")) > 0);
comparator.byIndex(2, false);
assertTrue(comparator.compare(res("a", "z", "mmm"), res("z", "a", "mmm")) == 0);
assertTrue(comparator.compare(res("a", "x", "mmm"), res("a", "y", "mmm")) == 0);
}
@Test
@ -89,6 +90,22 @@ public class ScanningResultComparatorTest {
assertTrue(comparator.compare(res("Z"), res("a")) > 0);
}
@Test
public void fallbackToIP() throws Exception {
Object ip1 = new InetAddressHolder(InetAddress.getByName("127.0.0.25"));
Object ip2 = new InetAddressHolder(InetAddress.getByName("127.0.0.30"));
Object ip3 = new InetAddressHolder(InetAddress.getByName("128.0.0.25"));
comparator.byIndex(1, true);
assertTrue(comparator.compare(res(ip1, "a"), res(ip1, "A")) == 0);
assertTrue(comparator.compare(res(ip1, "a"), res(ip2, "a")) < 0);
assertTrue(comparator.compare(res(ip3, "a"), res(ip2, "a")) > 0);
comparator.byIndex(1, false);
assertTrue(comparator.compare(res(ip1, null), res(ip2, null)) > 0);
assertTrue(comparator.compare(res(ip3, null), res(ip2, null)) < 0);
}
@Test
public void sortingWorksInBothDirections() throws Exception {
ScanningResult[] results = {
@ -121,7 +138,7 @@ public class ScanningResultComparatorTest {
assertEquals(NotAvailable.VALUE, results[3].getValues().get(0));
assertEquals(NotScanned.VALUE, results[5].getValues().get(0));
}
private ScanningResult res(Object ... values) throws UnknownHostException {
ScanningResult result = new ScanningResult(InetAddress.getLocalHost(), values.length);
for (int i = 0; i < values.length; i++) {