HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 1 | package org.onlab.util; |
| 2 | |
| 3 | import com.google.common.annotations.Beta; |
| 4 | |
| 5 | /** |
| 6 | * Data rate unit. |
| 7 | */ |
| 8 | @Beta |
| 9 | public enum DataRateUnit { |
| 10 | /** |
| 11 | * Bit per second. |
| 12 | */ |
| 13 | BPS(1L), |
| 14 | /** |
| 15 | * Kilobit per second. |
| 16 | * (Decimal/SI) |
| 17 | */ |
| 18 | KBPS(1_000L), |
| 19 | /** |
| 20 | * Megabit per second. |
| 21 | * (Decimal/SI) |
| 22 | */ |
| 23 | MBPS(1_000_000L), |
| 24 | /** |
| 25 | * Gigabit per second. |
| 26 | * (Decimal/SI) |
| 27 | */ |
| 28 | GBPS(1_000_000_000L); |
| 29 | |
| 30 | private final long multiplier; |
| 31 | |
| 32 | DataRateUnit(long multiplier) { |
| 33 | this.multiplier = multiplier; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns the multiplier to use, when converting value of this unit to bps. |
| 38 | * |
| 39 | * @return multiplier |
| 40 | */ |
| 41 | public long multiplier() { |
| 42 | return multiplier; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Converts given value in this unit to bits per seconds. |
| 47 | * |
| 48 | * @param v data rate value |
| 49 | * @return {@code v} in bits per seconds |
| 50 | */ |
| 51 | public long toBitsPerSecond(long v) { |
| 52 | return v * multiplier; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Converts given value in this unit to bits per seconds. |
| 57 | * |
| 58 | * @param v data rate value |
| 59 | * @return {@code v} in bits per seconds |
| 60 | */ |
| 61 | public double toBitsPerSecond(double v) { |
| 62 | return v * multiplier; |
| 63 | } |
| 64 | } |