blob: 24b6551440aba454125d82970a6e454be862a068 [file] [log] [blame]
Sho SHIMIZUf33b8932016-01-25 18:43:32 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZUf33b8932016-01-25 18:43:32 -08003 *
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 */
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080017
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
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080024/**
Sho SHIMIZU37a038b2016-02-12 17:45:59 -080025 * ResourceId for {@link ContinuousResource}.
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080026 */
27@Beta
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080028public final class ContinuousResourceId extends ResourceId {
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080029 private final ImmutableList<Object> components;
Sho SHIMIZU2a704512016-01-26 14:41:34 -080030
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080031 // for printing purpose only (used in toString() implementation)
32 private final String name;
33
Sho SHIMIZU2a704512016-01-26 14:41:34 -080034 ContinuousResourceId(ImmutableList.Builder<Object> parentComponents, Class<?> last) {
35 this.components = parentComponents.add(last.getCanonicalName()).build();
36 this.name = last.getSimpleName();
37 }
38
Sho SHIMIZU7f8a7cb2016-01-27 15:09:13 -080039 // for serializer
40 ContinuousResourceId() {
41 this.components = ImmutableList.of();
42 this.name = "";
43 }
44
Sho SHIMIZU2486ddd2016-01-29 11:51:12 -080045 @Override
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080046 ImmutableList<Object> components() {
47 return components;
48 }
49
Sho SHIMIZU74ac9012016-02-12 10:03:21 -080050 @Override
51 String simpleTypeName() {
52 return name;
53 }
54
Sho SHIMIZUcaa2f7b2016-02-12 17:38:49 -080055 @Override
56 boolean isTypeOf(Class<?> type) {
57 String typeName = (String) lastComponent();
58 return typeName.equals(type.getCanonicalName());
59 }
60
61 @Override
62 boolean isSubTypeOf(Class<?> ancestor) {
63 String typeName = (String) lastComponent();
64 boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName());
65 boolean foundInAncestor = components.subList(0, components.size()).stream()
Sho SHIMIZU8add0f62017-03-16 18:32:59 -070066 .anyMatch(x -> ancestor.isAssignableFrom(x.getClass()));
Sho SHIMIZUcaa2f7b2016-02-12 17:38:49 -080067 return foundInAncestor || foundInLeaf;
68 }
69
Sho SHIMIZU2a704512016-01-26 14:41:34 -080070 /**
71 * {@inheritDoc}
72 *
73 * A child of a continuous-type resource is prohibited.
74 * {@link UnsupportedOperationException} is always thrown.
75 */
76 @Override
77 public DiscreteResourceId child(Object child) {
78 throw new UnsupportedOperationException();
79 }
80
81 /**
82 * {@inheritDoc}
83 *
84 * A child of a continuous-type resource is prohibited.
85 * {@link UnsupportedOperationException} is always thrown.
86 */
87 @Override
88 public ContinuousResourceId child(Class<?> child) {
89 throw new UnsupportedOperationException();
90 }
91
92 @Override
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080093 public Optional<DiscreteResourceId> parent() {
Jon Hallcbd1b392017-01-18 20:15:44 -080094 if (components.isEmpty()) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080095 return Optional.empty();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080096 }
97 if (components.size() == 1) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080098 return Optional.of(ROOT);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080099 } else {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -0800100 return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800101 }
102 }
103
Sho SHIMIZUcaa2f7b2016-02-12 17:38:49 -0800104 private Object lastComponent() {
105 return components.get(components.size() - 1);
106 }
107
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800108 @Override
109 public int hashCode() {
110 return components.hashCode();
111 }
112
113 @Override
114 public boolean equals(Object obj) {
115 if (this == obj) {
116 return true;
117 }
118 if (obj == null || getClass() != obj.getClass()) {
119 return false;
120 }
121 final ContinuousResourceId other = (ContinuousResourceId) obj;
122 return Objects.equals(this.components, other.components);
123 }
124
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800125 @Override
126 public String toString() {
127 // due to performance consideration, the value might need to be stored in a field
128 return ImmutableList.builder()
129 .addAll(components.subList(0, components.size() - 1))
130 .add(name)
131 .build().toString();
132 }
133}