blob: 70e011951913c4dc41fa3c05332c89862f2dbfce [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}.
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080029 */
30@Beta
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080031public final class DiscreteResourceId extends ResourceId {
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080032 private final ImmutableList<Object> components;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080033
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080034 DiscreteResourceId(ImmutableList<Object> components) {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080035 this.components = components;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080036 }
37
38 DiscreteResourceId() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080039 this.components = ImmutableList.of();
40 }
41
Sho SHIMIZU2486ddd2016-01-29 11:51:12 -080042 @Override
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080043 ImmutableList<Object> components() {
44 return components;
45 }
46
Sho SHIMIZU2a704512016-01-26 14:41:34 -080047 @Override
Sho SHIMIZU74ac9012016-02-12 10:03:21 -080048 String simpleTypeName() {
49 if (components.isEmpty()) {
50 return "Root";
51 }
52
53 return components.get(components.size() - 1).getClass().getSimpleName();
54 }
55
56 @Override
Sho SHIMIZU2a704512016-01-26 14:41:34 -080057 public DiscreteResourceId child(Object child) {
58 checkArgument(!(child instanceof Class<?>));
59
Sho SHIMIZU460b9722016-01-28 10:48:26 -080060 return Resources.discrete(this, child).id();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080061 }
62
63 @Override
64 public ContinuousResourceId child(Class<?> child) {
65 checkNotNull(child);
66
Sho SHIMIZU460b9722016-01-28 10:48:26 -080067 return Resources.continuous(this, child).id();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080068 }
69
70 @Override
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080071 public Optional<DiscreteResourceId> parent() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080072 if (components.size() == 0) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080073 return Optional.empty();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080074 }
75 if (components.size() == 1) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080076 return Optional.of(ROOT);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080077 } else {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080078 return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
Sho SHIMIZU2a704512016-01-26 14:41:34 -080079 }
80 }
81
82 @Override
83 public int hashCode() {
84 return components.hashCode();
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj) {
90 return true;
91 }
92 if (obj == null || getClass() != obj.getClass()) {
93 return false;
94 }
95 final DiscreteResourceId other = (DiscreteResourceId) obj;
96 return Objects.equals(this.components, other.components);
97 }
98
99 @Override
100 public String toString() {
101 return components.toString();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800102 }
103}