@ SuppressWarnings ( "unchecked" ) public static < T > T cleanse ( T list , T element ) { if ( list == null || list == element ) { return null ; } else if ( list instanceof Pair ) { Pair < T , T > pair = ( Pair < T , T > ) list ; if ( pair . first == element ) { return pair . second ; } else if ( pair . second == element ) { return pair . first ; } else if ( pair . second instanceof Pair ) { pair . second = cleanse ( pair . second , element ) ; } } return list ; }
Cleanses an isomorphic list of a particular element . Only one thread should be updating the list at a time .