KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > Util


1 /*
2  * Japex ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * This Software is distributed under the following terms:
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, is permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistribution in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * Neither the name of Sun Microsystems, Inc., 'Java', 'Java'-based names,
19  * nor the names of contributors may be used to endorse or promote products
20  * derived from this Software without specific prior written permission.
21  *
22  * The Software is provided "AS IS," without a warranty of any kind. ALL
23  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
24  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25  * PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS
26  * SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE
27  * AS A RESULT OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE
28  * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE
29  * LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
30  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED
31  * AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
32  * INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * You acknowledge that the Software is not designed, licensed or intended
36  * for use in the design, construction, operation or maintenance of any
37  * nuclear facility.
38  */

39
40 package com.sun.japex;
41
42 import java.io.*;
43 import java.util.*;
44 import java.lang.reflect.*;
45 import java.text.DecimalFormat JavaDoc;
46
47 public class Util {
48     
49     static final int KB = 1024;
50     static final String JavaDoc spaces = " ";
51     
52     static Method currentTime;
53     static boolean unitIsMillis;
54     
55     static {
56         // Use nanoTime() if available - JDK 1.5
57
try {
58             currentTime = System JavaDoc.class.getMethod("nanoTime", null);
59             unitIsMillis = false;
60         }
61         catch (NoSuchMethodException JavaDoc e) {
62             try {
63                 currentTime = System JavaDoc.class.getMethod("currentTimeMillis", null);
64                 unitIsMillis = true;
65             }
66             catch (NoSuchMethodException JavaDoc ee) {
67                 e.printStackTrace();
68                 System.exit(1);
69             }
70         }
71     }
72     
73     /** Creates a new instance of Util */
74     public Util() {
75     }
76
77     static String JavaDoc getSpaces(int length) {
78         return spaces.substring(0, length);
79     }
80     
81     static public byte[] streamToByteArray(InputStream is) {
82         ByteArrayOutputStream bos = new ByteArrayOutputStream(8 * KB);
83         int c;
84         try {
85             while ((c = is.read()) != -1) {
86                 bos.write(c);
87             }
88         } catch (IOException e) {
89             throw new RuntimeException JavaDoc(e);
90         }
91         return bos.toByteArray();
92     }
93         
94     public static long parseDuration(String JavaDoc duration) {
95         try {
96             int length = duration.length();
97             switch (length) {
98                 case 1:
99                 case 2:
100                     // S?S
101
return Integer.parseInt(duration.substring(0, length))
102                            * 1000;
103                 case 5:
104                     if (duration.charAt(2) == ':') {
105                         // MM:SS
106
return Integer.parseInt(duration.substring(0, 2))
107                                * 60 * 1000 +
108                                Integer.parseInt(duration.substring(3, 5))
109                                * 1000;
110                     }
111                     break;
112                 case 8:
113                     // HH:MM:SS
114
if (duration.charAt(2) == ':' && duration.charAt(5) == ':') {
115                         return Integer.parseInt(duration.substring(0, 2))
116                                * 60 * 60 * 1000 +
117                                Integer.parseInt(duration.substring(3, 5))
118                                * 60 * 1000 +
119                                Integer.parseInt(duration.substring(6, 8))
120                                * 1000;
121                     }
122                     break;
123             }
124         }
125         catch (NumberFormatException JavaDoc e) {
126             // Falls through
127
}
128         throw new RuntimeException JavaDoc("Duration '" + duration
129             + "' does not conform to pattern '((HH:)?MM:)?SS'");
130     }
131     
132     public static long currentTimeNanos() {
133         try {
134             long t = ((Number JavaDoc) currentTime.invoke(null, null)).longValue();
135             return unitIsMillis ? millisToNanos(t) : t;
136         }
137         catch (Exception JavaDoc e) {
138             e.printStackTrace();
139             System.exit(1);
140         }
141         return 0;
142     }
143     
144     public static long currentTimeMillis() {
145         try {
146             long t = ((Number JavaDoc) currentTime.invoke(null, null)).longValue();
147             return unitIsMillis ? t : (long) nanosToMillis(t);
148         }
149         catch (Exception JavaDoc e) {
150             e.printStackTrace();
151             System.exit(1);
152         }
153         return 0;
154     }
155     
156     public static long millisToNanos(long millis) {
157         return millis * 1000000L;
158     }
159     
160     public static double nanosToMillis(long nanos) {
161         return nanos / 1000000.0;
162     }
163     
164     public static double arithmeticMean(double[] sample) {
165         double mean = 0.0;
166         for (int i = 0; i < sample.length; i++) {
167             mean += sample[i];
168         }
169         return (mean / sample.length);
170     }
171     
172     public static double standardDev(double[] sample) {
173         double mean = arithmeticMean(sample);
174
175         // Compute biased variance
176
double variance = 0.0;
177         for (int i = 0; i < sample.length; i++) {
178             variance += (sample[i] - mean) * (sample[i] - mean);
179         }
180         variance /= sample.length;
181         
182         // Return standard deviation
183
return Math.sqrt(variance);
184     }
185     
186     static DecimalFormat JavaDoc _decimalFormat = new DecimalFormat JavaDoc("0.00");
187     
188     public static String JavaDoc formatDouble(double value) {
189         return _decimalFormat.format(value);
190     }
191 }
192
Popular Tags