blob: 0797dcd82783f7910a96621d29dd07058c217f8f [file] [log] [blame]
Sho SHIMIZU81697792015-05-08 11:09:38 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Sho SHIMIZU81697792015-05-08 11:09:38 -07003 *
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 */
16package org.onlab.util;
17
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -070018import com.google.common.collect.ComparisonChain;
19
Sho SHIMIZU81697792015-05-08 11:09:38 -070020/**
21 * Representation of bandwidth.
22 * Use the static factory method corresponding to the unit (like Kbps) you desire on instantiation.
23 */
Sho SHIMIZU31f37ed2016-01-08 18:45:54 -080024public interface Bandwidth extends RichComparable<Bandwidth> {
Sho SHIMIZU81697792015-05-08 11:09:38 -070025
26 /**
27 * Creates a new instance with given bandwidth.
28 *
Sho SHIMIZU31f37ed2016-01-08 18:45:54 -080029 * @param v bandwidth value
30 * @param unit {@link DataRateUnit} of {@code v}
31 * @return {@link Bandwidth} instance with given bandwidth
Sho SHIMIZU81697792015-05-08 11:09:38 -070032 */
Sho SHIMIZU31f37ed2016-01-08 18:45:54 -080033 static Bandwidth of(long v, DataRateUnit unit) {
34 return new LongBandwidth(unit.toBitsPerSecond(v));
Sho SHIMIZU81697792015-05-08 11:09:38 -070035 }
36
37 /**
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070038 * 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 SHIMIZU31f37ed2016-01-08 18:45:54 -080044 static Bandwidth of(double v, DataRateUnit unit) {
45 return new DoubleBandwidth(unit.toBitsPerSecond(v));
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070046 }
47
48 /**
Sho SHIMIZU81697792015-05-08 11:09:38 -070049 * 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 SHIMIZU31f37ed2016-01-08 18:45:54 -080054 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 SHIMIZU81697792015-05-08 11:09:38 -070066 }
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 SHIMIZU31f37ed2016-01-08 18:45:54 -080074 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 SHIMIZU81697792015-05-08 11:09:38 -070085 return bps(kbps * 1_000L);
86 }
87
88 /**
Konstantinos Kanonakis0a9031d2016-09-22 11:35:11 -050089 * 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 SHIMIZU81697792015-05-08 11:09:38 -0700109 * 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 SHIMIZU31f37ed2016-01-08 18:45:54 -0800114 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 SHIMIZU81697792015-05-08 11:09:38 -0700125 return bps(mbps * 1_000_000L);
126 }
127
128 /**
Konstantinos Kanonakis0a9031d2016-09-22 11:35:11 -0500129 * 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 SHIMIZU81697792015-05-08 11:09:38 -0700149 * 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 SHIMIZU31f37ed2016-01-08 18:45:54 -0800154 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 SHIMIZU81697792015-05-08 11:09:38 -0700165 return bps(gbps * 1_000_000_000L);
166 }
167
168 /**
Konstantinos Kanonakis0a9031d2016-09-22 11:35:11 -0500169 * 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 SHIMIZU81697792015-05-08 11:09:38 -0700189 * Returns bandwidth in bps.
190 *
191 * @return bandwidth in bps.
192 */
Sho SHIMIZU31f37ed2016-01-08 18:45:54 -0800193 double bps();
Sho SHIMIZU81697792015-05-08 11:09:38 -0700194
Sho SHIMIZU5c73fd92015-05-08 17:11:46 -0700195 /**
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 SHIMIZU31f37ed2016-01-08 18:45:54 -0800201 default Bandwidth add(Bandwidth value) {
202 return bps(this.bps() + value.bps());
Sho SHIMIZU5c73fd92015-05-08 17:11:46 -0700203 }
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 SHIMIZU31f37ed2016-01-08 18:45:54 -0800211 default Bandwidth subtract(Bandwidth value) {
212 return bps(this.bps() - value.bps());
Sho SHIMIZU5c73fd92015-05-08 17:11:46 -0700213 }
214
Sho SHIMIZU81697792015-05-08 11:09:38 -0700215 @Override
Sho SHIMIZU31f37ed2016-01-08 18:45:54 -0800216 default int compareTo(Bandwidth other) {
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -0700217 return ComparisonChain.start()
Sho SHIMIZU31f37ed2016-01-08 18:45:54 -0800218 .compare(this.bps(), other.bps())
Sho SHIMIZU2908d9e2015-05-08 16:40:06 -0700219 .result();
220 }
Sho SHIMIZU81697792015-05-08 11:09:38 -0700221}