blob: 1d405a89c7480f7589be3d447f7b33e0d0f63664 [file] [log] [blame]
Sho SHIMIZUf33b8932016-01-25 18:43:32 -08001/*
2 * Copyright 2016 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 */
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080017
18import com.google.common.annotations.Beta;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080019import com.google.common.base.MoreObjects;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080020
21import java.util.Objects;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080022import java.util.Optional;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080023
Sho SHIMIZU0e1a4762016-02-11 13:04:41 -080024import static com.google.common.base.Preconditions.checkNotNull;
25
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080026/**
27 * Represents a resource path which specifies a resource which can be measured
28 * as continuous value. Bandwidth of a link is an example of the resource.
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080029 */
30@Beta
Sho SHIMIZU2a704512016-01-26 14:41:34 -080031public final class ContinuousResource implements Resource {
32 private final ContinuousResourceId id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080033 private final double value;
34
Sho SHIMIZU2a704512016-01-26 14:41:34 -080035 ContinuousResource(ContinuousResourceId id, double value) {
36 this.id = id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080037 this.value = value;
38 }
39
Sho SHIMIZU7f8a7cb2016-01-27 15:09:13 -080040 // for serializer
41 ContinuousResource() {
42 this.id = null;
43 this.value = 0;
44 }
45
Sho SHIMIZU2a704512016-01-26 14:41:34 -080046 @Override
47 public ContinuousResourceId id() {
48 return id;
49 }
50
Sho SHIMIZU003ed322016-02-11 12:58:42 -080051 @Override
Sho SHIMIZU74ac9012016-02-12 10:03:21 -080052 public String simpleTypeName() {
53 return id.simpleTypeName();
54 }
55
56 @Override
Sho SHIMIZU003ed322016-02-11 12:58:42 -080057 public boolean isTypeOf(Class<?> type) {
58 checkNotNull(type);
59
Sho SHIMIZUcaa2f7b2016-02-12 17:38:49 -080060 return id.isTypeOf(type);
Sho SHIMIZU003ed322016-02-11 12:58:42 -080061 }
62
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080063 /**
Sho SHIMIZU2a704512016-01-26 14:41:34 -080064 * Returns the value of the resource amount.
65 *
66 * @return the value of the resource amount
67 */
Sho SHIMIZU2a704512016-01-26 14:41:34 -080068 public double value() {
69 return value;
70 }
71
72 @Override
Sho SHIMIZUb08d5862016-02-11 12:37:28 -080073 public boolean isSubTypeOf(Class<?> ancestor) {
Sho SHIMIZU0e1a4762016-02-11 13:04:41 -080074 checkNotNull(ancestor);
75
Sho SHIMIZUcaa2f7b2016-02-12 17:38:49 -080076 return id.isSubTypeOf(ancestor);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080077 }
78
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -080079 /**
80 * {@inheritDoc}
81 *
82 * A user must specify Double.class or double.class to avoid an empty value.
83 */
84 @Override
85 public <T> Optional<T> valueAs(Class<T> type) {
86 checkNotNull(type);
87
88 if (type == Object.class || type == double.class || type == Double.class) {
89 @SuppressWarnings("unchecked")
90 T value = (T) Double.valueOf(this.value);
91 return Optional.of(value);
92 }
93
94 return Optional.empty();
95 }
96
Sho SHIMIZU2a704512016-01-26 14:41:34 -080097 @Override
Sho SHIMIZU2a704512016-01-26 14:41:34 -080098 public DiscreteResource child(Object child) {
99 throw new UnsupportedOperationException();
100 }
101
102 @Override
103 public ContinuousResource child(Class<?> child, double value) {
104 throw new UnsupportedOperationException();
105 }
106
107 @Override
108 public Optional<DiscreteResource> parent() {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800109 return id.parent().map(x -> Resources.discrete(x).resource());
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800110 }
111
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800112 @Override
113 public int hashCode() {
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800114 return Objects.hash(id, value);
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800115 }
116
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj) {
120 return true;
121 }
122 if (obj == null || getClass() != obj.getClass()) {
123 return false;
124 }
125 final ContinuousResource other = (ContinuousResource) obj;
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800126 return Objects.equals(this.id, other.id)
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800127 && Objects.equals(this.value, other.value);
128 }
129
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800130 @Override
131 public String toString() {
132 return MoreObjects.toStringHelper(this)
133 .add("id", id)
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -0800134 .add("value", value)
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800135 .toString();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800136 }
137}