blob: c120e59b399621d54acfb7b3a56301f15de1ac90 [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;
23
24import static com.google.common.base.Preconditions.checkArgument;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080025
26/**
27 * Represents a resource path which specifies a resource which can be measured
28 * as a discrete unit. A VLAN ID and a MPLS label of a link are examples of the resource.
29 * <p>
30 * Note: This class is exposed to the public, but intended to be used in the resource API
31 * implementation only. It is not for resource API user.
32 * </p>
33 */
34@Beta
Sho SHIMIZU2a704512016-01-26 14:41:34 -080035public final class DiscreteResource implements Resource {
36 private final DiscreteResourceId id;
37
38 DiscreteResource(DiscreteResourceId id) {
39 this.id = id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080040 }
41
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080042 DiscreteResource() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080043 this.id = ResourceId.ROOT;
44 }
45
46 @Override
47 public DiscreteResourceId id() {
48 return id;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080049 }
50
51 /**
52 * The user of this methods must receive the return value as the correct type.
53 * Otherwise, this methods throws an exception.
54 *
55 * @param <T> type of the return value
56 * @return the volume of this resource
57 */
58 @SuppressWarnings("unchecked")
59 @Override
60 // TODO: consider receiving Class<T> as an argument. Which approach is convenient?
61 public <T> T volume() {
62 return (T) last();
63 }
64
65 @Override
Sho SHIMIZU9c02f3a2016-01-29 15:11:59 -080066 public boolean isTypeOf(Class<?> ancestorType) {
67 return id.components().stream()
68 .map(Object::getClass)
69 .filter(x -> x.equals(ancestorType))
70 .findAny()
71 .isPresent();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080072 }
73
74 @Override
75 public Object last() {
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080076 if (id.components().isEmpty()) {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080077 return null;
78 }
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080079 return id.components().get(id.components().size() - 1);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080080 }
81
82 @Override
83 public DiscreteResource child(Object child) {
84 checkArgument(!(child instanceof Class<?>));
85
Sho SHIMIZU460b9722016-01-28 10:48:26 -080086 return Resources.discrete(id.child(child)).resource();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080087 }
88
89 @Override
90 public ContinuousResource child(Class<?> child, double value) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -080091 return Resources.continuous(id.child(child)).resource(value);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080092 }
93
94 @Override
95 public Optional<DiscreteResource> parent() {
Sho SHIMIZU460b9722016-01-28 10:48:26 -080096 return id.parent().map(x -> Resources.discrete(x).resource());
Sho SHIMIZU2a704512016-01-26 14:41:34 -080097 }
98
99 @Override
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800100 public int hashCode() {
101 // the value returing from volume() is excluded due to optimization
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800102 return id.hashCode();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800103 }
104
105 @Override
106 public boolean equals(Object obj) {
107 if (this == obj) {
108 return true;
109 }
110 if (obj == null || getClass() != obj.getClass()) {
111 return false;
112 }
113 final DiscreteResource other = (DiscreteResource) obj;
114 // the value returing from volume() is excluded due to optimization
Sho SHIMIZU34b55b62016-01-27 12:49:43 -0800115 return Objects.equals(this.id, other.id);
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800116 }
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800117
118 @Override
119 public String toString() {
120 return MoreObjects.toStringHelper(this)
121 .add("id", id)
122 .add("volume", volume())
123 .toString();
124 }
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800125}