blob: 80c8f5971a49d4ab74e2d3f41c6b3d4feb292b37 [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
53 return new DiscreteResourceId(ImmutableList.builder()
54 .addAll(components)
55 .add(child)
56 .build());
57 }
58
59 @Override
60 public ContinuousResourceId child(Class<?> child) {
61 checkNotNull(child);
62
63 return new ContinuousResourceId(ImmutableList.builder().addAll(components), child);
64 }
65
66 @Override
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080067 public Optional<DiscreteResourceId> parent() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080068 if (components.size() == 0) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080069 return Optional.empty();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080070 }
71 if (components.size() == 1) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080072 return Optional.of(ROOT);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080073 } else {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080074 return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
Sho SHIMIZU2a704512016-01-26 14:41:34 -080075 }
76 }
77
78 @Override
79 public int hashCode() {
80 return components.hashCode();
81 }
82
83 @Override
84 public boolean equals(Object obj) {
85 if (this == obj) {
86 return true;
87 }
88 if (obj == null || getClass() != obj.getClass()) {
89 return false;
90 }
91 final DiscreteResourceId other = (DiscreteResourceId) obj;
92 return Objects.equals(this.components, other.components);
93 }
94
95 @Override
96 public String toString() {
97 return components.toString();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080098 }
99}