001: // $Id: RspList.java,v 1.7 2006/05/13 08:48:38 belaban Exp $
002:
003: package org.jgroups.util;
004:
005: import org.jgroups.Address;
006:
007: import java.util.*;
008:
009: /**
010: * Contains responses from all members. Marks faulty members.
011: * A RspList is a response list used in peer-to-peer protocols.
012: */
013: public class RspList implements Map {
014:
015: /** Map<Address, Rsp> */
016: final Map rsps = new HashMap();
017:
018: public RspList() {
019:
020: }
021:
022: /** Adds a lkist of responses
023: * @param responses Collection<Rsp>
024: */
025: public RspList(Collection responses) {
026: if (responses != null) {
027: for (Iterator it = responses.iterator(); it.hasNext();) {
028: Rsp rsp = (Rsp) it.next();
029: rsps.put(rsp.getSender(), rsp);
030: }
031: }
032: }
033:
034: public boolean isEmpty() {
035: return rsps.isEmpty();
036: }
037:
038: public boolean containsKey(Object key) {
039: return rsps.containsKey(key);
040: }
041:
042: public boolean containsValue(Object value) {
043: return rsps.containsValue(value);
044: }
045:
046: /**
047: * Returns the Rsp associated with address key
048: * @param key Address (key)
049: * @return Rsp
050: */
051: public Object get(Object key) {
052: return rsps.get(key);
053: }
054:
055: /**
056: * Returns the value associated with address key
057: * @param key
058: * @return Object value
059: */
060: public Object getValue(Object key) {
061: Rsp rsp = (Rsp) get(key);
062: return rsp != null ? rsp.getValue() : null;
063: }
064:
065: public Object put(Object key, Object value) {
066: return rsps.put(key, value);
067: }
068:
069: public Object remove(Object key) {
070: return rsps.remove(key);
071: }
072:
073: public void putAll(Map m) {
074: rsps.putAll(m);
075: }
076:
077: public void clear() {
078: rsps.clear();
079: }
080:
081: public Set keySet() {
082: return rsps.keySet();
083: }
084:
085: public Collection values() {
086: return rsps.values();
087: }
088:
089: public Set entrySet() {
090: return rsps.entrySet();
091: }
092:
093: /**
094: * Clears the response list
095: * @deprecated Use {@link #clear()} instead
096: */
097: public void reset() {
098: clear();
099: }
100:
101: public void addRsp(Address sender, Object retval) {
102: Rsp rsp = (Rsp) get(sender);
103: if (rsp != null) {
104: rsp.sender = sender;
105: rsp.retval = retval;
106: rsp.received = true;
107: rsp.suspected = false;
108: return;
109: }
110: rsps.put(sender, new Rsp(sender, retval));
111: }
112:
113: public void addNotReceived(Address sender) {
114: Rsp rsp = (Rsp) get(sender);
115: if (rsp == null)
116: rsps.put(sender, new Rsp(sender));
117: }
118:
119: public void addSuspect(Address sender) {
120: Rsp rsp = (Rsp) get(sender);
121: if (rsp != null) {
122: rsp.sender = sender;
123: rsp.retval = null;
124: rsp.received = false;
125: rsp.suspected = true;
126: return;
127: }
128: rsps.put(sender, new Rsp(sender, true));
129: }
130:
131: public boolean isReceived(Address sender) {
132: Rsp rsp = (Rsp) get(sender);
133: if (rsp == null)
134: return false;
135: return rsp.received;
136: }
137:
138: public int numSuspectedMembers() {
139: int num = 0;
140: Rsp rsp;
141: Collection values = values();
142: for (Iterator it = values.iterator(); it.hasNext();) {
143: rsp = (Rsp) it.next();
144: if (rsp.wasSuspected())
145: num++;
146: }
147: return num;
148: }
149:
150: /** Returns the first value in the response set. This is random, but we try to return a non-null value first */
151: public Object getFirst() {
152: Collection values = values();
153: for (Iterator it = values.iterator(); it.hasNext();) {
154: Rsp rsp = (Rsp) it.next();
155: if (rsp.getValue() != null)
156: return rsp.getValue();
157: }
158: return null;
159: }
160:
161: /**
162: * Returns the results from non-suspected members that are not null.
163: */
164: public Vector getResults() {
165: Vector ret = new Vector();
166: Rsp rsp;
167: Object val;
168:
169: for (Iterator it = values().iterator(); it.hasNext();) {
170: rsp = (Rsp) it.next();
171: if (rsp.wasReceived() && (val = rsp.getValue()) != null)
172: ret.addElement(val);
173: }
174: return ret;
175: }
176:
177: public Vector getSuspectedMembers() {
178: Vector retval = new Vector();
179: Rsp rsp;
180:
181: for (Iterator it = values().iterator(); it.hasNext();) {
182: rsp = (Rsp) it.next();
183: if (rsp.wasSuspected())
184: retval.addElement(rsp.getSender());
185: }
186: return retval;
187: }
188:
189: public boolean isSuspected(Address sender) {
190: Rsp rsp = (Rsp) get(sender);
191: if (rsp == null)
192: return false;
193: return rsp.suspected;
194: }
195:
196: public int size() {
197: return rsps.size();
198: }
199:
200: /**
201: * Returns the Rsp at index i
202: * @param i The index
203: * @return a Rsp
204: * @throws ArrayIndexOutOfBoundsException
205: * @deprecated Use {@link #entrySet()} or {@link #values()} instead
206: */
207: public Object elementAt(int i)
208: throws ArrayIndexOutOfBoundsException {
209: Set keys = new TreeSet(keySet());
210: if (keys == null)
211: return null;
212: Object[] keys_array = keys.toArray();
213: Object key = keys_array[i];
214: return get(key);
215: }
216:
217: public String toString() {
218: StringBuffer ret = new StringBuffer();
219: Rsp rsp;
220:
221: for (Iterator it = values().iterator(); it.hasNext();) {
222: rsp = (Rsp) it.next();
223: ret.append("[" + rsp + "]\n");
224: }
225: return ret.toString();
226: }
227:
228: boolean contains(Address sender) {
229: return containsKey(sender);
230: }
231:
232: }
|