blob: 587010675e770669ed0579935cc34988e4bdff97 [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 -080024import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * ResourceId for {@link ContinuousResource}
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 ContinuousResourceId 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 // for printing purpose only (used in toString() implementation)
37 private final String name;
38
39 ContinuousResourceId(ImmutableList<Object> components, String name) {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080040 this.components = components;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080041 this.name = checkNotNull(name);
42 }
43
Sho SHIMIZU2a704512016-01-26 14:41:34 -080044 ContinuousResourceId(ImmutableList.Builder<Object> parentComponents, Class<?> last) {
45 this.components = parentComponents.add(last.getCanonicalName()).build();
46 this.name = last.getSimpleName();
47 }
48
Sho SHIMIZU7f8a7cb2016-01-27 15:09:13 -080049 // for serializer
50 ContinuousResourceId() {
51 this.components = ImmutableList.of();
52 this.name = "";
53 }
54
Sho SHIMIZU42ac51f2016-01-27 12:59:31 -080055 ImmutableList<Object> components() {
56 return components;
57 }
58
Sho SHIMIZU2a704512016-01-26 14:41:34 -080059 /**
60 * {@inheritDoc}
61 *
62 * A child of a continuous-type resource is prohibited.
63 * {@link UnsupportedOperationException} is always thrown.
64 */
65 @Override
66 public DiscreteResourceId child(Object child) {
67 throw new UnsupportedOperationException();
68 }
69
70 /**
71 * {@inheritDoc}
72 *
73 * A child of a continuous-type resource is prohibited.
74 * {@link UnsupportedOperationException} is always thrown.
75 */
76 @Override
77 public ContinuousResourceId child(Class<?> child) {
78 throw new UnsupportedOperationException();
79 }
80
81 @Override
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080082 public Optional<DiscreteResourceId> parent() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080083 if (components.size() == 0) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080084 return Optional.empty();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080085 }
86 if (components.size() == 1) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080087 return Optional.of(ROOT);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080088 } else {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080089 return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
Sho SHIMIZU2a704512016-01-26 14:41:34 -080090 }
91 }
92
93 @Override
94 public int hashCode() {
95 return components.hashCode();
96 }
97
98 @Override
99 public boolean equals(Object obj) {
100 if (this == obj) {
101 return true;
102 }
103 if (obj == null || getClass() != obj.getClass()) {
104 return false;
105 }
106 final ContinuousResourceId other = (ContinuousResourceId) obj;
107 return Objects.equals(this.components, other.components);
108 }
109
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800110 @Override
111 public String toString() {
112 // due to performance consideration, the value might need to be stored in a field
113 return ImmutableList.builder()
114 .addAll(components.subList(0, components.size() - 1))
115 .add(name)
116 .build().toString();
117 }
118}