001: package com.jamonapi;
002:
003: import java.util.*;
004:
005: /** Class that can be used as a composite key for MonitorFactor.add(compositeKey, 100) method calls
006: * Note the passed in LinkedHashMap is used as a key to another Map that looks up the associated monitor.
007: * Sun java docs says this regarding keys in Maps: "Note: great care must be exercised if mutable objects
008: * are used as map keys. The behavior of a map is not specified if the value of an
009: * object is changed in a manner that affects equals comparisons while the object is a key in the map."
010: * So once a value is placed in the LinkedHashMap is passed to the MonKeyBase
011: * constructor it should not be changed.
012: *
013: */
014: public class MonKeyBase implements MonKey {
015:
016: /**
017: * A key implmentation for label, and units type monitors.
018: */
019:
020: private Map keyMap;
021: private String rangeKeyStr;
022: private Object details;
023:
024: /** Calls the other constructor. The keyMap will be used to create the range name */
025: public MonKeyBase(LinkedHashMap keyMap) {
026: this (null, keyMap);
027: }
028:
029: /** The LinkHashMap will contain key value pairs. For example: fn=steve, ln=souza, age=44. LinkHashMap is used
030: * as the insertion order needs to be retained for displaying header, and data. The rangeKeyString is used
031: * to look up an associated range should one exist */
032: public MonKeyBase(String rangeKeyStr, LinkedHashMap keyMap) {
033: this .rangeKeyStr = rangeKeyStr;
034: this .keyMap = (keyMap == null) ? new LinkedHashMap() : keyMap;
035: }
036:
037: /**
038: * Returns any object that has a named key. In this keys case 'label' and
039: * 'units' makes sense, but any values are acceptible.
040: */
041: public Object getValue(String key) {
042: if ("details".equalsIgnoreCase(key))
043: return getDetails();
044: else
045: return keyMap.get(key);
046: }
047:
048: /**
049: * This method is called automatically by a HashMap when this class is used
050: * as a HashMap key. A Coordinate is considered equal if its x and y
051: * variables have the same value.
052: */
053:
054: public boolean equals(Object compareKey) {
055:
056: if (this == compareKey)
057: return true;
058: else if (compareKey instanceof MonKeyBase) {
059: MonKeyBase key = (MonKeyBase) compareKey;
060: return keyMap.equals(key.getMonKeyMap());
061: } else
062: return false;
063:
064: }
065:
066: /** Used when key is put into a Map to look up the monitor */
067: public int hashCode() {
068: return keyMap.hashCode();
069: }
070:
071: /** Return the map underlying this Object */
072: public Map getMonKeyMap() {
073: return keyMap;
074: }
075:
076: /** Puts the word 'Label' into the list. Used in the basic JAMon report */
077: public List getBasicHeader(List header) {
078: header.add(LABEL_HEADER);
079: return header;
080: }
081:
082: /** Returns each key in the map as a header element in the list */
083: public List getDisplayHeader(List header) {
084: return getHeader(header);
085: }
086:
087: /** Returns each key in the map as a header element in the list */
088: public List getHeader(List header) {
089: Iterator iter = keyMap.keySet().iterator();
090: while (iter.hasNext()) {
091: header.add(iter.next());
092: }
093:
094: return header;
095: }
096:
097: /** Returns the values assoicated with the key as a comma delimited entry in the list.
098: * For example if the map contains firstname, lastname, age then something like
099: * steve, souza, 44 would be returned
100: */
101: public List getBasicRowData(List rowData) {
102: Collection row = keyMap.values();
103: int currentElement = 1;
104: int lastElement = row.size();
105: Iterator iter = row.iterator();
106: StringBuffer buff = new StringBuffer();
107:
108: // loop through elements creating a comma delimeted list of the values
109: while (iter.hasNext()) {
110: buff.append(iter.next());
111: if (currentElement != lastElement)
112: buff.append(", ");
113:
114: currentElement++;
115: }
116:
117: if (buff.length() > 0)
118: rowData.add(buff.toString());
119:
120: return rowData;
121: }
122:
123: /** Add each value from the map at an element to the list.
124: */
125: public List getRowData(List rowData) {
126: Collection row = keyMap.values();
127: Iterator iter = row.iterator();
128: // loop through elements creating a comma delimeted list of the values
129: while (iter.hasNext()) {
130: rowData.add(iter.next());
131: }
132:
133: return rowData;
134: }
135:
136: /** Add each value from the map at an element to the list.
137: */
138: public List getRowDisplayData(List rowData) {
139: return getRowData(rowData);
140: }
141:
142: /** Returns a string representation of this object: JAMon Key, firstname=steve, lastname=souza, age=44*/
143: public String toString() {
144: Iterator iter = keyMap.entrySet().iterator();
145: StringBuffer buff = new StringBuffer("JAMon Key");
146: while (iter.hasNext()) {
147: Map.Entry entry = (Map.Entry) iter.next();
148: buff.append(", ").append(entry.getKey()).append("=")
149: .append(entry.getValue());
150:
151: }
152:
153: return buff.toString();
154: }
155:
156: /** Returns either the passed in range key, or it builds the key from the maps keys concatenated */
157: public String getRangeKey() {
158: if (rangeKeyStr == null) {
159: StringBuffer buff = new StringBuffer();
160: Iterator iter = keyMap.keySet().iterator();
161: while (iter.hasNext())
162: buff.append(iter.next()).append(" ");
163:
164: rangeKeyStr = buff.toString();
165: }
166:
167: return rangeKeyStr;
168: }
169:
170: public Object getDetails() {
171: if (details == null) {
172: List list = new ArrayList();
173: Iterator iter = keyMap.entrySet().iterator();
174: //StringBuffer buff=new StringBuffer();
175: while (iter.hasNext()) {
176: Map.Entry entry = (Map.Entry) iter.next();
177: Object value = entry.getValue();
178: if (value instanceof MonKeyItem)
179: value = ((MonKeyItem) value).getDetails();
180:
181: list.add(value);
182: // buff.append(value).append(", ");
183:
184: }
185:
186: return list;
187: } else
188: return details;
189: }
190:
191: public void setDetails(Object details) {
192: this .details = details;
193:
194: }
195:
196: private void testDisplay() {
197:
198: System.out.println("\n***");
199: System.out.println("toString()=" + this );
200: System.out.println("getValue calls=" + getValue("fn") + ", "
201: + getValue("ln") + ", " + getValue("age"));
202: System.out.println("getRangeKey()=" + getRangeKey());
203:
204: // Header calls
205: System.out.println("\nHeader calls");
206: System.out.println("getBasicHeader()="
207: + getBasicHeader(new ArrayList()));
208: System.out.println("getHeader()=" + getHeader(new ArrayList()));
209: System.out.println("getDisplayHeader()="
210: + getDisplayHeader(new ArrayList()));
211:
212: // Data Calls
213: System.out.println("\nData calls");
214: System.out.println("getBasicRowData()="
215: + getBasicRowData(new ArrayList()));
216: System.out.println("getRowData()="
217: + getRowData(new ArrayList()));
218: System.out.println("getRowDisplayData()="
219: + getRowDisplayData(new ArrayList()));
220: System.out.println("getDetailLabel()=" + getDetails());
221:
222: }
223:
224: public static void main(String[] arg) {
225: LinkedHashMap steveMap = new LinkedHashMap();
226: LinkedHashMap mindyMap = new LinkedHashMap();
227:
228: steveMap.put("fn", new MonKeyItemBase("steve summary",
229: "steve detail"));
230: steveMap.put("ln", "souza");
231: steveMap.put("age", new Long(44));
232:
233: mindyMap.put("fn", "mindy");
234: mindyMap.put("ln", "bobish");
235: mindyMap.put("age", new Long(33));
236:
237: MonKey steveMonKey = new MonKeyBase("Steves Range", steveMap);
238: MonKey steveMonKey2 = new MonKeyBase(steveMap);
239: MonKey mindyMonKey = new MonKeyBase(mindyMap);
240:
241: System.out.println("do 2 steves equal="
242: + steveMonKey.equals(steveMonKey));
243: System.out.println("do mindy and steve equal="
244: + steveMonKey.equals(mindyMonKey));
245:
246: ((MonKeyBase) steveMonKey).testDisplay();
247: ((MonKeyBase) steveMonKey2).testDisplay();
248: ((MonKeyBase) mindyMonKey).testDisplay();
249:
250: }
251:
252: }
|