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:
016: package org.griphyn.cPlanner.visualize.spaceusage;
017:
018: import java.util.Date;
019:
020: /**
021: * A data class that stores the space usage.
022: *
023: * @author Karan Vahi
024: * @version $Revision: 50 $
025: */
026:
027: public class Space implements Cloneable {
028:
029: /**
030: * Holds the timestamp when usage was taken.
031: */
032: private Date mDate;
033:
034: /**
035: * The size.
036: */
037: private float mSize;
038:
039: /**
040: * The units in which the size is stored.
041: * M for megabyte, K for Kilobytes, T for terabytes.
042: * The default unit is K
043: */
044: private char mUnit;
045:
046: /**
047: * index to do conversions between units.
048: */
049: private int mIndex;
050:
051: /**
052: * Indicates whether the space denoted is the amount of space
053: * cleaned up.
054: */
055: private boolean mCleanupSpace;
056:
057: /**
058: * The jobname for which the reading was taken.
059: */
060: private String mAssociatedJob;
061:
062: /**
063: * The overloaded constructor.
064: *
065: * @param d the date.
066: */
067: public Space(Date d) {
068: mDate = d;
069: mUnit = 'K';
070: mIndex = getIndex(mUnit);
071: mCleanupSpace = false;
072: }
073:
074: /**
075: * The overloaded constructor.
076: *
077: * @param d Date
078: * @param size the size with the last character denoting the unit.
079: */
080: public Space(Date d, String size) {
081: mDate = d;
082: mUnit = 'K';
083: mIndex = getIndex(mUnit);
084: setSize(size);
085: mCleanupSpace = false;
086: }
087:
088: /**
089: * Sets boolean indicating that space denoted is the amount cleaned.
090: *
091: * @param cleanup boolean indicating that value is cleaned up.
092: */
093: public void setCleanupFlag(boolean cleanup) {
094: mCleanupSpace = cleanup;
095: }
096:
097: /**
098: * Sets the associated job for which the reading was taken.
099: *
100: * @param job the associated job.
101: */
102: public void setAssociatedJob(String job) {
103: mAssociatedJob = job;
104: }
105:
106: /**
107: * Returns the associated job.
108: *
109: * @return the associated job.
110: */
111: public String getAssociatedJob() {
112: return mAssociatedJob;
113: }
114:
115: /**
116: * Returns boolean indicating whether the space denoted is the amount cleaned
117: * or not.
118: *
119: * @return boolean indicating that value is cleaned up.
120: */
121: public boolean getCleanupFlag() {
122: return mCleanupSpace;
123: }
124:
125: /**
126: * Sets the size.
127: *
128: * @param size the size optionally with the units.
129: * @param unit the unit of the size
130: */
131: public void setSize(float size, char unit) {
132: mSize = size;
133: mUnit = unit;
134: mIndex = getIndex(unit);
135: }
136:
137: /**
138: * Sets the size.
139: *
140: * @param size the size optionally with the units.
141: */
142: public void setSize(String size) {
143: size = size.trim();
144: char c = size.charAt(size.length() - 1);
145:
146: if (Character.isLetter(c)) {
147: if (validUnit(c)) {
148: mUnit = c;
149: mIndex = this .getIndex(c);
150: mSize = Float.parseFloat(size.substring(0, size
151: .length() - 1));
152: } else {
153: throw new RuntimeException("Invald unit " + c);
154: }
155: } else {
156: mSize = Float.parseFloat(size);
157: mUnit = 'K';
158: }
159: }
160:
161: /**
162: * Returns the time at which Space was record.
163: *
164: * @return Date
165: */
166: public Date getDate() {
167: return mDate;
168: }
169:
170: /**
171: * Returns the size in the units associated with it.
172: *
173: * @return size in float
174: */
175: public float getSize() {
176: return mSize;
177: }
178:
179: /**
180: * Returns the size in particular unit.
181: *
182: * @param u the unit.
183: *
184: * @return size in float
185: */
186: public float getSize(char u) {
187: int index = getIndex(u);
188: //System.out.println( "difference is " + (mIndex - index) );
189: //System.out.println( "multiplying factor is " + this.getMultiplyingFactor( 1024, mIndex - index));
190: //return mSize * (float) Math.pow( 1024, mIndex - index );
191: return mSize * this .getMultiplyingFactor(1024, mIndex - index);
192: }
193:
194: /**
195: * Returns the units associated with the size.
196: *
197: * @return the unit
198: */
199: public char getUnits() {
200: return mUnit;
201: }
202:
203: /**
204: * Returns if a character is a valid unit or not
205: *
206: * @param c the unit
207: *
208: * @return boolean
209: */
210: public boolean validUnit(char c) {
211: return (c == 'K' || c == 'M' || c == 'G');
212: }
213:
214: /**
215: * Returns a textual description of the content
216: *
217: * @return String.
218: */
219: public String toString() {
220: StringBuffer sb = new StringBuffer();
221: sb.append(mDate).append(" ").append(mSize).append(mUnit);
222: sb.append(" ").append(getAssociatedJob());
223: if (mCleanupSpace) {
224: sb.append(" cleaned up space");
225: }
226: return sb.toString();
227: }
228:
229: /**
230: * Returns the clone of the object.
231: *
232: * @return the clone
233: */
234: public Object clone() {
235: Space s;
236: try {
237: s = (Space) super .clone();
238: } catch (CloneNotSupportedException e) {
239: //somewhere in the hierarch chain clone is not implemented
240: throw new RuntimeException(
241: "Clone not implemented in the base class of "
242: + this .getClass().getName(), e);
243: }
244: s.mAssociatedJob = this .mAssociatedJob;
245: s.mCleanupSpace = this .mCleanupSpace;
246: s.mDate = (Date) this .mDate.clone();
247: s.mIndex = this .mIndex;
248: s.mSize = this .mSize;
249: s.mUnit = this .mUnit;
250:
251: return s;
252: }
253:
254: /**
255: * Returns the index for the associated unit.
256: *
257: * @return the index.
258: */
259: private int getIndex() {
260: return mIndex;
261: }
262:
263: /**
264: * Returns the index for a unit.
265: *
266: * @param u the unit
267: * @return the index.
268: */
269: private int getIndex(char u) {
270: int index = -1;
271: switch (u) {
272: case 'K':
273: index = 1;
274: break;
275:
276: case 'M':
277: index = 2;
278: break;
279:
280: case 'G':
281: index = 3;
282: break;
283:
284: default:
285: throw new RuntimeException("Invalid unit scheme" + u);
286: }
287: return index;
288: }
289:
290: /**
291: * Returns multiplying factor for conversion.
292: * Simulates ^ operator.
293: *
294: * @param base
295: * @param power the power to raise the base to.
296: *
297: * @return multiplying value
298: */
299: private float getMultiplyingFactor(int base, int power) {
300: float result = 1;
301:
302: if (power >= 0) {
303: for (int i = 0; i < power; i++) {
304: result *= base;
305: }
306: } else {
307: for (int i = 0; i < -power; i++) {
308: result /= base;
309: }
310: }
311: return result;
312: }
313:
314: }
|