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;
          }
        }
      }
    }
  }
}

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 ; } } } } } }
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 .
