All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jgroups.util.RspList Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version

package org.jgroups.util;


import org.jgroups.Address;

import java.util.*;


/**
 * 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 implements Map>, Iterable> {
    final Map> rsps=new HashMap<>();


    public RspList() {

    }

    /** Adds a list of responses
     * @param responses Collection
     */
    public RspList(Collection> responses) {
        if(responses != null) {
            for(Rsp rsp: responses) {
                rsps.put(rsp.getSender(), rsp);
            }
        }
    }


    public boolean isEmpty() {
        return rsps.isEmpty();
    }

    public boolean containsKey(Object key) {
        return rsps.containsKey(key);
    }

    public boolean containsValue(Object value) {
        return rsps.containsValue(value);
    }

    /**
     * Returns the Rsp associated with address key
     * @param key Address (key)
     * @return Rsp
     */
    public Rsp get(Object key) {
        return rsps.get(key);
    }

    /**
     * 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 Rsp put(Address key, Rsp value) {
        return rsps.put(key, value);
    }

    public Rsp remove(Object key) {
        return rsps.remove(key);
    }

    public void putAll(Map> m) {
        rsps.putAll(m);
    }

    public void clear() {
        rsps.clear();
    }

    public Set
keySet() { return rsps.keySet(); } public Collection> values() { return rsps.values(); } public Set>> entrySet() { return rsps.entrySet(); } public void addRsp(Address sender, T retval) { Rsp rsp=get(sender); if(rsp != null) { rsp.setValue(retval); return; } rsps.put(sender, new Rsp<>(sender, retval)); } public void addNotReceived(Address sender) { Rsp rsp=get(sender); if(rsp == null) rsps.put(sender, new Rsp(sender)); } public boolean isReceived(Address sender) { Rsp rsp=get(sender); return rsp != null && rsp.received; } public int numSuspectedMembers() { int num=0; Collection> values=values(); for(Rsp rsp: values) { if(rsp.wasSuspected()) num++; } return num; } public int numReceived() { int num=0; Collection> values=values(); for(Rsp rsp: values) { if(rsp.wasReceived()) num++; } return num; } /** Returns the first value in the response set. This is random, but we try to return a non-null value first */ public T getFirst() { Collection> values=values(); for(Rsp rsp: values) { if(rsp.getValue() != null) return rsp.getValue(); } return null; } /** * Returns the results from non-suspected members that are not null. */ public List getResults() { List ret=new ArrayList<>(size()); T val; for(Rsp rsp: values()) { if(rsp.wasReceived() && (val=rsp.getValue()) != null) ret.add(val); } return ret; } public List
getSuspectedMembers() { List
retval=new ArrayList<>(); for(Rsp rsp: values()) { if(rsp.wasSuspected()) retval.add(rsp.getSender()); } return retval; } public boolean isSuspected(Address sender) { Rsp rsp=get(sender); return rsp != null && rsp.suspected; } public int size() { return rsps.size(); } public String toString() { StringBuilder ret=new StringBuilder(); for(Rsp rsp: values()) { ret.append("[" + rsp + "]\n"); } return ret.toString(); } boolean contains(Address sender) { return containsKey(sender); } public Iterator> iterator() { return rsps.values().iterator(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy