Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 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 | |
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 | import java.util.Objects; |
| 21 | |
| 22 | /** |
| 23 | * Representation of bandwidth. |
| 24 | * Use the static factory method corresponding to the unit (like Kbps) you desire on instantiation. |
| 25 | */ |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 26 | public final class Bandwidth implements RichComparable<Bandwidth> { |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 27 | |
| 28 | private final double bps; |
| 29 | |
| 30 | /** |
| 31 | * Creates a new instance with given bandwidth. |
| 32 | * |
| 33 | * @param bps bandwidth value to be assigned |
| 34 | */ |
| 35 | private Bandwidth(double bps) { |
| 36 | this.bps = bps; |
| 37 | } |
| 38 | |
| 39 | // Constructor for serialization |
| 40 | private Bandwidth() { |
| 41 | this.bps = 0; |
| 42 | } |
| 43 | |
| 44 | /** |
HIGUCHI Yuta | 501c465 | 2015-10-29 14:21:48 -0700 | [diff] [blame] | 45 | * Creates a new instance with given bandwidth. |
| 46 | * |
| 47 | * @param v bandwidth value |
| 48 | * @param unit {@link DataRateUnit} of {@code v} |
| 49 | * @return {@link Bandwidth} instance with given bandwidth |
| 50 | */ |
| 51 | public static Bandwidth of(double v, DataRateUnit unit) { |
| 52 | return new Bandwidth(unit.toBitsPerSecond(v)); |
| 53 | } |
| 54 | |
| 55 | /** |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 56 | * Creates a new instance with given bandwidth in bps. |
| 57 | * |
| 58 | * @param bps bandwidth value to be assigned |
| 59 | * @return {@link Bandwidth} instance with given bandwidth |
| 60 | */ |
| 61 | public static Bandwidth bps(double bps) { |
| 62 | return new Bandwidth(bps); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Creates a new instance with given bandwidth in Kbps. |
| 67 | * |
| 68 | * @param kbps bandwidth value to be assigned |
| 69 | * @return {@link Bandwidth} instance with given bandwidth |
| 70 | */ |
| 71 | public static Bandwidth kbps(double kbps) { |
| 72 | return bps(kbps * 1_000L); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates a new instance with given bandwidth in Mbps. |
| 77 | * |
| 78 | * @param mbps bandwidth value to be assigned |
| 79 | * @return {@link Bandwidth} instance with given bandwidth |
| 80 | */ |
| 81 | public static Bandwidth mbps(double mbps) { |
| 82 | return bps(mbps * 1_000_000L); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Creates a new instance with given bandwidth in Gbps. |
| 87 | * |
| 88 | * @param gbps bandwidth value to be assigned |
| 89 | * @return {@link Bandwidth} instance with given bandwidth |
| 90 | */ |
| 91 | public static Bandwidth gbps(double gbps) { |
| 92 | return bps(gbps * 1_000_000_000L); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns bandwidth in bps. |
| 97 | * |
| 98 | * @return bandwidth in bps. |
| 99 | */ |
| 100 | public double bps() { |
| 101 | return bps; |
| 102 | } |
| 103 | |
Sho SHIMIZU | 5c73fd9 | 2015-05-08 17:11:46 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Returns a Bandwidth whose value is (this + value). |
| 106 | * |
| 107 | * @param value value to be added to this Frequency |
| 108 | * @return this + value |
| 109 | */ |
| 110 | public Bandwidth add(Bandwidth value) { |
| 111 | return bps(this.bps + value.bps); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns a Bandwidth whose value is (this - value). |
| 116 | * |
| 117 | * @param value value to be added to this Frequency |
| 118 | * @return this - value |
| 119 | */ |
| 120 | public Bandwidth subtract(Bandwidth value) { |
| 121 | return bps(this.bps - value.bps); |
| 122 | } |
| 123 | |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 124 | @Override |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 125 | public int compareTo(Bandwidth other) { |
| 126 | return ComparisonChain.start() |
| 127 | .compare(this.bps, other.bps) |
| 128 | .result(); |
| 129 | } |
| 130 | |
| 131 | @Override |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 132 | public boolean equals(Object obj) { |
| 133 | if (obj instanceof Bandwidth) { |
| 134 | Bandwidth that = (Bandwidth) obj; |
| 135 | return Objects.equals(this.bps, that.bps); |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public int hashCode() { |
| 142 | return Long.hashCode(Double.doubleToLongBits(bps)); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public String toString() { |
| 147 | return String.valueOf(this.bps); |
| 148 | } |
| 149 | } |