public HostStatusTracker computeNewHostStatus ( Collection < Host > hostsUp , Collection < Host > hostsDown ) { verifyMutuallyExclusive ( hostsUp , hostsDown ) ; Set < Host > nextActiveHosts = new HashSet < Host > ( hostsUp ) ; // Get the hosts that are currently down Set < Host > nextInactiveHosts = new HashSet < Host > ( hostsDown ) ; // add any previous hosts that were currently down iff they are still reported by the HostSupplier Set < Host > union = new HashSet <> ( hostsUp ) ; union . addAll ( hostsDown ) ; if ( ! union . containsAll ( inactiveHosts ) ) { logger . info ( "REMOVING at least one inactive host from {} b/c it is no longer reported by HostSupplier" , inactiveHosts ) ; inactiveHosts . retainAll ( union ) ; } nextInactiveHosts . addAll ( inactiveHosts ) ; // Now remove from the total set of inactive hosts any host that is currently up. // This typically happens when a host moves from the inactive state to the active state. // And hence it will be there in the prev inactive set, and will also be there in the new active set // for this round. for ( Host host : nextActiveHosts ) { nextInactiveHosts . remove ( host ) ; } // Now add any host that is not in the new active hosts set and that was in the previous active set Set < Host > prevActiveHosts = new HashSet < Host > ( activeHosts ) ; prevActiveHosts . removeAll ( hostsUp ) ; // If anyone is remaining in the prev set then add it to the inactive set, since it has gone away nextInactiveHosts . addAll ( prevActiveHosts ) ; for ( Host host : nextActiveHosts ) { host . setStatus ( Status . Up ) ; } for ( Host host : nextInactiveHosts ) { host . setStatus ( Status . Down ) ; } return new HostStatusTracker ( nextActiveHosts , nextInactiveHosts ) ; }
Helper method that actually changes the state of the class to reflect the new set of hosts up and down Note that the new HostStatusTracker is returned that holds onto the new state . Calling classes must update their references to use the new HostStatusTracker