blob: 4dadacf869dd5dc1e7c96d5f7f35edd622c769e5 [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
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080024/**
25 * ResourceId for {@link ContinuousResource}
26 *
27 * Note: This class is exposed to the public, but intended to be used in the resource API
28 * implementation only. It is not for resource API user.
29 */
30@Beta
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080031public final class ContinuousResourceId 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 // for printing purpose only (used in toString() implementation)
35 private final String name;
36
Sho SHIMIZU2a704512016-01-26 14:41:34 -080037 ContinuousResourceId(ImmutableList.Builder<Object> parentComponents, Class<?> last) {
38 this.components = parentComponents.add(last.getCanonicalName()).build();
39 this.name = last.getSimpleName();
40 }
41
Sho SHIMIZU7f8a7cb2016-01-27 15:09:13 -080042 // for serializer
43 ContinuousResourceId() {
44 this.components = ImmutableList.of();
45 this.name = "";
46 }
47
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080048 ImmutableList<Object> components() {
49 return components;
50 }
51
Sho SHIMIZU2a704512016-01-26 14:41:34 -080052 /**
53 * {@inheritDoc}
54 *
55 * A child of a continuous-type resource is prohibited.
56 * {@link UnsupportedOperationException} is always thrown.
57 */
58 @Override
59 public DiscreteResourceId child(Object child) {
60 throw new UnsupportedOperationException();
61 }
62
63 /**
64 * {@inheritDoc}
65 *
66 * A child of a continuous-type resource is prohibited.
67 * {@link UnsupportedOperationException} is always thrown.
68 */
69 @Override
70 public ContinuousResourceId child(Class<?> child) {
71 throw new UnsupportedOperationException();
72 }
73
74 @Override
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080075 public Optional<DiscreteResourceId> parent() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080076 if (components.size() == 0) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080077 return Optional.empty();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080078 }
79 if (components.size() == 1) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080080 return Optional.of(ROOT);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080081 } else {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080082 return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
Sho SHIMIZU2a704512016-01-26 14:41:34 -080083 }
84 }
85
86 @Override
87 public int hashCode() {
88 return components.hashCode();
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (this == obj) {
94 return true;
95 }
96 if (obj == null || getClass() != obj.getClass()) {
97 return false;
98 }
99 final ContinuousResourceId other = (ContinuousResourceId) obj;
100 return Objects.equals(this.components, other.components);
101 }
102
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800103 @Override
104 public String toString() {
105 // due to performance consideration, the value might need to be stored in a field
106 return ImmutableList.builder()
107 .addAll(components.subList(0, components.size() - 1))
108 .add(name)
109 .build().toString();
110 }
111}