blob: 0bfb3795529b407c56e4b35518a4e645119795f3 [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'Connor6de2e202015-05-21 14:30:41 -070016package org.onosproject.net.resource.link;
Toshio Koidec9051db2014-10-20 15:18:37 -070017
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070018import org.onlab.util.Bandwidth;
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070019import org.onlab.util.DataRateUnit;
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 SHIMIZU1c221ad2015-07-02 10:08:08 -070027public final class BandwidthResource implements 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 SHIMIZU6d01d3d2015-05-08 14:08:36 -070036 public BandwidthResource(Bandwidth bandwidth) {
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070037 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 /**
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070046 * Creates a new bandwidth resource.
47 *
48 * @param v amount of bandwidth to request
49 * @param unit {@link DataRateUnit} of {@code v}
50 * @return {@link BandwidthResource} instance with given bandwidth
51 */
52 public static BandwidthResource of(double v, DataRateUnit unit) {
53 return new BandwidthResource(Bandwidth.of(v, unit));
54 }
55
56 /**
Toshio Koidec9051db2014-10-20 15:18:37 -070057 * Returns bandwidth as a double value.
58 *
59 * @return bandwidth as a double value
60 */
61 public double toDouble() {
Sho SHIMIZUf81725b2015-05-08 13:36:02 -070062 return bandwidth.bps();
Toshio Koidec9051db2014-10-20 15:18:37 -070063 }
64
65 @Override
66 public boolean equals(Object obj) {
Sho SHIMIZU63feca72015-05-07 10:44:25 -070067 if (obj instanceof BandwidthResource) {
68 BandwidthResource that = (BandwidthResource) obj;
Toshio Koidec9051db2014-10-20 15:18:37 -070069 return Objects.equals(this.bandwidth, that.bandwidth);
70 }
71 return false;
72 }
73
74 @Override
75 public int hashCode() {
76 return Objects.hashCode(this.bandwidth);
77 }
78
79 @Override
80 public String toString() {
81 return String.valueOf(this.bandwidth);
82 }
83}