01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.web.axis.blackboardCount;
28:
29: import java.util.Iterator;
30: import java.util.HashMap;
31: import java.util.Map;
32:
33: /**
34: * A table of "classname -> int" data returned by
35: * {@link BlackboardCount#getBlackboardCount}.
36: * <p>
37: * The Axis BeanSerializer does not directly support {@link Map}s,
38: * so this is a wrapper.
39: */
40: public final class ResultMap {
41:
42: private Map m;
43:
44: /** constructor for bean deserializer */
45: public ResultMap() {
46: }
47:
48: /** constructor for our plugin */
49: public ResultMap(Map m) {
50: this .m = m;
51: }
52:
53: /** get the object, avoid bean serializer "get*" method. */
54: public Map toMap() {
55: return m;
56: }
57:
58: public ResultEntry[] getEntries() {
59: // convert to entries, since the bean serializer doesn't
60: // understand Maps
61: int n = (m == null ? 0 : m.size());
62: ResultEntry[] entries = new ResultEntry[n];
63: if (n > 0) {
64: int j = 0;
65: Iterator iter = m.entrySet().iterator();
66: for (int i = 0; i < n; i++) {
67: Map.Entry me = (Map.Entry) iter.next();
68: Object key = me.getKey();
69: Object value = me.getValue();
70: if (key instanceof Class) {
71: key = ((Class) key).getName();
72: }
73: if (key instanceof String && value instanceof Number) {
74: entries[j++] = new ResultEntry((String) key,
75: ((Number) value).intValue());
76: }
77: }
78: }
79: return entries;
80: }
81:
82: public void setEntries(ResultEntry[] entries) {
83: // convert to map
84: int n = (entries == null ? 0 : entries.length);
85: m = new HashMap(n);
86: for (int i = 0; i < n; i++) {
87: ResultEntry re = (ResultEntry) entries[i];
88: if (re == null) {
89: continue;
90: }
91: m.put(re.getClassname(), new Integer(re.getCount()));
92: }
93: }
94:
95: public String toString() {
96: return "(ResultMap entries=" + m + ")";
97: }
98: }
|