Index: RowRecordsAggregate.java =================================================================== RCS file: /home/cvspublic/jakarta-poi/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java,v --- RowRecordsAggregate.java 1.2 +++ RowRecordsAggregate.java @@ -62,1 +62,1 @@ -import java.util.HashMap; --- +import java.util.TreeMap; @@ -65,0 +65,1 @@ +import java.util.Comparator; @@ -69,0 +70,1 @@ + * @author Tal Moshaiov (talm@vistaportal.com) @@ -76,1 +78,1 @@ - HashMap records = null; --- + TreeMap records = null; @@ -80,1 +82,0 @@ - @@ -83,1 +84,1 @@ - records = new HashMap(); --- + records = new TreeMap(new RowRecordsComparator()); @@ -219,1 +220,49 @@ -} --- + + /** + * compares between two row records according to their row number + */ + private class RowRecordsComparator implements Comparator + { + /** + * Object equality, implemented as object identity + * @param o Object we're being compared to + * @return true if identical, else false + */ + public boolean equals(Object obj) + { + return this == obj; + } + + /** + * compare method. Checks if any of the parameters is null or not + * instances of RowRecord. + * One row is less than another if its row number is smaller than + * the other's. + * + * @param row1 first object to compare, better be a RowRecord + * @param row2 second object to compare, better be a RowRecord + * + * @return negative value if row1 < row2, + * zero if row1 == row2, + * positive value if row1 > row2. + */ + public int compare(Object row1, Object row2) { + //if any of the records is null, return not equal + if (row1 == null || row2 == null) + return -1; + //if any of the objects isn't a RowRecrod, return not equal + if (!(row2 instanceof RowRecord && row2 instanceof RowRecord)) + return -1; + //get row numbers of both row records + int row1Number = ((RowRecord)row1).getRowNumber(); + int row2Number = ((RowRecord)row2).getRowNumber(); + //compare row numbers and return result + if (row1Number == row2Number) + return 0; + else if (row1Number < row2Number) + return -1; + else + return 1; + } + }//end private class RowRecordsComparator +}