]> git.argeo.org Git - lgpl/argeo-commons.git/blob - basic/runtime/org.argeo.basic.nodeps/src/main/java/org/argeo/util/Throughput.java
Deal with null values
[lgpl/argeo-commons.git] / basic / runtime / org.argeo.basic.nodeps / src / main / java / org / argeo / util / Throughput.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.util;
18
19 import java.text.NumberFormat;
20 import java.text.ParseException;
21 import java.util.Locale;
22
23 import org.argeo.ArgeoException;
24
25 public class Throughput {
26 private final static NumberFormat usNumberFormat = NumberFormat
27 .getInstance(Locale.US);
28
29 public enum Unit {
30 s, m, h, d
31 }
32
33 private final Double value;
34 private final Unit unit;
35
36 public Throughput(Double value, Unit unit) {
37 this.value = value;
38 this.unit = unit;
39 }
40
41 public Throughput(Long periodMs, Long count, Unit unit) {
42 if (unit.equals(Unit.s))
43 value = ((double) count * 1000d) / periodMs;
44 else if (unit.equals(Unit.m))
45 value = ((double) count * 60d * 1000d) / periodMs;
46 else if (unit.equals(Unit.h))
47 value = ((double) count * 60d * 60d * 1000d) / periodMs;
48 else if (unit.equals(Unit.d))
49 value = ((double) count * 24d * 60d * 60d * 1000d) / periodMs;
50 else
51 throw new ArgeoException("Unsupported unit " + unit);
52 this.unit = unit;
53 }
54
55 public Throughput(Double value, String unitStr) {
56 this(value, Unit.valueOf(unitStr));
57 }
58
59 public Throughput(String def) {
60 int index = def.indexOf('/');
61 if (def.length() < 3 || index <= 0 || index != def.length() - 2)
62 throw new ArgeoException(def + " no a proper throughput definition"
63 + " (should be <value>/<unit>, e.g. 3.54/s or 1500/h");
64 String valueStr = def.substring(0, index);
65 String unitStr = def.substring(index + 1);
66 try {
67 this.value = usNumberFormat.parse(valueStr).doubleValue();
68 } catch (ParseException e) {
69 throw new ArgeoException("Cannot parse " + valueStr
70 + " as a number.", e);
71 }
72 this.unit = Unit.valueOf(unitStr);
73 }
74
75 public Long asMsPeriod() {
76 if (unit.equals(Unit.s))
77 return Math.round(1000d / value);
78 else if (unit.equals(Unit.m))
79 return Math.round((60d * 1000d) / value);
80 else if (unit.equals(Unit.h))
81 return Math.round((60d * 60d * 1000d) / value);
82 else if (unit.equals(Unit.d))
83 return Math.round((24d * 60d * 60d * 1000d) / value);
84 else
85 throw new ArgeoException("Unsupported unit " + unit);
86 }
87
88 @Override
89 public String toString() {
90 return usNumberFormat.format(value) + '/' + unit;
91 }
92
93 public Double getValue() {
94 return value;
95 }
96
97 public Unit getUnit() {
98 return unit;
99 }
100
101 }