blob: f5301f734579bd5a91c923e3f82eaef9db31a8f9 [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
Sho SHIMIZU2a704512016-01-26 14:41:34 -080021import java.util.List;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080022import java.util.Objects;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080023import java.util.Optional;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080024
25/**
26 * Represents a resource path which specifies a resource which can be measured
27 * as continuous value. Bandwidth of a link is an example of the resource.
28 * <p>
29 * Note: This class is exposed to the public, but intended to be used in the resource API
30 * implementation only. It is not for resource API user.
31 */
32@Beta
Sho SHIMIZU2a704512016-01-26 14:41:34 -080033public final class ContinuousResource implements Resource {
34 private final ContinuousResourceId id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080035 private final double value;
36
Sho SHIMIZU2a704512016-01-26 14:41:34 -080037 ContinuousResource(ContinuousResourceId id, double value) {
38 this.id = id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080039 this.value = value;
40 }
41
Sho SHIMIZU7f8a7cb2016-01-27 15:09:13 -080042 // for serializer
43 ContinuousResource() {
44 this.id = null;
45 this.value = 0;
46 }
47
Sho SHIMIZU2a704512016-01-26 14:41:34 -080048 @Override
49 public ContinuousResourceId id() {
50 return id;
51 }
52
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080053 /**
54 * The user of this methods must receive the return value as Double or double.
55 * Otherwise, this methods throws an exception.
56 *
57 * @param <T> type of the return value
58 * @return the volume of this resource
59 */
60 @SuppressWarnings("unchecked")
61 @Override
62 public <T> T volume() {
63 return (T) Double.valueOf(value);
64 }
65
Sho SHIMIZU2a704512016-01-26 14:41:34 -080066 /**
67 * Returns the value of the resource amount.
68 *
69 * @return the value of the resource amount
70 */
71 // FIXME: overlapping a purpose with volume()
72 public double value() {
73 return value;
74 }
75
76 @Override
77 public List<Object> components() {
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080078 return id.components();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080079 }
80
81 @Override
82 public Object last() {
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080083 if (id.components().isEmpty()) {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080084 return null;
85 }
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080086 return id.components().get(id.components().size() - 1);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080087 }
88
89 @Override
90 public DiscreteResource child(Object child) {
91 throw new UnsupportedOperationException();
92 }
93
94 @Override
95 public ContinuousResource child(Class<?> child, double value) {
96 throw new UnsupportedOperationException();
97 }
98
99 @Override
100 public Optional<DiscreteResource> parent() {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800101 return id.parent().map(x -> Resource.discrete(x).resource());
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800102 }
103
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800104 @Override
105 public int hashCode() {
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800106 return Objects.hash(id, value);
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj) {
112 return true;
113 }
114 if (obj == null || getClass() != obj.getClass()) {
115 return false;
116 }
117 final ContinuousResource other = (ContinuousResource) obj;
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800118 return Objects.equals(this.id, other.id)
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800119 && Objects.equals(this.value, other.value);
120 }
121
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800122 @Override
123 public String toString() {
124 return MoreObjects.toStringHelper(this)
125 .add("id", id)
126 .add("volume", value)
127 .toString();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800128 }
129}