1/*
2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.tools.jstat;
27
28import java.util.*;
29
30/**
31 * A typesafe enumeration for describing data scaling semantics
32 *
33 * @author Brian Doherty
34 * @since 1.5
35 */
36public class Scale {
37    private static int nextOrdinal = 0;
38    private static HashMap<String, Scale> map = new HashMap<String, Scale>();
39
40    private final String name;
41    private final int ordinal = nextOrdinal++;
42    private final double factor;
43
44    private Scale(String name, double factor) {
45        this.name = name;
46        this.factor = factor;
47        assert !map.containsKey(name);
48        map.put(name, this);
49    }
50
51    /**
52     * Scale representing a no scaling
53     */
54    public static final Scale RAW = new Scale("raw", 1);
55
56    /**
57     * Scale representing a percent scaling
58     */
59    public static final Scale PERCENT = new Scale("percent", 1/100);
60
61    /**
62     * Scale representing a kilo scaling
63     */
64    public static final Scale KILO = new Scale("K", 1024);
65
66    /**
67     * Scale representing a mega scaling
68     */
69    public static final Scale MEGA = new Scale("M", 1024*1024);
70
71    /**
72     * Scale representing a giga scaling
73     */
74    public static final Scale GIGA = new Scale("G", 1024*1024*1024);
75
76    /**
77     * Scale representing a tera scaling
78     */
79    public static final Scale TERA = new Scale("T", 1024*1024*1024*1024);
80
81    /**
82     * Scale representing a tera scaling
83     */
84    public static final Scale PETA = new Scale("P", 1024*1024*1024*1024*1024);
85
86    /**
87     * Scale representing a pico scaling
88     */
89    public static final Scale PICO = new Scale("p", 10.0E-12);
90
91    /**
92     * Scale representing a nano scaling
93     */
94    public static final Scale NANO = new Scale("n", 10.0E-9);
95
96    /**
97     * Scale representing a micro scaling
98     */
99    public static final Scale MICRO = new Scale("u", 10.0E-6);
100
101    /**
102     * Scale representing a milli scaling
103     */
104    public static final Scale MILLI = new Scale("m", 10.0E-3);
105
106    /**
107     * Scale representing a picosecond scaling
108     */
109    public static final Scale PSEC = new Scale("ps", 10.0E-12);
110
111    /**
112     * Scale representing a nanosecond scaling
113     */
114    public static final Scale NSEC = new Scale("ns", 10.0E-9);
115
116    /**
117     * Scale representing a microsecond scaling
118     */
119    public static final Scale USEC = new Scale("us", 10.0E-6);
120
121    /**
122     * Scale representing a millisecond scaling
123     */
124    public static final Scale MSEC = new Scale("ms", 10.0E-3);
125
126    /**
127     * Scale representing a second scaling
128     */
129    public static final Scale SEC = new Scale("s", 1);
130    public static final Scale SEC2 = new Scale("sec", 1);
131
132    /**
133     * Scale representing a minutes scaling
134     */
135    public static final Scale MINUTES = new Scale("min", 1/60.0);
136
137    /**
138     * Scale representing a hours scaling
139     */
140    public static final Scale HOUR = new Scale("h", 1/(60.0*60.0));
141    public static final Scale HOUR2 = new Scale("hour", 1/(60.0*60.0));
142
143    /**
144     * Returns the scaling factor of this Scale object
145     *
146     * @return  the scaling factor of this Scale object
147     */
148    public double getFactor() {
149        return factor;
150    }
151
152    /**
153     * Returns the string representation of this Scale object.
154     * The string representation is the name of the Scale object.
155     *
156     * @return  the string representation of this Scale object
157     */
158    public String toString() {
159        return name;
160    }
161
162    /**
163     * Maps a string to its corresponding Scale object.
164     *
165     * @param   s  a string to match against Scale objects.
166     * @return     The Scale object matching the given string.
167     */
168    public static Scale toScale(String s) {
169        return map.get(s);
170    }
171
172    /**
173     * Returns an enumeration of the keys for this enumerated type
174     *
175     * @param   s  an string to match against Scale objects.
176     * @return     The Scale object matching the given string.
177     */
178    protected static Set<String> keySet() {
179        return map.keySet();
180    }
181
182    protected double scale(double value) {
183        return value/factor;
184    }
185}
186