blob: 0f3919847bc038c987cbcbb3f89a56b31b9160b9 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.resource;
Toshio Koidec9051db2014-10-20 15:18:37 -070017
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070018import org.onlab.util.Bandwidth;
19
Toshio Koidec9051db2014-10-20 15:18:37 -070020import java.util.Objects;
21
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070022import static com.google.common.base.Preconditions.checkNotNull;
23
Toshio Koidec9051db2014-10-20 15:18:37 -070024/**
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080025 * Representation of bandwidth resource in bps.
Toshio Koidec9051db2014-10-20 15:18:37 -070026 */
Sho SHIMIZU63feca72015-05-07 10:44:25 -070027public final class BandwidthResource extends LinkResource {
Toshio Koidec9051db2014-10-20 15:18:37 -070028
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070029 private final Bandwidth bandwidth;
Toshio Koidec9051db2014-10-20 15:18:37 -070030
31 /**
32 * Creates a new instance with given bandwidth.
33 *
34 * @param bandwidth bandwidth value to be assigned
35 */
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070036 private BandwidthResource(Bandwidth bandwidth) {
37 this.bandwidth = checkNotNull(bandwidth);
Toshio Koidec9051db2014-10-20 15:18:37 -070038 }
39
Thomas Vachuska7d0032b2014-11-04 17:39:57 -080040 // Constructor for serialization
Sho SHIMIZU63feca72015-05-07 10:44:25 -070041 private BandwidthResource() {
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070042 this.bandwidth = null;
Thomas Vachuska7d0032b2014-11-04 17:39:57 -080043 }
44
Toshio Koidec9051db2014-10-20 15:18:37 -070045 /**
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080046 * Creates a new instance with given bandwidth in bps.
Toshio Koidec9051db2014-10-20 15:18:37 -070047 *
48 * @param bandwidth bandwidth value to be assigned
Sho SHIMIZU63feca72015-05-07 10:44:25 -070049 * @return {@link BandwidthResource} instance with given bandwidth
Toshio Koidec9051db2014-10-20 15:18:37 -070050 */
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080051 @Deprecated
Sho SHIMIZU63feca72015-05-07 10:44:25 -070052 public static BandwidthResource valueOf(double bandwidth) {
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080053 return bps(bandwidth);
54 }
55
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070056 public static BandwidthResource from(Bandwidth bandwidth) {
57 return new BandwidthResource(bandwidth);
58 }
59
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080060 /**
61 * Creates a new instance with given bandwidth in bps.
62 *
Sho SHIMIZU9a6ae382015-05-08 13:32:19 -070063 * @param bps bandwidth value to be assigned
Sho SHIMIZU63feca72015-05-07 10:44:25 -070064 * @return {@link BandwidthResource} instance with given bandwidth
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080065 */
Sho SHIMIZU9a6ae382015-05-08 13:32:19 -070066 public static BandwidthResource bps(double bps) {
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070067 return from(Bandwidth.bps(bps));
Toshio Koidec9051db2014-10-20 15:18:37 -070068 }
69
70 /**
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080071 * Creates a new instance with given bandwidth in Kbps.
72 *
Sho SHIMIZU9a6ae382015-05-08 13:32:19 -070073 * @param kbps bandwidth value to be assigned
Sho SHIMIZU63feca72015-05-07 10:44:25 -070074 * @return {@link BandwidthResource} instance with given bandwidth
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080075 */
Sho SHIMIZU9a6ae382015-05-08 13:32:19 -070076 public static BandwidthResource kbps(double kbps) {
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070077 return from(Bandwidth.kbps(kbps));
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080078 }
79
80 /**
81 * Creates a new instance with given bandwidth in Mbps.
82 *
Sho SHIMIZU9a6ae382015-05-08 13:32:19 -070083 * @param mbps bandwidth value to be assigned
Sho SHIMIZU63feca72015-05-07 10:44:25 -070084 * @return {@link BandwidthResource} instance with given bandwidth
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080085 */
Sho SHIMIZU9a6ae382015-05-08 13:32:19 -070086 public static BandwidthResource mbps(double mbps) {
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070087 return from(Bandwidth.mbps(mbps));
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080088 }
89
90 /**
91 * Creates a new instance with given bandwidth in Gbps.
92 *
Sho SHIMIZU9a6ae382015-05-08 13:32:19 -070093 * @param gbps bandwidth value to be assigned
Sho SHIMIZU63feca72015-05-07 10:44:25 -070094 * @return {@link BandwidthResource} instance with given bandwidth
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080095 */
Sho SHIMIZU9a6ae382015-05-08 13:32:19 -070096 public static BandwidthResource gbps(double gbps) {
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070097 return from(Bandwidth.gbps(gbps));
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080098 }
99
100 /**
Toshio Koidec9051db2014-10-20 15:18:37 -0700101 * Returns bandwidth as a double value.
102 *
103 * @return bandwidth as a double value
104 */
105 public double toDouble() {
Sho SHIMIZUf81725b2015-05-08 13:36:02 -0700106 return bandwidth.bps();
Toshio Koidec9051db2014-10-20 15:18:37 -0700107 }
108
109 @Override
110 public boolean equals(Object obj) {
Sho SHIMIZU63feca72015-05-07 10:44:25 -0700111 if (obj instanceof BandwidthResource) {
112 BandwidthResource that = (BandwidthResource) obj;
Toshio Koidec9051db2014-10-20 15:18:37 -0700113 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}