org.jgroups.protocols.relay.SiteStatus Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
package org.jgroups.protocols.relay;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Maintains the status of sites (up, down, undefined)
* @author Bela Ban
* @since 5.2.17
*/
public class SiteStatus {
public enum Status {up,down};
protected final Map sites=new HashMap<>();
/**
* Adds a set of sites to the cache. Returns a set of sites for which notifications should be emitted. For each
* site S, the following happens:
*
* - S is not present: add a new entry with the given status for S and add S to the return value
* - S is present: if S != status: change the status and add S to the return value, else no-op
*
* @param sites
* @param status
* @return
*/
public synchronized Set add(Set sites, Status status) {
Set retval=new HashSet<>();
for(String site: sites) {
Status s=this.sites.get(site);
if(s == null) {
this.sites.put(site, status);
retval.add(site);
}
else {
if(s != status) {
this.sites.put(site, status);
retval.add(site);
}
}
}
return retval;
}
public synchronized Status get(String site) {
return this.sites.get(site);
}
public synchronized SiteStatus clear() {
sites.clear();
return this;
}
public String toString() {
return sites.entrySet().stream()
.map(e -> String.format("%s: %s", e.getKey(), e.getValue())).collect(Collectors.joining("\n"));
}
}