2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.util
;
18 import java
.text
.NumberFormat
;
19 import java
.text
.ParseException
;
20 import java
.util
.Locale
;
22 import org
.argeo
.ArgeoException
;
24 public class Throughput
{
25 private final static NumberFormat usNumberFormat
= NumberFormat
26 .getInstance(Locale
.US
);
32 private final Double value
;
33 private final Unit unit
;
35 public Throughput(Double value
, Unit unit
) {
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
;
50 throw new ArgeoException("Unsupported unit " + unit
);
54 public Throughput(Double value
, String unitStr
) {
55 this(value
, Unit
.valueOf(unitStr
));
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);
66 this.value
= usNumberFormat
.parse(valueStr
).doubleValue();
67 } catch (ParseException e
) {
68 throw new ArgeoException("Cannot parse " + valueStr
69 + " as a number.", e
);
71 this.unit
= Unit
.valueOf(unitStr
);
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
);
84 throw new ArgeoException("Unsupported unit " + unit
);
88 public String
toString() {
89 return usNumberFormat
.format(value
) + '/' + unit
;
92 public Double
getValue() {
96 public Unit
getUnit() {