Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
Toshio Koide | c9051db | 2014-10-20 15:18:37 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | /** |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 21 | * Representation of bandwidth resource in bps. |
Toshio Koide | c9051db | 2014-10-20 15:18:37 -0700 | [diff] [blame] | 22 | */ |
| 23 | public final class Bandwidth extends LinkResource { |
| 24 | |
| 25 | private final double bandwidth; |
| 26 | |
| 27 | /** |
| 28 | * Creates a new instance with given bandwidth. |
| 29 | * |
| 30 | * @param bandwidth bandwidth value to be assigned |
| 31 | */ |
| 32 | private Bandwidth(double bandwidth) { |
| 33 | this.bandwidth = bandwidth; |
| 34 | } |
| 35 | |
Thomas Vachuska | 7d0032b | 2014-11-04 17:39:57 -0800 | [diff] [blame] | 36 | // Constructor for serialization |
| 37 | private Bandwidth() { |
| 38 | this.bandwidth = 0; |
| 39 | } |
| 40 | |
Toshio Koide | c9051db | 2014-10-20 15:18:37 -0700 | [diff] [blame] | 41 | /** |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 42 | * Creates a new instance with given bandwidth in bps. |
Toshio Koide | c9051db | 2014-10-20 15:18:37 -0700 | [diff] [blame] | 43 | * |
| 44 | * @param bandwidth bandwidth value to be assigned |
| 45 | * @return {@link Bandwidth} instance with given bandwidth |
| 46 | */ |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 47 | @Deprecated |
Toshio Koide | c9051db | 2014-10-20 15:18:37 -0700 | [diff] [blame] | 48 | public static Bandwidth valueOf(double bandwidth) { |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 49 | return bps(bandwidth); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Creates a new instance with given bandwidth in bps. |
| 54 | * |
| 55 | * @param bandwidth bandwidth value to be assigned |
| 56 | * @return {@link Bandwidth} instance with given bandwidth |
| 57 | */ |
| 58 | public static Bandwidth bps(double bandwidth) { |
Toshio Koide | c9051db | 2014-10-20 15:18:37 -0700 | [diff] [blame] | 59 | return new Bandwidth(bandwidth); |
| 60 | } |
| 61 | |
| 62 | /** |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 63 | * Creates a new instance with given bandwidth in Kbps. |
| 64 | * |
| 65 | * @param bandwidth bandwidth value to be assigned |
| 66 | * @return {@link Bandwidth} instance with given bandwidth |
| 67 | */ |
| 68 | public static Bandwidth kbps(double bandwidth) { |
| 69 | return new Bandwidth(bandwidth * 1_000L); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Creates a new instance with given bandwidth in Mbps. |
| 74 | * |
| 75 | * @param bandwidth bandwidth value to be assigned |
| 76 | * @return {@link Bandwidth} instance with given bandwidth |
| 77 | */ |
| 78 | public static Bandwidth mbps(double bandwidth) { |
| 79 | return new Bandwidth(bandwidth * 1_000_000L); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Creates a new instance with given bandwidth in Gbps. |
| 84 | * |
| 85 | * @param bandwidth bandwidth value to be assigned |
| 86 | * @return {@link Bandwidth} instance with given bandwidth |
| 87 | */ |
| 88 | public static Bandwidth gbps(double bandwidth) { |
| 89 | return new Bandwidth(bandwidth * 1_000_000_000L); |
| 90 | } |
| 91 | |
| 92 | /** |
Toshio Koide | c9051db | 2014-10-20 15:18:37 -0700 | [diff] [blame] | 93 | * Returns bandwidth as a double value. |
| 94 | * |
| 95 | * @return bandwidth as a double value |
| 96 | */ |
| 97 | public double toDouble() { |
| 98 | return bandwidth; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean equals(Object obj) { |
| 103 | if (obj instanceof Bandwidth) { |
| 104 | Bandwidth that = (Bandwidth) obj; |
| 105 | return Objects.equals(this.bandwidth, that.bandwidth); |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int hashCode() { |
| 112 | return Objects.hashCode(this.bandwidth); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public String toString() { |
| 117 | return String.valueOf(this.bandwidth); |
| 118 | } |
| 119 | } |