blob: 4b83344897093150bfe58ceffb768df8a0a234b0 [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;
19import com.google.common.collect.ImmutableList;
20
Sho SHIMIZU2a704512016-01-26 14:41:34 -080021import java.util.Objects;
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080022import java.util.Optional;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080023
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080027/**
28 * ResourceId for {@link DiscreteResource}.
29 *
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 */
33@Beta
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080034public final class DiscreteResourceId extends ResourceId {
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080035 private final ImmutableList<Object> components;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080036
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080037 DiscreteResourceId(ImmutableList<Object> components) {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080038 this.components = components;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080039 }
40
41 DiscreteResourceId() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080042 this.components = ImmutableList.of();
43 }
44
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080045 ImmutableList<Object> components() {
46 return components;
47 }
48
Sho SHIMIZU2a704512016-01-26 14:41:34 -080049 @Override
50 public DiscreteResourceId child(Object child) {
51 checkArgument(!(child instanceof Class<?>));
52
Sho SHIMIZU460b9722016-01-28 10:48:26 -080053 return Resources.discrete(this, child).id();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080054 }
55
56 @Override
57 public ContinuousResourceId child(Class<?> child) {
58 checkNotNull(child);
59
Sho SHIMIZU460b9722016-01-28 10:48:26 -080060 return Resources.continuous(this, child).id();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080061 }
62
63 @Override
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080064 public Optional<DiscreteResourceId> parent() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080065 if (components.size() == 0) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080066 return Optional.empty();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080067 }
68 if (components.size() == 1) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080069 return Optional.of(ROOT);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080070 } else {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080071 return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
Sho SHIMIZU2a704512016-01-26 14:41:34 -080072 }
73 }
74
75 @Override
76 public int hashCode() {
77 return components.hashCode();
78 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (this == obj) {
83 return true;
84 }
85 if (obj == null || getClass() != obj.getClass()) {
86 return false;
87 }
88 final DiscreteResourceId other = (DiscreteResourceId) obj;
89 return Objects.equals(this.components, other.components);
90 }
91
92 @Override
93 public String toString() {
94 return components.toString();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080095 }
96}