Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | /** |
Konstantinos Kanonakis | 0a9031d | 2016-09-22 11:35:11 -0500 | [diff] [blame] | 89 | * Creates a new instance with given bandwidth in KBps. |
| 90 | * |
| 91 | * @param kBps bandwidth value to be assigned |
| 92 | * @return {@link Bandwidth} instance with given bandwidth |
| 93 | */ |
| 94 | static Bandwidth kBps(long kBps) { |
| 95 | return bps(kBps * 8_000L); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Creates a new instance with given bandwidth in KBps. |
| 100 | * |
| 101 | * @param kBps bandwidth value to be assigned |
| 102 | * @return {@link Bandwidth} instance with given bandwidth |
| 103 | */ |
| 104 | static Bandwidth kBps(double kBps) { |
| 105 | return bps(kBps * 8_000L); |
| 106 | } |
| 107 | |
| 108 | /** |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 109 | * Creates a new instance with given bandwidth in Mbps. |
| 110 | * |
| 111 | * @param mbps 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 mbps(long mbps) { |
| 115 | return bps(mbps * 1_000_000L); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Creates a new instance with given bandwidth in Mbps. |
| 120 | * |
| 121 | * @param mbps bandwidth value to be assigned |
| 122 | * @return {@link Bandwidth} instance with given bandwidth |
| 123 | */ |
| 124 | static Bandwidth mbps(double mbps) { |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 125 | return bps(mbps * 1_000_000L); |
| 126 | } |
| 127 | |
| 128 | /** |
Konstantinos Kanonakis | 0a9031d | 2016-09-22 11:35:11 -0500 | [diff] [blame] | 129 | * Creates a new instance with given bandwidth in MBps. |
| 130 | * |
| 131 | * @param mBps bandwidth value to be assigned |
| 132 | * @return {@link Bandwidth} instance with given bandwidth |
| 133 | */ |
| 134 | static Bandwidth mBps(long mBps) { |
| 135 | return bps(mBps * 8_000_000L); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Creates a new instance with given bandwidth in MBps. |
| 140 | * |
| 141 | * @param mBps bandwidth value to be assigned |
| 142 | * @return {@link Bandwidth} instance with given bandwidth |
| 143 | */ |
| 144 | static Bandwidth mBps(double mBps) { |
| 145 | return bps(mBps * 8_000_000L); |
| 146 | } |
| 147 | |
| 148 | /** |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 149 | * Creates a new instance with given bandwidth in Gbps. |
| 150 | * |
| 151 | * @param gbps bandwidth value to be assigned |
| 152 | * @return {@link Bandwidth} instance with given bandwidth |
| 153 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 154 | static Bandwidth gbps(long gbps) { |
| 155 | return bps(gbps * 1_000_000_000L); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Creates a new instance with given bandwidth in Gbps. |
| 160 | * |
| 161 | * @param gbps bandwidth value to be assigned |
| 162 | * @return {@link Bandwidth} instance with given bandwidth |
| 163 | */ |
| 164 | static Bandwidth gbps(double gbps) { |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 165 | return bps(gbps * 1_000_000_000L); |
| 166 | } |
| 167 | |
| 168 | /** |
Konstantinos Kanonakis | 0a9031d | 2016-09-22 11:35:11 -0500 | [diff] [blame] | 169 | * Creates a new instance with given bandwidth in GBps. |
| 170 | * |
| 171 | * @param gBps bandwidth value to be assigned |
| 172 | * @return {@link Bandwidth} instance with given bandwidth |
| 173 | */ |
| 174 | static Bandwidth gBps(long gBps) { |
| 175 | return bps(gBps * 8_000_000_000L); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Creates a new instance with given bandwidth in GBps. |
| 180 | * |
| 181 | * @param gBps bandwidth value to be assigned |
| 182 | * @return {@link Bandwidth} instance with given bandwidth |
| 183 | */ |
| 184 | static Bandwidth gBps(double gBps) { |
| 185 | return bps(gBps * 8_000_000_000L); |
| 186 | } |
| 187 | |
| 188 | /** |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 189 | * Returns bandwidth in bps. |
| 190 | * |
| 191 | * @return bandwidth in bps. |
| 192 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 193 | double bps(); |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 194 | |
Sho SHIMIZU | 5c73fd9 | 2015-05-08 17:11:46 -0700 | [diff] [blame] | 195 | /** |
| 196 | * Returns a Bandwidth whose value is (this + value). |
| 197 | * |
| 198 | * @param value value to be added to this Frequency |
| 199 | * @return this + value |
| 200 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 201 | default Bandwidth add(Bandwidth value) { |
| 202 | return bps(this.bps() + value.bps()); |
Sho SHIMIZU | 5c73fd9 | 2015-05-08 17:11:46 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Returns a Bandwidth whose value is (this - value). |
| 207 | * |
| 208 | * @param value value to be added to this Frequency |
| 209 | * @return this - value |
| 210 | */ |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 211 | default Bandwidth subtract(Bandwidth value) { |
| 212 | return bps(this.bps() - value.bps()); |
Sho SHIMIZU | 5c73fd9 | 2015-05-08 17:11:46 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 215 | @Override |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 216 | default int compareTo(Bandwidth other) { |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 217 | return ComparisonChain.start() |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 218 | .compare(this.bps(), other.bps()) |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 219 | .result(); |
| 220 | } |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 221 | } |