blob: 92a98b8f02a9abdac5bbd1d67a657f6439bc6f36 [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;
22
23import static com.google.common.base.Preconditions.checkArgument;
24import static com.google.common.base.Preconditions.checkNotNull;
25
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080026/**
27 * ResourceId for {@link DiscreteResource}.
28 *
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 SHIMIZUf33b8932016-01-25 18:43:32 -080033public final class DiscreteResourceId extends ResourceId {
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080034 private final ImmutableList<Object> components;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080035
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080036 DiscreteResourceId(ImmutableList<Object> components) {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080037 this.components = components;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080038 }
39
40 DiscreteResourceId() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080041 this.components = ImmutableList.of();
42 }
43
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080044 ImmutableList<Object> components() {
45 return components;
46 }
47
Sho SHIMIZU2a704512016-01-26 14:41:34 -080048 @Override
49 public DiscreteResourceId child(Object child) {
50 checkArgument(!(child instanceof Class<?>));
51
52 return new DiscreteResourceId(ImmutableList.builder()
53 .addAll(components)
54 .add(child)
55 .build());
56 }
57
58 @Override
59 public ContinuousResourceId child(Class<?> child) {
60 checkNotNull(child);
61
62 return new ContinuousResourceId(ImmutableList.builder().addAll(components), child);
63 }
64
65 @Override
66 DiscreteResourceId parent() {
67 if (components.size() == 0) {
68 return null;
69 }
70 if (components.size() == 1) {
71 return ROOT;
72 } else {
73 return new DiscreteResourceId(components.subList(0, components.size() - 1));
74 }
75 }
76
77 @Override
78 public int hashCode() {
79 return components.hashCode();
80 }
81
82 @Override
83 public boolean equals(Object obj) {
84 if (this == obj) {
85 return true;
86 }
87 if (obj == null || getClass() != obj.getClass()) {
88 return false;
89 }
90 final DiscreteResourceId other = (DiscreteResourceId) obj;
91 return Objects.equals(this.components, other.components);
92 }
93
94 @Override
95 public String toString() {
96 return components.toString();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080097 }
98}