Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 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.onlab.util; |
| 17 | |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ComparisonChain; |
| 19 | |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 20 | /** |
| 21 | * Representation of bandwidth. |
| 22 | * Use the static factory method corresponding to the unit (like Kbps) you desire on instantiation. |
| 23 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 24 | public interface Bandwidth extends RichComparable<Bandwidth> { |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Creates a new instance with given bandwidth. |
| 28 | * |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 29 | * @param v bandwidth value |
| 30 | * @param unit {@link DataRateUnit} of {@code v} |
| 31 | * @return {@link Bandwidth} instance with given bandwidth |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 32 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 33 | static Bandwidth of(long v, DataRateUnit unit) { |
| 34 | return new LongBandwidth(unit.toBitsPerSecond(v)); |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /** |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 38 | * Creates a new instance with given bandwidth. |
| 39 | * |
| 40 | * @param v bandwidth value |
| 41 | * @param unit {@link DataRateUnit} of {@code v} |
| 42 | * @return {@link Bandwidth} instance with given bandwidth |
| 43 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 44 | static Bandwidth of(double v, DataRateUnit unit) { |
| 45 | return new DoubleBandwidth(unit.toBitsPerSecond(v)); |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 49 | * Creates a new instance with given bandwidth in bps. |
| 50 | * |
| 51 | * @param bps bandwidth value to be assigned |
| 52 | * @return {@link Bandwidth} instance with given bandwidth |
| 53 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 54 | static Bandwidth bps(long bps) { |
| 55 | return new LongBandwidth(bps); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Creates a new instance with given bandwidth in bps. |
| 60 | * |
| 61 | * @param bps bandwidth value to be assigned |
| 62 | * @return {@link Bandwidth} instance with given bandwidth |
| 63 | */ |
| 64 | static Bandwidth bps(double bps) { |
| 65 | return new DoubleBandwidth(bps); |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Creates a new instance with given bandwidth in Kbps. |
| 70 | * |
| 71 | * @param kbps bandwidth value to be assigned |
| 72 | * @return {@link Bandwidth} instance with given bandwidth |
| 73 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 74 | static Bandwidth kbps(long kbps) { |
| 75 | return bps(kbps * 1_000L); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates a new instance with given bandwidth in Kbps. |
| 80 | * |
| 81 | * @param kbps bandwidth value to be assigned |
| 82 | * @return {@link Bandwidth} instance with given bandwidth |
| 83 | */ |
| 84 | static Bandwidth kbps(double kbps) { |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 85 | return bps(kbps * 1_000L); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Creates a new instance with given bandwidth in Mbps. |
| 90 | * |
| 91 | * @param mbps bandwidth value to be assigned |
| 92 | * @return {@link Bandwidth} instance with given bandwidth |
| 93 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 94 | static Bandwidth mbps(long mbps) { |
| 95 | return bps(mbps * 1_000_000L); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Creates a new instance with given bandwidth in Mbps. |
| 100 | * |
| 101 | * @param mbps bandwidth value to be assigned |
| 102 | * @return {@link Bandwidth} instance with given bandwidth |
| 103 | */ |
| 104 | static Bandwidth mbps(double mbps) { |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 105 | return bps(mbps * 1_000_000L); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Creates a new instance with given bandwidth in Gbps. |
| 110 | * |
| 111 | * @param gbps bandwidth value to be assigned |
| 112 | * @return {@link Bandwidth} instance with given bandwidth |
| 113 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 114 | static Bandwidth gbps(long gbps) { |
| 115 | return bps(gbps * 1_000_000_000L); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Creates a new instance with given bandwidth in Gbps. |
| 120 | * |
| 121 | * @param gbps bandwidth value to be assigned |
| 122 | * @return {@link Bandwidth} instance with given bandwidth |
| 123 | */ |
| 124 | static Bandwidth gbps(double gbps) { |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 125 | return bps(gbps * 1_000_000_000L); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns bandwidth in bps. |
| 130 | * |
| 131 | * @return bandwidth in bps. |
| 132 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 133 | double bps(); |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 134 | |
Sho SHIMIZU | 5c73fd9 | 2015-05-08 17:11:46 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Returns a Bandwidth whose value is (this + value). |
| 137 | * |
| 138 | * @param value value to be added to this Frequency |
| 139 | * @return this + value |
| 140 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 141 | default Bandwidth add(Bandwidth value) { |
| 142 | return bps(this.bps() + value.bps()); |
Sho SHIMIZU | 5c73fd9 | 2015-05-08 17:11:46 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns a Bandwidth whose value is (this - value). |
| 147 | * |
| 148 | * @param value value to be added to this Frequency |
| 149 | * @return this - value |
| 150 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 151 | default Bandwidth subtract(Bandwidth value) { |
| 152 | return bps(this.bps() - value.bps()); |
Sho SHIMIZU | 5c73fd9 | 2015-05-08 17:11:46 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 155 | @Override |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 156 | default int compareTo(Bandwidth other) { |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 157 | return ComparisonChain.start() |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 158 | .compare(this.bps(), other.bps()) |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 159 | .result(); |
| 160 | } |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 161 | } |