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

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

The newest version!
// $Id: RspList.java,v 1.10 2010/01/09 11:18:47 belaban Exp $

package bboss.org.jgroups.util;


import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

import bboss.org.jgroups.Address;


/**
 * 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 {
    public static final RspList EMPTY_RSP_LIST=new RspList();

    /** Map */
    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 Object 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(); } /** * Clears the response list * @deprecated Use {@link #clear()} instead */ public void reset() { clear(); } public void addRsp(Address sender, Object retval) { Rsp rsp=get(sender); if(rsp != null) { rsp.sender=sender; rsp.retval=retval; rsp.received=true; rsp.suspected=false; 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 void addSuspect(Address sender) { Rsp rsp=get(sender); if(rsp != null) { rsp.sender=sender; rsp.retval=null; rsp.received=false; rsp.suspected=true; return; } rsps.put(sender, new Rsp(sender, true)); } 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 Object 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 Vector getResults() { Vector ret=new Vector(); Object val; for(Rsp rsp: values()) { if(rsp.wasReceived() && (val=rsp.getValue()) != null) ret.addElement(val); } return ret; } public Vector
getSuspectedMembers() { Vector
retval=new Vector
(); for(Rsp rsp: values()) { if(rsp.wasSuspected()) retval.addElement(rsp.getSender()); } return retval; } public boolean isSuspected(Address sender) { Rsp rsp=get(sender); return rsp != null && rsp.suspected; } public int size() { return rsps.size(); } /** * Returns the Rsp at index i * @param i The index * @return a Rsp * @throws ArrayIndexOutOfBoundsException * @deprecated Use {@link #entrySet()} or {@link #values()} instead */ public Object elementAt(int i) throws ArrayIndexOutOfBoundsException { Set
keys=new TreeSet
(keySet()); Object[] keys_array=keys.toArray(); Object key=keys_array[i]; return get(key); } 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); } }