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