public static List < BlockingState > sortedCopy ( final Iterable < BlockingState > blockingStates ) { final List < BlockingState > blockingStatesSomewhatSorted = Ordering . < BlockingState > natural ( ) . immutableSortedCopy ( blockingStates ) ; final List < BlockingState > result = new LinkedList < BlockingState > ( ) ; // Make sure same-day transitions are always returned in the same order depending on their attributes final Iterator < BlockingState > iterator = blockingStatesSomewhatSorted . iterator ( ) ; BlockingState prev = null ; while ( iterator . hasNext ( ) ) { final BlockingState current = iterator . next ( ) ; if ( iterator . hasNext ( ) ) { final BlockingState next = iterator . next ( ) ; if ( prev != null && current . getEffectiveDate ( ) . equals ( next . getEffectiveDate ( ) ) && current . getBlockedId ( ) . equals ( next . getBlockedId ( ) ) && ! current . getService ( ) . equals ( next . getService ( ) ) ) { // Same date, same blockable id, different services (for same-service events, trust the total ordering) // Make sure block billing transitions are respected first BlockingState prevCandidate = insertTiedBlockingStatesInTheRightOrder ( result , current , next , prev . isBlockBilling ( ) , current . isBlockBilling ( ) , next . isBlockBilling ( ) ) ; if ( prevCandidate == null ) { // Then respect block entitlement transitions prevCandidate = insertTiedBlockingStatesInTheRightOrder ( result , current , next , prev . isBlockEntitlement ( ) , current . isBlockEntitlement ( ) , next . isBlockEntitlement ( ) ) ; if ( prevCandidate == null ) { // And finally block changes transitions prevCandidate = insertTiedBlockingStatesInTheRightOrder ( result , current , next , prev . isBlockChange ( ) , current . isBlockChange ( ) , next . isBlockChange ( ) ) ; if ( prevCandidate == null ) { // Trust the current sorting result . add ( current ) ; result . add ( next ) ; prev = next ; } else { prev = prevCandidate ; } } else { prev = prevCandidate ; } } else { prev = prevCandidate ; } } else { result . add ( current ) ; result . add ( next ) ; prev = next ; } } else { // End of the list result . add ( current ) ; } } return result ; }
Ordering is critical here especially for Junction