blob: 6a6d6c15b8f71e334412270501682896670d84a6 [file] [log] [blame]
Sho SHIMIZU81697792015-05-08 11:09:38 -07001/*
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 */
16package org.onlab.util;
17
18import 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 */
24public 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}