blob: 839d211d8a8fbacaa028227ea47f4cd39a150d0a [file] [log] [blame]
Sho SHIMIZU31f37ed2016-01-08 18:45:54 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZU31f37ed2016-01-08 18:45:54 -08003 *
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 com.google.common.collect.ComparisonChain;
19
20/**
21 * Representation of bandwidth.
22 * Use the static factory method corresponding to the unit (like Kbps) you desire on instantiation.
23 */
24final class LongBandwidth implements Bandwidth {
25
26 private final long bps;
27
28 /**
29 * Creates a new instance with given bandwidth.
30 *
31 * @param bps bandwidth value to be assigned
32 */
33 LongBandwidth(long bps) {
34 this.bps = bps;
35 }
36
37 // Constructor for serialization
38 private LongBandwidth() {
39 this.bps = 0;
40 }
41 /**
42 * Returns bandwidth in bps.
43 *
44 * @return bandwidth in bps.
45 */
46 public double bps() {
47 return bps;
48 }
49
50 /**
51 * Returns a Bandwidth whose value is (this + value).
52 *
53 * @param value value to be added to this Frequency
54 * @return this + value
55 */
56 public Bandwidth add(Bandwidth value) {
57 if (value instanceof LongBandwidth) {
58 return Bandwidth.bps(this.bps + ((LongBandwidth) value).bps);
59 }
60 return Bandwidth.bps(this.bps + value.bps());
61 }
62
63 /**
64 * Returns a Bandwidth whose value is (this - value).
65 *
66 * @param value value to be added to this Frequency
67 * @return this - value
68 */
69 public Bandwidth subtract(Bandwidth value) {
70 if (value instanceof LongBandwidth) {
71 return Bandwidth.bps(this.bps - ((LongBandwidth) value).bps);
72 }
73 return Bandwidth.bps(this.bps - value.bps());
74 }
75
76 @Override
77 public int compareTo(Bandwidth other) {
78 if (other instanceof LongBandwidth) {
79 return ComparisonChain.start()
80 .compare(this.bps, ((LongBandwidth) other).bps)
81 .result();
82 }
83 return ComparisonChain.start()
84 .compare(this.bps, other.bps())
85 .result();
86 }
87
88 @Override
89 public boolean equals(Object obj) {
90 if (obj == this) {
91 return true;
92 }
93
94 if (obj instanceof Bandwidth) {
95 return this.compareTo((Bandwidth) obj) == 0;
96 }
97
98 return false;
99 }
100
101 @Override
102 public int hashCode() {
103 return Long.hashCode(bps);
104 }
105
106 @Override
107 public String toString() {
108 return String.valueOf(this.bps);
109 }
110}