001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstat;
027:
028: import java.util.*;
029:
030: /**
031: * A typesafe enumeration for describing data scaling semantics
032: *
033: * @author Brian Doherty
034: * @version 1.9, 05/09/07
035: * @since 1.5
036: */
037: public class Scale {
038: private static int nextOrdinal = 0;
039: private static HashMap<String, Scale> map = new HashMap<String, Scale>();
040:
041: private final String name;
042: private final int ordinal = nextOrdinal++;
043: private final double factor;
044:
045: private Scale(String name, double factor) {
046: this .name = name;
047: this .factor = factor;
048: assert !map.containsKey(name);
049: map.put(name, this );
050: }
051:
052: /**
053: * Scale representing a no scaling
054: */
055: public static final Scale RAW = new Scale("raw", 1);
056:
057: /**
058: * Scale representing a percent scaling
059: */
060: public static final Scale PERCENT = new Scale("percent", 1 / 100);
061:
062: /**
063: * Scale representing a kilo scaling
064: */
065: public static final Scale KILO = new Scale("K", 1024);
066:
067: /**
068: * Scale representing a mega scaling
069: */
070: public static final Scale MEGA = new Scale("M", 1024 * 1024);
071:
072: /**
073: * Scale representing a giga scaling
074: */
075: public static final Scale GIGA = new Scale("G", 1024 * 1024 * 1024);
076:
077: /**
078: * Scale representing a tera scaling
079: */
080: public static final Scale TERA = new Scale("T",
081: 1024 * 1024 * 1024 * 1024);
082:
083: /**
084: * Scale representing a tera scaling
085: */
086: public static final Scale PETA = new Scale("P", 1024 * 1024 * 1024
087: * 1024 * 1024);
088:
089: /**
090: * Scale representing a pico scaling
091: */
092: public static final Scale PICO = new Scale("p", 10.0E-12);
093:
094: /**
095: * Scale representing a nano scaling
096: */
097: public static final Scale NANO = new Scale("n", 10.0E-9);
098:
099: /**
100: * Scale representing a micro scaling
101: */
102: public static final Scale MICRO = new Scale("u", 10.0E-6);
103:
104: /**
105: * Scale representing a milli scaling
106: */
107: public static final Scale MILLI = new Scale("m", 10.0E-3);
108:
109: /**
110: * Scale representing a picosecond scaling
111: */
112: public static final Scale PSEC = new Scale("ps", 10.0E-12);
113:
114: /**
115: * Scale representing a nanosecond scaling
116: */
117: public static final Scale NSEC = new Scale("ns", 10.0E-9);
118:
119: /**
120: * Scale representing a microsecond scaling
121: */
122: public static final Scale USEC = new Scale("us", 10.0E-6);
123:
124: /**
125: * Scale representing a millisecond scaling
126: */
127: public static final Scale MSEC = new Scale("ms", 10.0E-3);
128:
129: /**
130: * Scale representing a second scaling
131: */
132: public static final Scale SEC = new Scale("s", 1);
133: public static final Scale SEC2 = new Scale("sec", 1);
134:
135: /**
136: * Scale representing a minutes scaling
137: */
138: public static final Scale MINUTES = new Scale("min", 1 / 60.0);
139:
140: /**
141: * Scale representing a hours scaling
142: */
143: public static final Scale HOUR = new Scale("h", 1 / (60.0 * 60.0));
144: public static final Scale HOUR2 = new Scale("hour",
145: 1 / (60.0 * 60.0));
146:
147: /**
148: * Returns the scaling factor of this Scale object
149: *
150: * @return the scaling factor of this Scale object
151: */
152: public double getFactor() {
153: return factor;
154: }
155:
156: /**
157: * Returns the string representation of this Scale object.
158: * The string representation is the name of the Scale object.
159: *
160: * @return the string representation of this Scale object
161: */
162: public String toString() {
163: return name;
164: }
165:
166: /**
167: * Maps a string to its corresponding Scale object.
168: *
169: * @param s a string to match against Scale objects.
170: * @return The Scale object matching the given string.
171: */
172: public static Scale toScale(String s) {
173: return map.get(s);
174: }
175:
176: /**
177: * Returns an enumeration of the keys for this enumerated type
178: *
179: * @param s an string to match against Scale objects.
180: * @return The Scale object matching the given string.
181: */
182: protected static Set keySet() {
183: return map.keySet();
184: }
185:
186: protected double scale(double value) {
187: return value / factor;
188: }
189: }
|