001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.cPlanner.visualize.spaceusage;
016:
017: import java.util.Map;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.LinkedList;
021: import java.util.Comparator;
022: import java.util.Collections;
023: import java.util.Iterator;
024:
025: /**
026: * A container object that stores the space usage for each site.
027: *
028: * @author Karan Vahi
029: * @version $Revision: 50 $
030: */
031: public class SpaceUsage {
032:
033: /**
034: * The map that stores the list of space objects indexed by site name.
035: */
036: private Map mStore;
037:
038: /**
039: * The default store.
040: */
041: public SpaceUsage() {
042: mStore = new HashMap();
043: }
044:
045: /**
046: * Returns an iterator to list of <code>String</code> site identifiers
047: * for which data is available.
048: *
049: * @return Iterator
050: */
051: public Iterator siteIterator() {
052: return mStore.keySet().iterator();
053: }
054:
055: /**
056: * Returns the list of <code>Space</code> objects corresponding to a
057: * particular site.
058: *
059: * @param site the site for which sizes are required.
060: *
061: * @return List
062: */
063: public List getSizes(String site) {
064: return (mStore.containsKey(site) ? (List) mStore.get(site)
065: : new LinkedList());
066: }
067:
068: /**
069: * Add a Space record to the store.
070: *
071: * @param site the site for which the record is logged.
072: * @param record the <code>SpaceUsage</code> record.
073: */
074: public void addRecord(String site, Space record) {
075: List l = (mStore.containsKey(site)) ? (List) mStore.get(site)
076: : new LinkedList();
077: l.add(record);
078: mStore.put(site, l);
079: }
080:
081: /**
082: * Sorts the records for each site.
083: */
084: public void sort() {
085: SpaceComparator s = new SpaceComparator();
086: for (Iterator it = mStore.entrySet().iterator(); it.hasNext();) {
087: Map.Entry entry = (Map.Entry) it.next();
088: List l = (List) entry.getValue();
089: Collections.sort(l, s);
090: }
091: }
092:
093: /**
094: * Returns textual description of the object.
095: *
096: * @return the textual description
097: */
098: public String toString() {
099: StringBuffer sb = new StringBuffer();
100: sb.append("{\n ");
101: for (Iterator it = mStore.entrySet().iterator(); it.hasNext();) {
102: Map.Entry entry = (Map.Entry) it.next();
103: List l = (List) entry.getValue();
104: sb.append(entry.getKey()).append(" -> ");
105: for (Iterator lIT = l.iterator(); lIT.hasNext();) {
106: sb.append("\n\t");
107: sb.append(lIT.next());
108: sb.append(" , ");
109: }
110: }
111: sb.append("\n}");
112: return sb.toString();
113: }
114: }
115:
116: /**
117: * Comparator for Space objects that allows us to sort on time.
118: *
119: */
120: class SpaceComparator implements Comparator {
121:
122: /**
123: * Implementation of the {@link java.lang.Comparable} interface.
124: * Compares this object with the specified object for order. Returns a
125: * negative integer, zero, or a positive integer as this object is
126: * less than, equal to, or greater than the specified object. The
127: * definitions are compared by their type, and by their short ids.
128: *
129: * @param o1 is the object to be compared
130: * @param o2 is the object to be compared with o1.
131: *
132: * @return a negative number, zero, or a positive number, if the
133: * object compared against is less than, equals or greater than
134: * this object.
135: * @exception ClassCastException if the specified object's type
136: * prevents it from being compared to this Object.
137: */
138: public int compare(Object o1, Object o2) {
139: if (o1 instanceof Space && o2 instanceof Space) {
140: Space s1 = (Space) o1;
141: Space s2 = (Space) o2;
142:
143: return s1.getDate().compareTo(s2.getDate());
144: } else {
145: throw new ClassCastException("object is not a Space");
146: }
147: }
148: }
|