KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > client > InstrumentSampleUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.client;
21
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23
24 /**
25  * A series of methods which are useful when working with InstrumentSamples.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:19 $
29  * @since 4.1
30  */

31 public class InstrumentSampleUtils
32 {
33     /**
34      * Resolves an instrument sample type based on a name.
35      *
36      * @param type Type of the InstrumentSample to resolve. Accepted values are:
37      * "max", "maximum", "min", "minimum", "mean",
38      * "ctr", and "counter".
39      *
40      * @throws ConfigurationException if the specified sample type is unknown.
41      */

42     public static int resolveInstrumentSampleType( String JavaDoc type )
43         throws ConfigurationException {
44         
45         if ( type.equalsIgnoreCase( "max" ) || type.equalsIgnoreCase( "maximum" ) )
46         {
47             return InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MAXIMUM;
48         }
49         else if ( type.equalsIgnoreCase( "min" ) || type.equalsIgnoreCase( "minimum" ) )
50         {
51             return InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MINIMUM;
52         }
53         else if ( type.equalsIgnoreCase( "mean" ) )
54         {
55             return InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MEAN;
56         }
57         else if ( type.equalsIgnoreCase( "ctr" ) || type.equalsIgnoreCase( "counter" ) )
58         {
59             return InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_COUNTER;
60         }
61         else
62         {
63             throw new ConfigurationException( "'" + type + "' is not a valid sample type." );
64         }
65     }
66     
67     public static String JavaDoc getInstrumentSampleTypeName( int type )
68     {
69         switch ( type )
70         {
71         case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MAXIMUM:
72             return "maximum";
73             
74         case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MINIMUM:
75             return "minimum";
76         
77         case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MEAN:
78             return "mean";
79             
80         case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_COUNTER:
81             return "counter";
82             
83         default:
84             return "unknown-" + type;
85         }
86     }
87     
88     /**
89      * Generates a sample name given its parameters.
90      *
91      * @param sampleType Type of the sample.
92      * @param sampleInterval Interval of the sample.
93      * @param sampleSize Size of the sample.
94      *
95      * @return A sample name.
96      */

97     public static String JavaDoc generateInstrumentSampleName( int sampleType,
98                                                        long sampleInterval,
99                                                        int sampleSize )
100     {
101         return getInstrumentSampleTypeName( sampleType ) + "_" +
102             sampleInterval + "_" + sampleSize;
103     }
104     
105     /**
106      * Generates a fully qualified sample name given its parameters.
107      *
108      * @param instrumentName Name of the instrument which owns the sample.
109      * @param sampleType Type of the sample.
110      * @param sampleInterval Interval of the sample.
111      * @param sampleSize Size of the sample.
112      *
113      * @return A fully qualified sample name.
114      */

115     public static String JavaDoc generateFullInstrumentSampleName( String JavaDoc instrumentName,
116                                                            int sampleType,
117                                                            long sampleInterval,
118                                                            int sampleSize )
119     {
120         return instrumentName + "." +
121             generateInstrumentSampleName( sampleType, sampleInterval, sampleSize );
122     }
123     
124     /**
125      * Returns the default description for a given type.
126      *
127      * @param type Whose description is being requested.
128      * @param interval Interval of the Sample.
129      *
130      * @return The description.
131      */

132     public static String JavaDoc getDefaultDescriptionForType( int type, long interval )
133     {
134         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
135         switch ( type )
136         {
137         case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_COUNTER:
138             sb.append( "Count each " );
139             break;
140             
141         case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MAXIMUM:
142             sb.append( "Max Value each " );
143             break;
144             
145         case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MEAN:
146             sb.append( "Mean Value each " );
147             break;
148             
149         case InstrumentSampleElementData.INSTRUMENT_SAMPLE_TYPE_MINIMUM:
150             sb.append( "Min Value each " );
151             break;
152             
153         default:
154             return "Unknown Type " + type;
155         }
156         
157         if ( interval == 1000 )
158         {
159             sb.append( "Second" );
160         }
161         else if ( interval == 60000 )
162         {
163             sb.append( "Minute" );
164         }
165         else if ( interval == 3600000 )
166         {
167             sb.append( "Hour" );
168         }
169         else if ( interval == 86400000 )
170         {
171             sb.append( "Day" );
172         }
173         else if ( interval % 86400000 == 0 )
174         {
175             sb.append( interval / 86400000 );
176             sb.append( " Days" );
177         }
178         else if ( interval % 3600000 == 0 )
179         {
180             sb.append( interval / 3600000 );
181             sb.append( " Hours" );
182         }
183         else if ( interval % 60000 == 0 )
184         {
185             sb.append( interval / 60000 );
186             sb.append( " Minutes" );
187         }
188         else if ( interval % 1000 == 0 )
189         {
190             sb.append( interval / 1000 );
191             sb.append( " Seconds" );
192         }
193         else
194         {
195             sb.append( interval );
196             sb.append( " Milliseconds" );
197         }
198         
199         return sb.toString();
200     }
201 }
202
Popular Tags