blob: 46237e00551b5bab7c0a968f8ea2de26e5810e45 [file] [log] [blame]
Sho SHIMIZUf33b8932016-01-25 18:43:32 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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()
66 .filter(x -> ancestor.isAssignableFrom(x.getClass()))
67 .findAny()
68 .isPresent();
69 return foundInAncestor || foundInLeaf;
70 }
71
Sho SHIMIZU2a704512016-01-26 14:41:34 -080072 /**
73 * {@inheritDoc}
74 *
75 * A child of a continuous-type resource is prohibited.
76 * {@link UnsupportedOperationException} is always thrown.
77 */
78 @Override
79 public DiscreteResourceId child(Object child) {
80 throw new UnsupportedOperationException();
81 }
82
83 /**
84 * {@inheritDoc}
85 *
86 * A child of a continuous-type resource is prohibited.
87 * {@link UnsupportedOperationException} is always thrown.
88 */
89 @Override
90 public ContinuousResourceId child(Class<?> child) {
91 throw new UnsupportedOperationException();
92 }
93
94 @Override
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080095 public Optional<DiscreteResourceId> parent() {
Jon Hallcbd1b392017-01-18 20:15:44 -080096 if (components.isEmpty()) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080097 return Optional.empty();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080098 }
99 if (components.size() == 1) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -0800100 return Optional.of(ROOT);
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800101 } else {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -0800102 return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800103 }
104 }
105
Sho SHIMIZUcaa2f7b2016-02-12 17:38:49 -0800106 private Object lastComponent() {
107 return components.get(components.size() - 1);
108 }
109
Sho SHIMIZU2a704512016-01-26 14:41:34 -0800110 @Override
111 public int hashCode() {
112 return components.hashCode();
113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (obj == null || getClass() != obj.getClass()) {
121 return false;
122 }
123 final ContinuousResourceId other = (ContinuousResourceId) obj;
124 return Objects.equals(this.components, other.components);
125 }
126
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800127 @Override
128 public String toString() {
129 // due to performance consideration, the value might need to be stored in a field
130 return ImmutableList.builder()
131 .addAll(components.subList(0, components.size() - 1))
132 .add(name)
133 .build().toString();
134 }
135}