View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.utils.thresholds;
17  
18  import java.math.BigDecimal;
19  
20  public enum Prefixes {
21      /**
22       * Yotta : 10^24.
23       * You can use both yotta or Y (case sensitive) as unit of prefix.
24       */
25      yotta(new BigDecimal(10).pow(24)),
26      /**
27       * Zetta : 10^21.
28       * You can use both zetta or Z (case sensitive) as unit of prefix.
29       */
30      zetta(new BigDecimal(10).pow(21)),
31      /**
32       * Exa : 10^28.
33       * You can use both exa or E (case sensitive) as unit of prefix.
34       */
35      exa(new BigDecimal(10).pow(18)),
36      /**
37       * Peta : 10^15.
38       * You can use both peta or P (case sensitive) as unit of prefix.
39       */
40      peta(new BigDecimal(10).pow(15)),
41      /**
42       * Tera : 10^12.
43       * You can use both tera or T (case sensitive) as unit of prefix.
44       */
45      tera(new BigDecimal(10).pow(12)),
46      /**
47       * Giga : 10^9.
48       * You can use both giga or G (case sensitive) as unit of prefix.
49       */
50      giga(new BigDecimal(10).pow(9)),
51      /**
52       * Mega : 10^6.
53       * You can use both mega or M (case sensitive) as unit of prefix.
54       */
55      mega(new BigDecimal(10).pow(6)),
56      /**
57       * Kilo : 10^3.
58       * You can use both kilo or k (case sensitive) as unit of prefix.
59       */
60      kilo(new BigDecimal(10).pow(3)),
61      /**
62       * Hecto : 10^2.
63       * You can use both hecto or h (case sensitive) as unit of prefix.
64       */
65      hecto(new BigDecimal(10).pow(2)),
66      /**
67       * Deka : 10^1.
68       * You can use both deka or da (case sensitive) as unit of prefix.
69       */
70      deka(new BigDecimal(10).pow(1)),
71      /**
72       * Deci : 10^-1.
73       * You can use both deci or d (case sensitive) as unit of prefix.
74       */
75      deci(new BigDecimal(1).divide(new BigDecimal(10).pow(1))),
76      /**
77       * Centi : 10^-2.
78       * You can use both centi or c (case sensitive) as unit of prefix.
79       */
80      centi(new BigDecimal(1).divide(new BigDecimal(10).pow(2))),
81      /**
82       * Milli : 10^-3.
83       * You can use both milli or m (case sensitive) as unit of prefix.
84       */
85      milli(new BigDecimal(1).divide(new BigDecimal(10).pow(3))),
86      /**
87       * Micro : 10^-6.
88       * You can use both micro or u (case sensitive) as unit of prefix.
89       */
90      micro(new BigDecimal(1).divide(new BigDecimal(10).pow(6))),
91      /**
92       * Nano : 10^-9.
93       * You can use both nano or n (case sensitive) as unit of prefix.
94       */
95      nano(new BigDecimal(1).divide(new BigDecimal(10).pow(9))),
96      /**
97       * Pico : 10^-12.
98       * You can use both pico or p (case sensitive) as unit of prefix.
99       */
100     pico(new BigDecimal(1).divide(new BigDecimal(10).pow(12))),
101     /**
102      * Femto : 10^-15.
103      * You can use both femto or f (case sensitive) as unit of prefix.
104      */
105     femto(new BigDecimal(1).divide(new BigDecimal(10).pow(15))),
106     /**
107      * Atto : 10^-18.
108      * You can use both atto or a (case sensitive) as unit of prefix.
109      */
110     atto(new BigDecimal(1).divide(new BigDecimal(10).pow(18))),
111     /**
112      * Zepto : 10^-21.
113      * You can use both zepto or z (case sensitive) as unit of prefix.
114      */
115     zepto(new BigDecimal(1).divide(new BigDecimal(10).pow(21))),
116     /**
117      * Yocto : 10^-24.
118      * You can use both yocto or y (case sensitive) as unit of prefix.
119      */
120     yocto(new BigDecimal(1).divide(new BigDecimal(10).pow(24))),
121     /**
122      * Kibi: 2^10.
123      * You can use both kibi or Ki (case sensitive) as unit of prefix.
124      */
125     kibi(new BigDecimal(2).pow(10)),
126     /**
127      * Mebi: 2^20.
128      * You can use both mebi or Mi (case sensitive) as unit of prefix.
129      */
130     mebi(new BigDecimal(2).pow(20)),
131     /**
132      * Gibi: 2^30.
133      * You can use both gibi or Gi (case sensitive) as unit of prefix.
134      */
135     gibi(new BigDecimal(2).pow(30)),
136     /**
137      * Tebi: 2^40.
138      * You can use both tebi or Ti (case sensitive) as unit of prefix.
139      */
140     tebi(new BigDecimal(2).pow(40)),
141     /**
142      * Pebi: 2^50.
143      * You can use both pebi or Pi (case sensitive) as unit of prefix.
144      */
145     pebi(new BigDecimal(2).pow(50)),
146     /**
147      * Exbi: 2^60.
148      * You can use both exbi or Ei (case sensitive) as unit of prefix.
149      */
150     exbi(new BigDecimal(2).pow(60));
151 
152     /**
153      * The multiplier for the current prefix.
154      */
155     private final BigDecimal multiplier;
156 
157     /**
158      * Builds the enumeration specifying the multiplier.
159      * @param value The multiplier
160      */
161     Prefixes(final BigDecimal value) {
162         multiplier = value;
163     }
164 
165     /**
166      * Multiplies the value with the multiplier associated
167      * with this prefix.
168      * @param value The value
169      * @return The multiplied value
170      */
171     public BigDecimal convert(final BigDecimal value) {
172         return multiplier.multiply(value);
173     }
174 
175     /**
176      * Multiplies the value with the multiplier associated
177      * with this prefix.
178      * @param value The value
179      * @return The multiplied value
180      */
181     public BigDecimal convert(final int value) {
182         return multiplier.multiply(new BigDecimal(value));
183     }
184 
185     /**
186      * Multiplies the value with the multiplier associated
187      * with this prefix.
188      * @param value The value
189      * @return The multiplied value
190      */
191     public BigDecimal convert(final long value) {
192         return multiplier.multiply(new BigDecimal(value));
193     }
194 
195     /**
196      * Multiplies the value with the multiplier associated
197      * with this prefix.
198      * @param value The value
199      * @return The multiplied value
200      */
201     public BigDecimal convert(final double value) {
202         return multiplier.multiply(new BigDecimal(value));
203     }
204 
205     /**
206      * Creates the enumeration from its prefix string.
207      * @param prefixChar The prefix
208      * @return The enumeration
209      */
210     public static Prefixes fromChar(final char prefixChar) {
211         switch (prefixChar) {
212         case 'Y':
213             return yotta;
214         case 'Z':
215             return zetta;
216         case 'E':
217             return exa;
218         case 'P':
219             return peta;
220         case 'T':
221             return tera;
222         case 'G':
223             return giga;
224         case 'M':
225             return mega;
226         case 'k':
227             return kilo;
228         case 'h':
229             return hecto;
230         case 'd':
231             return deci;
232         case 'c':
233             return centi;
234         case 'm':
235             return milli;
236         case 'u':
237             return micro;
238         case 'n':
239             return nano;
240         case 'p':
241             return pico;
242         case 'f':
243             return femto;
244         case 'a':
245             return atto;
246         case 'z':
247             return zepto;
248         case 'y':
249             return yocto;
250         default:
251             throw new IllegalArgumentException("" + prefixChar);
252         }
253     }
254 
255     /**
256      * Returns the enumeration relative to the passed in prefix or string.
257      * @param prefixString The prefix
258      * @return The enumeration
259      */
260     public static Prefixes fromString(final String prefixString) {
261         if (prefixString.length() == 1) {
262             return fromChar(prefixString.charAt(0));
263         }
264 
265         String s = prefixString.toLowerCase();
266         if (s.equals("da")) {
267             return deka;
268         }
269         if (s.equals("ki")) {
270             return kilo;
271         }
272         if (s.equals("mi")) {
273             return mebi;
274         }
275         if (s.equals("gi")) {
276             return gibi;
277         }
278         if (s.equals("ti")) {
279             return tebi;
280         }
281         if (s.equals("pi")) {
282             return pebi;
283         }
284         if (s.equals("ei")) {
285             return exbi;
286         }
287 
288         return valueOf(prefixString);
289         //throw new IllegalArgumentException(prefixString);
290     }
291 }