org.jgroups.util.RspList 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.util;
import org.jgroups.Address;
import java.util.*;
import java.util.stream.Collectors;
/**
* Contains responses from all members. Marks faulty members.
* A RspList is a response list used in peer-to-peer protocols. This class is unsynchronized
*/
public class RspList extends HashMap> implements Iterable> {
private static final long serialVersionUID=6085009056724212815L;
public RspList() {
}
public RspList(int size) {
super(size);
}
public RspList(Map> map) {
putAll(map);
}
/**
* Returns the value associated with address key
* @param key
* @return Object value
*/
public T getValue(Object key) {
Rsp rsp=get(key);
return rsp != null? rsp.getValue() : null;
}
public RspList addRsp(Address sender, T retval) {
Rsp rsp=get(sender);
if(rsp != null) {
rsp.setValue(retval);
return this;
}
put(sender, new Rsp<>(retval));
return this;
}
public boolean isReceived(Address sender) {
Rsp rsp=get(sender);
return rsp != null && rsp.wasReceived();
}
public int numSuspectedMembers() {
return (int)values().stream().filter(Rsp::wasSuspected).count();
}
public int numReceived() {
return (int)values().stream().filter(Rsp::wasReceived).count();
}
/** Returns the first value in the response set. This is random, but we try to return a non-null value first */
public T getFirst() {
Optional> retval=values().stream().filter(rsp -> rsp.getValue() != null).findFirst();
return retval.map(Rsp::getValue).orElse(null);
}
/**
* Returns the results from non-suspected members that are not null.
*/
public List getResults() {
return values().stream().filter(rsp -> rsp.wasReceived() && rsp.getValue() != null)
.collect(() -> new ArrayList<>(size()), (list,rsp) -> list.add(rsp.getValue()), (l,r) -> {});
}
public List getSuspectedMembers() {
return entrySet().stream().filter(entry -> entry.getValue() != null && entry.getValue().wasSuspected())
.map(Entry::getKey).collect(Collectors.toList());
}
public boolean isSuspected(Address sender) {
Rsp rsp=get(sender);
return rsp != null && rsp.wasSuspected();
}
public String toString() {
return entrySet().stream()
.collect(StringBuilder::new,
(sb,entry) -> sb.append("[").append(entry.getKey()).append(": ").append(entry.getValue()).append("]\n"),
(l,r)->{}).toString();
}
boolean contains(Address sender) {
return containsKey(sender);
}
public Iterator> iterator() {
return values().iterator();
}
}