question: private static void enhanceOrderingData ( DocumentOrderingWrapper [ ] wrappers ) throws CircularDependencyException { for ( int i = 0 ; i < wrappers . length ; i ++ ) { DocumentOrderingWrapper w = wrappers [ i ] ; for ( String id : w . getBeforeIds ( ) ) { if ( OTHERS_KEY . equals ( id ) ) { continue ; } for ( int ii = 0 ; ii < wrappers . length ; ii ++ ) { DocumentOrderingWrapper other = wrappers [ ii ] ; if ( id . equals ( other . id ) ) { String [ ] afterIds = other . getAfterIds ( ) ; if ( Arrays . binarySearch ( afterIds , w . id ) < 0 ) { Set < String > newAfterIds = new HashSet < > ( afterIds . length + 1 ) ; newAfterIds . addAll ( Arrays . asList ( afterIds ) ) ; newAfterIds . add ( w . id ) ; other . afterIds = newAfterIds . toArray ( new String [ newAfterIds . size ( ) ] ) ; Arrays . sort ( other . afterIds ) ; } String [ ] otherBeforeIds = other . getBeforeIds ( ) ; if ( otherBeforeIds . length > 0 ) { String [ ] currentBeforeIds = w . getBeforeIds ( ) ; Set < String > newBeforeIds = new HashSet < > ( ) ; newBeforeIds . addAll ( Arrays . asList ( currentBeforeIds ) ) ; for ( String bid : otherBeforeIds ) { if ( OTHERS_KEY . equals ( bid ) ) { continue ; } newBeforeIds . add ( bid ) ; } String [ ] temp = newBeforeIds . toArray ( new String [ newBeforeIds . size ( ) ] ) ; Arrays . sort ( temp ) ; if ( search ( temp , w . id ) ) { throw new CircularDependencyException ( ) ; } w . beforeIds = temp ; } } } } for ( String id : w . getAfterIds ( ) ) { if ( OTHERS_KEY . equals ( id ) ) { continue ; } for ( int ii = 0 ; ii < wrappers . length ; ii ++ ) { DocumentOrderingWrapper other = wrappers [ ii ] ; if ( id . equals ( other . id ) ) { String [ ] beforeIds = other . getBeforeIds ( ) ; if ( Arrays . binarySearch ( beforeIds , w . id ) < 0 ) { Set < String > newBeforeIds = new HashSet < > ( beforeIds . length + 1 ) ; newBeforeIds . addAll ( Arrays . asList ( beforeIds ) ) ; newBeforeIds . add ( w . id ) ; other . beforeIds = newBeforeIds . toArray ( new String [ newBeforeIds . size ( ) ] ) ; Arrays . sort ( other . beforeIds ) ; } String [ ] otherAfterIds = other . getAfterIds ( ) ; if ( otherAfterIds . length > 0 ) { String [ ] currentAfterIds = w . getAfterIds ( ) ; Set < String > newAfterIds = new HashSet < > ( ) ; newAfterIds . addAll ( Arrays . asList ( currentAfterIds ) ) ; for ( String bid : otherAfterIds ) { if ( OTHERS_KEY . equals ( bid ) ) { continue ; } newAfterIds . add ( bid ) ; } String [ ] temp = newAfterIds . toArray ( new String [ newAfterIds . size ( ) ] ) ; Arrays . sort ( temp ) ; if ( search ( temp , w . id ) ) { throw new CircularDependencyException ( ) ; } w . afterIds = temp ; } } } } } }
target:  Update before/after knowledge of all nodes in the array. Consider the case of A after B, B after C, C after D, and D with no ordering characteristics. When the code below executes, the before/after of specific nodes will be updated so that in the case of the example above we'd have: A -> after B B -> before A, after C C -> before B, A after D D -> before C, B, A So when an attempt is made to sort [A, B, C, D] the end result is [D, C, B, A] No extra enhancement of after ID information is necessary outside of the single node information due to the way the algorithm processes the array. This method also performs cyclic detection.  If, after updating the before/after information, the before/after information contains a reference to the document ID we're currently processing, throw an exception.