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 | /** |
| 45 | * Creates a new instance with given bandwidth in bps. |
| 46 | * |
| 47 | * @param bps bandwidth value to be assigned |
| 48 | * @return {@link Bandwidth} instance with given bandwidth |
| 49 | */ |
| 50 | public static Bandwidth bps(double bps) { |
| 51 | return new Bandwidth(bps); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Creates a new instance with given bandwidth in Kbps. |
| 56 | * |
| 57 | * @param kbps bandwidth value to be assigned |
| 58 | * @return {@link Bandwidth} instance with given bandwidth |
| 59 | */ |
| 60 | public static Bandwidth kbps(double kbps) { |
| 61 | return bps(kbps * 1_000L); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates a new instance with given bandwidth in Mbps. |
| 66 | * |
| 67 | * @param mbps bandwidth value to be assigned |
| 68 | * @return {@link Bandwidth} instance with given bandwidth |
| 69 | */ |
| 70 | public static Bandwidth mbps(double mbps) { |
| 71 | return bps(mbps * 1_000_000L); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Creates a new instance with given bandwidth in Gbps. |
| 76 | * |
| 77 | * @param gbps bandwidth value to be assigned |
| 78 | * @return {@link Bandwidth} instance with given bandwidth |
| 79 | */ |
| 80 | public static Bandwidth gbps(double gbps) { |
| 81 | return bps(gbps * 1_000_000_000L); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns bandwidth in bps. |
| 86 | * |
| 87 | * @return bandwidth in bps. |
| 88 | */ |
| 89 | public double bps() { |
| 90 | return bps; |
| 91 | } |
| 92 | |
Sho SHIMIZU | 5c73fd9 | 2015-05-08 17:11:46 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Returns a Bandwidth whose value is (this + value). |
| 95 | * |
| 96 | * @param value value to be added to this Frequency |
| 97 | * @return this + value |
| 98 | */ |
| 99 | public Bandwidth add(Bandwidth value) { |
| 100 | return bps(this.bps + value.bps); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns a Bandwidth whose value is (this - value). |
| 105 | * |
| 106 | * @param value value to be added to this Frequency |
| 107 | * @return this - value |
| 108 | */ |
| 109 | public Bandwidth subtract(Bandwidth value) { |
| 110 | return bps(this.bps - value.bps); |
| 111 | } |
| 112 | |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 113 | @Override |
Sho SHIMIZU | 2908d9e | 2015-05-08 16:40:06 -0700 | [diff] [blame] | 114 | public int compareTo(Bandwidth other) { |
| 115 | return ComparisonChain.start() |
| 116 | .compare(this.bps, other.bps) |
| 117 | .result(); |
| 118 | } |
| 119 | |
| 120 | @Override |
Sho SHIMIZU | 8169779 | 2015-05-08 11:09:38 -0700 | [diff] [blame] | 121 | public boolean equals(Object obj) { |
| 122 | if (obj instanceof Bandwidth) { |
| 123 | Bandwidth that = (Bandwidth) obj; |
| 124 | return Objects.equals(this.bps, that.bps); |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int hashCode() { |
| 131 | return Long.hashCode(Double.doubleToLongBits(bps)); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public String toString() { |
| 136 | return String.valueOf(this.bps); |
| 137 | } |
| 138 | } |