KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > internal > performance > data > Unit


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test.internal.performance.data;
12
13 import java.text.NumberFormat JavaDoc;
14
15
16 /**
17  * @since 3.1
18  */

19 public class Unit {
20
21     public static final Unit SECOND= new Unit("s", "second", false); //$NON-NLS-1$ //$NON-NLS-2$
22
public static final Unit BYTE= new Unit("byte", "byte", true); //$NON-NLS-1$ //$NON-NLS-2$
23
public static final Unit CARDINAL= new Unit("", "", false); //$NON-NLS-1$ //$NON-NLS-2$
24
public static final Unit INVOCATION= new Unit("invoc.", "invocation", false); //$NON-NLS-1$ //$NON-NLS-2$
25

26     private static final int T_DECIMAL= 1000;
27     private static final int T_BINARY= 1024;
28     
29     //protected static final String[] PREFIXES= new String[] { "y", "z", "a", "f", "p", "n", "u", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y" };
30
//protected static final String[] FULL_PREFIXES= new String[] { "yocto", "zepto", "atto", "femto", "pico", "nano", "micro", "milli", "", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta" };
31
//protected static final String[] BINARY_PREFIXES= new String[] { "", "", "", "", "", "", "", "", "", "ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
32
//protected static final String[] BINARY_FULL_PREFIXES= new String[] { "", "", "", "", "", "", "", "", "", "kibi", "mebi", "gibi", "tebi", "pebi", "exbi", "zebi", "yobi" };
33

34     private final String JavaDoc fShortName;
35     private final String JavaDoc fFullName;
36     private final boolean fIsBinary;
37     //private final int fPrecision= 2;
38

39     public Unit(String JavaDoc shortName, String JavaDoc fullName, boolean binary) {
40         fShortName= shortName;
41         fFullName= fullName;
42         fIsBinary= binary;
43     }
44     
45     public String JavaDoc getShortName() {
46         return fShortName;
47     }
48     
49     public String JavaDoc getFullName() {
50         return fFullName;
51     }
52     
53     public String JavaDoc getDisplayValue1(long magnitudel, int multiplier) {
54         
55         //return getDisplayValue1(magnitudel / multiplier);
56
//return Long.toString((double)(magnitudel / multiplier));
57
return getDisplayValue1(magnitudel / multiplier);
58     }
59
60     public String JavaDoc getDisplayValue1(double magnitude) {
61         
62         if (this == SECOND)
63             return formatedTime((long) (magnitude*1000.0));
64         return formatEng((long) (magnitude));
65         
66         /*
67         int div= fIsBinary ? T_BINARY : T_DECIMAL;
68         boolean negative= magnitude < 0;
69         double mag= Math.abs(magnitude), ratio= mag / div;
70         int divs= PREFIXES.length / 2;
71         while (ratio >= 1) {
72             mag= ratio;
73             divs++;
74             ratio= mag / div;
75         }
76         ratio= mag * div;
77         while (ratio > 0.0 && ratio < div) {
78             mag= ratio;
79             divs--;
80             ratio= mag * div;
81         }
82         
83         if (negative)
84             mag= -mag;
85         
86         String[] prefixes= fIsBinary ? BINARY_PREFIXES : PREFIXES;
87         NumberFormat format= NumberFormat.getInstance();
88         format.setMaximumFractionDigits(fPrecision);
89         if (divs > 0 && divs <= prefixes.length)
90             return prefixes[divs] + getShortName() + format.format(mag);
91         else
92             return getShortName() + magnitude;
93         */

94     }
95     
96     public String JavaDoc toString() {
97         return "Unit [" + getShortName() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
98
}
99     
100     /**
101      * Answer a formatted string for the elapsed time (minutes, hours or days)
102      * that is appropriate for the scale of the time.
103      *
104      * @param diff time in milliseconds
105      *
106      * I copied this from karasiuk.utility.TimeIt
107      * @return the formatted time
108      */

109     public static String JavaDoc formatedTime(long diff) {
110         if (diff < 0)
111             diff *= -1;
112         if (diff < 1000)
113             return String.valueOf(diff) + " ms"; //$NON-NLS-1$
114

115         NumberFormat JavaDoc nf= NumberFormat.getInstance();
116         nf.setMaximumFractionDigits(2);
117         double d = diff / 1000.0;
118         if (d < 60)
119             return nf.format(d) + " s"; //$NON-NLS-1$
120

121         d = d / 60.0;
122         if (d < 60.0)
123             return nf.format(d) + " m"; //$NON-NLS-1$
124

125         d = d / 60.0;
126         if (d < 24.0)
127             return nf.format(d) + " h"; //$NON-NLS-1$
128

129         d = d / 24.0;
130         return nf.format(d) + " d"; //$NON-NLS-1$
131
}
132     
133     /**
134      * Answer a number formatted using engineering conventions, K thousands, M millions,
135      * G billions and T trillions.
136      *
137      * I copied this method from karasiuk.utility.Misc.
138      * @param n the number to format
139      * @return the formatted number
140      */

141     public String JavaDoc formatEng(long n) {
142         int TSD= fIsBinary ? T_BINARY : T_DECIMAL;
143         if (n < TSD)
144             return String.valueOf(n);
145         double d = ((double)n) / TSD;
146         NumberFormat JavaDoc nf = NumberFormat.getInstance();
147         nf.setMaximumFractionDigits(2);
148         if (d < TSD)
149             return nf.format(d) + "K"; //$NON-NLS-1$
150

151         d = d / TSD;
152         if ( d < TSD)
153             return nf.format(d) + "M"; //$NON-NLS-1$
154

155         d = d / TSD;
156         if ( d < TSD)
157             return nf.format(d) + "G"; //$NON-NLS-1$
158

159         d = d / TSD;
160         return nf.format(d) + "T"; //$NON-NLS-1$
161
}
162
163 }
164
Popular Tags