blob: e7357687f97ba7f5a6c5f4903300a597bd61cc96 [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 */
16package org.onosproject.net.newresource;
17
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
24/**
25 * Represents a resource path which specifies a resource which can be measured
26 * as continuous value. Bandwidth of a link is an example of the resource.
27 * <p>
28 * Note: This class is exposed to the public, but intended to be used in the resource API
29 * implementation only. It is not for resource API user.
30 */
31@Beta
Sho SHIMIZU2a704512016-01-26 14:41:34 -080032public final class ContinuousResource implements Resource {
33 private final ContinuousResourceId id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080034 private final double value;
35
Sho SHIMIZU2a704512016-01-26 14:41:34 -080036 ContinuousResource(ContinuousResourceId id, double value) {
37 this.id = id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080038 this.value = value;
39 }
40
Sho SHIMIZU7f8a7cb2016-01-27 15:09:13 -080041 // for serializer
42 ContinuousResource() {
43 this.id = null;
44 this.value = 0;
45 }
46
Sho SHIMIZU2a704512016-01-26 14:41:34 -080047 @Override
48 public ContinuousResourceId id() {
49 return id;
50 }
51
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080052 /**
53 * The user of this methods must receive the return value as Double or double.
54 * Otherwise, this methods throws an exception.
55 *
56 * @param <T> type of the return value
57 * @return the volume of this resource
58 */
59 @SuppressWarnings("unchecked")
60 @Override
61 public <T> T volume() {
62 return (T) Double.valueOf(value);
63 }
64
Sho SHIMIZU2a704512016-01-26 14:41:34 -080065 /**
66 * Returns the value of the resource amount.
67 *
68 * @return the value of the resource amount
69 */
70 // FIXME: overlapping a purpose with volume()
71 public double value() {
72 return value;
73 }
74
75 @Override
Sho SHIMIZUb08d5862016-02-11 12:37:28 -080076 public boolean isSubTypeOf(Class<?> ancestor) {
Sho SHIMIZU9c02f3a2016-01-29 15:11:59 -080077 String typeName = (String) id.components().get(id.components().size() - 1);
Sho SHIMIZUb08d5862016-02-11 12:37:28 -080078 boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName());
Sho SHIMIZU9c02f3a2016-01-29 15:11:59 -080079 boolean foundInAncestor = id.components().subList(0, id.components().size()).stream()
80 .map(Object::getClass)
Sho SHIMIZUb08d5862016-02-11 12:37:28 -080081 .filter(x -> x.equals(ancestor))
Sho SHIMIZU9c02f3a2016-01-29 15:11:59 -080082 .findAny()
83 .isPresent();
84 return foundInAncestor || foundInLeaf;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080085 }
86
87 @Override
88 public Object last() {
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080089 if (id.components().isEmpty()) {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080090 return null;
91 }
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080092 return id.components().get(id.components().size() - 1);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080093 }
94
95 @Override
96 public DiscreteResource child(Object child) {
97 throw new UnsupportedOperationException();
98 }
99
100 @Override
101 public ContinuousResource child(Class<?> child, double value) {
102 throw new UnsupportedOperationException();
103 }
104
105 @Override
106 public Optional<DiscreteResource> parent() {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800107 return id.parent().map(x -> Resources.discrete(x).resource());
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800108 }
109
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800110 @Override
111 public int hashCode() {
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800112 return Objects.hash(id, value);
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (obj == null || getClass() != obj.getClass()) {
121 return false;
122 }
123 final ContinuousResource other = (ContinuousResource) obj;
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800124 return Objects.equals(this.id, other.id)
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800125 && Objects.equals(this.value, other.value);
126 }
127
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800128 @Override
129 public String toString() {
130 return MoreObjects.toStringHelper(this)
131 .add("id", id)
132 .add("volume", value)
133 .toString();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800134 }
135}