Committed by
Gerrit Code Review
Define Bandwidth class to represent bandwidth with explicit its unit
Change-Id: I863fea61fa5e70edfa53d11e720e400d100131c8
Showing
4 changed files
with
151 additions
and
0 deletions
... | @@ -223,6 +223,7 @@ public final class KryoNamespaces { | ... | @@ -223,6 +223,7 @@ public final class KryoNamespaces { |
223 | .register(new MacAddressSerializer(), MacAddress.class) | 223 | .register(new MacAddressSerializer(), MacAddress.class) |
224 | .register(VlanId.class) | 224 | .register(VlanId.class) |
225 | .register(Frequency.class) | 225 | .register(Frequency.class) |
226 | + .register(org.onlab.util.Bandwidth.class) | ||
226 | .build(); | 227 | .build(); |
227 | 228 | ||
228 | /** | 229 | /** | ... | ... |
... | @@ -366,6 +366,11 @@ public class KryoSerializerTest { | ... | @@ -366,6 +366,11 @@ public class KryoSerializerTest { |
366 | } | 366 | } |
367 | 367 | ||
368 | @Test | 368 | @Test |
369 | + public void testBandwidth() { | ||
370 | + testSerializedEquals(org.onlab.util.Bandwidth.mbps(1000.0)); | ||
371 | + } | ||
372 | + | ||
373 | + @Test | ||
369 | public void testLambdaConstraint() { | 374 | public void testLambdaConstraint() { |
370 | testSerializable(new LambdaConstraint(LambdaResource.valueOf(1))); | 375 | testSerializable(new LambdaConstraint(LambdaResource.valueOf(1))); |
371 | } | 376 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 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 | + */ | ||
16 | +package org.onlab.util; | ||
17 | + | ||
18 | +import java.util.Objects; | ||
19 | + | ||
20 | +/** | ||
21 | + * Representation of bandwidth. | ||
22 | + * Use the static factory method corresponding to the unit (like Kbps) you desire on instantiation. | ||
23 | + */ | ||
24 | +public final class Bandwidth { | ||
25 | + | ||
26 | + private final double bps; | ||
27 | + | ||
28 | + /** | ||
29 | + * Creates a new instance with given bandwidth. | ||
30 | + * | ||
31 | + * @param bps bandwidth value to be assigned | ||
32 | + */ | ||
33 | + private Bandwidth(double bps) { | ||
34 | + this.bps = bps; | ||
35 | + } | ||
36 | + | ||
37 | + // Constructor for serialization | ||
38 | + private Bandwidth() { | ||
39 | + this.bps = 0; | ||
40 | + } | ||
41 | + | ||
42 | + /** | ||
43 | + * Creates a new instance with given bandwidth in bps. | ||
44 | + * | ||
45 | + * @param bps bandwidth value to be assigned | ||
46 | + * @return {@link Bandwidth} instance with given bandwidth | ||
47 | + */ | ||
48 | + public static Bandwidth bps(double bps) { | ||
49 | + return new Bandwidth(bps); | ||
50 | + } | ||
51 | + | ||
52 | + /** | ||
53 | + * Creates a new instance with given bandwidth in Kbps. | ||
54 | + * | ||
55 | + * @param kbps bandwidth value to be assigned | ||
56 | + * @return {@link Bandwidth} instance with given bandwidth | ||
57 | + */ | ||
58 | + public static Bandwidth kbps(double kbps) { | ||
59 | + return bps(kbps * 1_000L); | ||
60 | + } | ||
61 | + | ||
62 | + /** | ||
63 | + * Creates a new instance with given bandwidth in Mbps. | ||
64 | + * | ||
65 | + * @param mbps bandwidth value to be assigned | ||
66 | + * @return {@link Bandwidth} instance with given bandwidth | ||
67 | + */ | ||
68 | + public static Bandwidth mbps(double mbps) { | ||
69 | + return bps(mbps * 1_000_000L); | ||
70 | + } | ||
71 | + | ||
72 | + /** | ||
73 | + * Creates a new instance with given bandwidth in Gbps. | ||
74 | + * | ||
75 | + * @param gbps bandwidth value to be assigned | ||
76 | + * @return {@link Bandwidth} instance with given bandwidth | ||
77 | + */ | ||
78 | + public static Bandwidth gbps(double gbps) { | ||
79 | + return bps(gbps * 1_000_000_000L); | ||
80 | + } | ||
81 | + | ||
82 | + /** | ||
83 | + * Returns bandwidth in bps. | ||
84 | + * | ||
85 | + * @return bandwidth in bps. | ||
86 | + */ | ||
87 | + public double bps() { | ||
88 | + return bps; | ||
89 | + } | ||
90 | + | ||
91 | + @Override | ||
92 | + public boolean equals(Object obj) { | ||
93 | + if (obj instanceof Bandwidth) { | ||
94 | + Bandwidth that = (Bandwidth) obj; | ||
95 | + return Objects.equals(this.bps, that.bps); | ||
96 | + } | ||
97 | + return false; | ||
98 | + } | ||
99 | + | ||
100 | + @Override | ||
101 | + public int hashCode() { | ||
102 | + return Long.hashCode(Double.doubleToLongBits(bps)); | ||
103 | + } | ||
104 | + | ||
105 | + @Override | ||
106 | + public String toString() { | ||
107 | + return String.valueOf(this.bps); | ||
108 | + } | ||
109 | +} |
1 | +/* | ||
2 | + * Copyright 2015 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 | + */ | ||
16 | +package org.onlab.util; | ||
17 | + | ||
18 | +import com.google.common.testing.EqualsTester; | ||
19 | +import org.junit.Test; | ||
20 | + | ||
21 | +/** | ||
22 | + * Unit tests for Bandwidth. | ||
23 | + */ | ||
24 | +public class BandwidthTest { | ||
25 | + /** | ||
26 | + * Tests equality of Bandwidth instances. | ||
27 | + */ | ||
28 | + @Test | ||
29 | + public void testEquality() { | ||
30 | + new EqualsTester() | ||
31 | + .addEqualityGroup(Bandwidth.kbps(1000.0), Bandwidth.kbps(1000.0), Bandwidth.mbps(1.0)) | ||
32 | + .addEqualityGroup(Bandwidth.gbps(1.0)) | ||
33 | + .testEquals(); | ||
34 | + } | ||
35 | + | ||
36 | +} |
-
Please register or login to post a comment