001: /*
002: * <copyright>
003: *
004: * Copyright 2003-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.lib.aggagent.query;
027:
028: import java.io.Serializable;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.LinkedList;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.TreeMap;
035:
036: import org.cougaar.lib.aggagent.session.XmlTransferable;
037: import org.cougaar.lib.aggagent.util.InverseSax;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.NodeList;
040:
041: // import org.cougaar.lib.aggagent.util.XmlUtils;
042:
043: public class ResultSetDataAtom implements XmlTransferable, Serializable {
044: public static String DATA_ATOM_TAG = "data_atom";
045: private static String ID_TAG = "id";
046: private static String VALUE_TAG = "value";
047: private static String NAME_ATT = "name";
048: private static String VALUE_ATT = "value";
049:
050: private Object lock = new Serializable() {
051: };
052:
053: // the aggregation of all the identifiers defines how these values
054: // should be managed. Sample identifiers: "org", "time", "metric", "item"
055: // A TreeMap is used here to guarantee that order is preserved
056: private Map identifiers = new TreeMap();
057:
058: // data payload for this managed set of data
059: private Map values = new HashMap();
060:
061: public ResultSetDataAtom() {
062: }
063:
064: public ResultSetDataAtom(ResultSetDataAtom da) {
065: identifiers = (Map) ((TreeMap) da.identifiers).clone();
066: values = (Map) ((HashMap) da.values).clone();
067: }
068:
069: private static Map makeIdMap(List l, CompoundKey k) {
070: Map ret = new TreeMap();
071: Iterator lit = l.iterator();
072: Iterator kit = k.getKeys();
073: while (kit.hasNext() && lit.hasNext())
074: ret.put(lit.next(), kit.next());
075:
076: return ret;
077: }
078:
079: public ResultSetDataAtom(List l, CompoundKey k) {
080: this (l, k, new HashMap());
081: }
082:
083: public ResultSetDataAtom(List l, CompoundKey k, Map vals) {
084: identifiers = makeIdMap(l, k);
085: values = vals;
086: }
087:
088: public ResultSetDataAtom(Element root) {
089: NodeList nl = root.getElementsByTagName(ID_TAG);
090: for (int i = 0; i < nl.getLength(); i++) {
091: Element e = (Element) nl.item(i);
092: addIdentifier(e.getAttribute(NAME_ATT), e
093: .getAttribute(VALUE_ATT));
094: }
095: nl = root.getElementsByTagName(VALUE_TAG);
096: for (int i = 0; i < nl.getLength(); i++) {
097: Element e = (Element) nl.item(i);
098: addValue(e.getAttribute(NAME_ATT), e
099: .getAttribute(VALUE_ATT));
100: }
101: }
102:
103: /**
104: * used to gain access to data payload (or to split the atom for storage)
105: */
106: public Map getValueMap() {
107: return values;
108: }
109:
110: /**
111: * used to set data payload (or to recombine the atom for retrieval)
112: */
113: public void setValueMap(Map newMap) {
114: values = newMap;
115: }
116:
117: public void addValue(Object name, Object value) {
118: synchronized (lock) {
119: values.put(name, value);
120: }
121: }
122:
123: public Object removeValue(Object name) {
124: synchronized (lock) {
125: return values.remove(name);
126: }
127: }
128:
129: public Object getValue(Object name) {
130: synchronized (lock) {
131: return values.get(name);
132: }
133: }
134:
135: public Iterator getValueNames() {
136: synchronized (lock) {
137: return new LinkedList(values.keySet()).iterator();
138: }
139: }
140:
141: public void addIdentifier(Object idName, Object idValue) {
142: synchronized (lock) {
143: identifiers.put(idName, idValue);
144: }
145: }
146:
147: public Object removeIdentifier(Object idName) {
148: synchronized (lock) {
149: return identifiers.remove(idName);
150: }
151: }
152:
153: public Object getIdentifier(Object idName) {
154: synchronized (lock) {
155: return identifiers.get(idName);
156: }
157: }
158:
159: public Iterator getIdentifierNames() {
160: synchronized (lock) {
161: return new LinkedList(identifiers.keySet()).iterator();
162: }
163: }
164:
165: public CompoundKey getKey(List l) {
166: return new CompoundKey(l, identifiers);
167: }
168:
169: public String toString() {
170: return toXml();
171: }
172:
173: public String toXml() {
174: InverseSax doc = new InverseSax();
175: includeXml(doc);
176: return doc.toString();
177: }
178:
179: public void includeXml(InverseSax doc) {
180: synchronized (lock) {
181: doc.addElement(DATA_ATOM_TAG);
182: addNameValueTags(ID_TAG, identifiers.entrySet().iterator(),
183: doc);
184: addNameValueTags(VALUE_TAG, values.entrySet().iterator(),
185: doc);
186: doc.endElement();
187: }
188: }
189:
190: private void addNameValueTags(String tag, Iterator i, InverseSax doc) {
191: while (i.hasNext()) {
192: Map.Entry me = (Map.Entry) i.next();
193: doc.addElement(tag);
194: doc.addAttribute(NAME_ATT, me.getKey().toString());
195: doc.addAttribute(VALUE_ATT, me.getValue().toString());
196: doc.endElement();
197: }
198: }
199:
200: /**
201: * Useful for debugging.
202: */
203: public void summarize() {
204: System.out.println("ResultSetDataAtom: summary:");
205: formatMap("identifiers", identifiers);
206: formatMap("values", values);
207: System.out.println("ResultSetDataAtom: done.");
208: }
209:
210: private static void formatMap(String name, Map m) {
211: System.out.print(" -> " + name);
212: if (m.size() == 0)
213: System.out.print(" <NONE>");
214: System.out.println();
215: for (Iterator i = m.entrySet().iterator(); i.hasNext();) {
216: Map.Entry me = (Map.Entry) i.next();
217: System.out.println(" - -> " + me.getKey() + " = "
218: + me.getValue());
219: }
220: }
221: }
|