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