blob: e044eac143554bba657bc270af349a23f4c17202 [file] [log] [blame]
Brian O'Connor7cbbbb72016-04-09 02:13:23 -07001/*
2 * Copyright 2015-present Open Networking Laboratory
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 */
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070016package org.onlab.util;
17
18import com.google.common.annotations.Beta;
19
20/**
21 * Data rate unit.
22 */
23@Beta
24public enum DataRateUnit {
25 /**
26 * Bit per second.
27 */
28 BPS(1L),
29 /**
30 * Kilobit per second.
31 * (Decimal/SI)
32 */
33 KBPS(1_000L),
34 /**
35 * Megabit per second.
36 * (Decimal/SI)
37 */
38 MBPS(1_000_000L),
39 /**
40 * Gigabit per second.
41 * (Decimal/SI)
42 */
43 GBPS(1_000_000_000L);
44
45 private final long multiplier;
46
47 DataRateUnit(long multiplier) {
48 this.multiplier = multiplier;
49 }
50
51 /**
52 * Returns the multiplier to use, when converting value of this unit to bps.
53 *
54 * @return multiplier
55 */
56 public long multiplier() {
57 return multiplier;
58 }
59
60 /**
61 * Converts given value in this unit to bits per seconds.
62 *
63 * @param v data rate value
64 * @return {@code v} in bits per seconds
65 */
66 public long toBitsPerSecond(long v) {
67 return v * multiplier;
68 }
69
70 /**
71 * Converts given value in this unit to bits per seconds.
72 *
73 * @param v data rate value
74 * @return {@code v} in bits per seconds
75 */
76 public double toBitsPerSecond(double v) {
77 return v * multiplier;
78 }
79}