Commits

timemachine committed 1327923

Simplify SchedulerComparator logic.

  • Participants
  • Parent commits 50ec84d

Comments (0)

Files changed (1)

timemachine-scheduler/src/main/java/timemachine/scheduler/support/ScheduleComparator.java

  */
 public class ScheduleComparator implements Comparator<Schedule>, Serializable {
 	private static final long serialVersionUID = 1L;
-	private static final int EQUAL = 0; // Do not move
-	private static final int LEFT = -1; // Move to top (first)
-	private static final int RIGHT = 1; // Move to bottom (last)
-	
-	/** We use and depend on the Enum position (ordinal) value for ordering defined in enum class! */
-	private int compareState(State left, State right) {
-		int result;
-		if (left == null && right == null)
-			result = EQUAL;
-		else if (left == null && right != null)
-			result = RIGHT;
-		else if (left != null && right == null)
-			result = LEFT;
-		else
-			result = left.ordinal() - right.ordinal();
-		return result;
-	}
-	
-	/** This is used to order both nextRun and lastModified dates. */
-	private int compareDate(Date left, Date right) {
-		int result;
-		if (left == null && right == null)
-			result = EQUAL;
-		else if (left == null && right != null)
-			result = RIGHT;
-		else if (left != null && right == null)
-			result = LEFT;
-		else
-			result = left.compareTo(right);
-		return result;
-	}
-	
-	/** Order by priority value 1 to 9. The lower number the higher priority. */
-	private int comparePriority(int left, int right) {
-		return left - right;
-	}
-	
-	private int compareId(Long left, Long right) {
-		int result;
-		if (left == null && right == null)
-			result = EQUAL;
-		else if (left == null && right != null)
-			result = RIGHT;
-		else if (left != null && right == null)
-			result = LEFT;
-		else
-			result = left.compareTo(right);
-		return result;
-	}
-	
-	/** This is used to ordering both delete and pause fields. We wnat both of these as invert sorting, meaning
-	 * they should go to the bottom (last) if it's true. */
-	private int compareBoolean(boolean left, boolean right) {
-		int result;
-		if (!left && right)
-			result = LEFT;
-		else if (left && !right)
-			result = RIGHT;
-		else
-			result = EQUAL;
-		return result;
-	}
-	
+
 	/** Compare two Schedule instances and return ordering int value. */
 	@Override
 	public int compare(Schedule left, Schedule right) {
-		if (left.equals(right))
-			return EQUAL;
-		
-		int result;
-		result = compareBoolean(left.isDeleted(), right.isDeleted());
-		if (result == 0) {
-			result = compareBoolean(left.isPaused(), right.isPaused());
-			if (result == 0) {
-				result = compareState(left.getState(), right.getState());
-				if (result == 0) {
-					result = compareDate(left.getNextRun(), right.getNextRun());
-					if (result == 0) {
-						result = comparePriority(left.getPriority(), right.getPriority());
-						if (result == 0) {
-							result = compareDate(left.getLastModified(), right.getLastModified());
-							if (result == 0) {
-								result = compareId(left.getId(), right.getId());
-							}
-						}
-					}
-				}
-			}
-		}
-		return result;
+        int result;
+
+        if (left.equals(right))
+            return 0;
+
+        if (left.isDeleted() && !right.isDeleted())
+            return 1;
+        if (!left.isDeleted() && right.isDeleted())
+            return -1;
+
+        if (left.isPaused() && !right.isPaused())
+            return 1;
+        if (!left.isPaused() && right.isPaused())
+            return -1;
+
+        result = compareNull(left.getState(), right.getState());
+        if (result != 0)
+            return result;
+        result = left.getState().ordinal() - right.getState().ordinal();
+        if (result != 0)
+            return result;
+
+        result = compareNull(left.getNextRun(), right.getNextRun());
+        if (result != 0)
+            return result;
+        result = left.getNextRun().compareTo(right.getNextRun());
+        if (result != 0)
+            return result;
+
+        result = left.getPriority() - right.getPriority();
+        if (result != 0)
+            return result;
+
+        result = compareNull(left.getLastModified(), right.getLastModified());
+        if (result != 0)
+            return result;
+        result = left.getLastModified().compareTo(right.getLastModified());
+        if (result != 0)
+            return result;
+
+        Long leftId = left.getId();
+        Long rightId = right.getId();
+        result = compareNull(leftId, rightId);
+        if (result != 0)
+            return result;
+        if (leftId > rightId)
+            return 1;
+        if (leftId < rightId)
+            return -1;
+
+        return 0; // they are same.
 	}
-	
-	/** Check this instance of comparator equals to other comparator or not. 
-	 * NOTE: This is not the same as the Schedule.equals, which is used in comparator() method. */
-	@Override
-	public boolean equals(Object obj) {
-		if (!(obj instanceof ScheduleComparator))
-			return false;
-		
-		// Compare by default using hashCode
-		return this.hashCode() == obj.hashCode();
-	}
-	
-	/** Call super.hashCode() just to satisfy Java contract when overriding equals(). */
-	@Override
-	public int hashCode() {
-		return super.hashCode();
-	}
+
+    /** Compare null value of two objects. non-null is more important than null. */
+    private int compareNull(Object left, Object right) {
+        if (left != null && right == null)
+            return -1;
+        if (left == null && right != null)
+            return 1;
+        return 0; // Both are nulls or non-nulls, and we consider them equals.
+    }
 }