package com.onaro.sanscreen.client.view.changes; import com.onaro.sanscreen.server.changes.*; import java.util.*; /** * Determines the sorting order of a list of {@link ChangeGroup} objects. */ public class GroupComparator extends ChangeComparator{ /** * Compares two change groups. The fields in the two groups are compared in * the following order:
    *
  1. Time *
  2. Change type - The order of types is determined using the CHANGE_TYPE_SORT_ORDER map *
  3. Source device and sub device *
  4. Destination device *
  5. Volume *
  6. Violation *
* * @param c1 the first compared group * @param c2 the second compared group * @return a negative integer, zero, or a positive integer as the * first argument is less than, equal to, or greater than the * second */ public int compare(Change c1, Change c2) { assert c1 instanceof ChangeGroup : "First argument not a ChangeGroup but a " + c1.getClass().getName(); //$NON-NLS-1$ ChangeGroup group1 = (ChangeGroup) c1; assert c2 instanceof ChangeGroup : "Second argument not a ChangeGroup but a " + c2.getClass().getName(); //$NON-NLS-1$ ChangeGroup group2 = (ChangeGroup) c2; //change groups should be ordered in a descending order of the time. long timeCompare = group2.getTime() - group1.getTime(); if (timeCompare != 0) { //convert the long to int without the risk of overflow return timeCompare > 0 ? 1 : -1; } assert group1.getType() != null: "group1.getType() is null"; //$NON-NLS-1$ assert group2.getType() != null: "group2.getType() is null"; //$NON-NLS-1$ int typeCompare = compareChangeTypes(group1.getType(), group2.getType()); if (typeCompare != 0) { return typeCompare; } int srcDevCompare = compareDevices(group1.getValue(SRC_MAIN_DEVICE_NAME), group1.getValue(SRC_MAIN_DEVICE_DEVICE_TYPE),group2.getValue(ChangeConstants.SRC_MAIN_DEVICE + ChangeConstants.NAME),group2.getValue(ChangeConstants.SRC_MAIN_DEVICE + ChangeConstants.DEVICE_TYPE)); if (srcDevCompare != 0) { return srcDevCompare; } int srcSubDevCompare = compareDevices(group1.getValue(SRC_SEC_DEVICE_NAME), group1.getValue(SRC_SEC_DEVICE_DEVICE_TYPE), group2.getValue(SRC_SEC_DEVICE_NAME), group2.getValue(SRC_SEC_DEVICE_DEVICE_TYPE)); if (srcSubDevCompare != 0) { return srcSubDevCompare; } int destDevCompare = compareDevices(group1.getValue(DEST_MAIN_DEVICE_NAME), group1.getValue(DEST_MAIN_DEVICE_DEVICE_TYPE),group2.getValue(DEST_MAIN_DEVICE_NAME), group2.getValue(DEST_MAIN_DEVICE_DEVICE_TYPE)); if (destDevCompare != 0) { return destDevCompare; } int volumeCompare = compareVolumes(group1.getValue(VOLUME_NAME), group2.getValue(VOLUME_NAME)); if (volumeCompare != 0) { return volumeCompare; } int propertyCompare = compareStrings(group1.getValue(ChangeConstants.PROPERTY_NAME), group2.getValue(ChangeConstants.PROPERTY_NAME)); if (propertyCompare != 0) { return propertyCompare; } int oldCompare = compareStrings(group1.getValue(ChangeConstants.OLD_VALUE), group2.getValue(ChangeConstants.OLD_VALUE)); if (oldCompare != 0) { return oldCompare; } int strComp = compareStrings(group1.getValue(ChangeConstants.NEW_VALUE), group2.getValue(ChangeConstants.NEW_VALUE)); if (strComp != 0) { return strComp; } return compareLists(group1.getChanges(), group2.getChanges()); } /** * Compares the content of the groups. * @param changes1 content of the first group * @param changes2 content of the second group * @return first compare the size of the lists. If the size is the same will compare each of the members and return the result */ public int compareLists(List changes1, List changes2){ assert changes1 != null: "changes1 is null"; //$NON-NLS-1$ assert changes2 != null: "changes2 is null"; //$NON-NLS-1$ int compSize = changes1.size()-changes2.size(); if(compSize != 0){ return compSize; } Iterator itr1 = changes1.iterator(); Iterator itr2 = changes2.iterator(); while(itr1.hasNext()){ int tmpCompare = super.compare(itr1.next(), itr2.next()); if(tmpCompare != 0){ return tmpCompare; } } return 0; } }