blob: 03b634b80753ddb2c4f42bf9a837c9100d571d15 [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 SHIMIZU42ac51f2016-01-27 12:59:31 -080049 ImmutableList<Object> components() {
50 return components;
51 }
52
Sho SHIMIZU2a704512016-01-26 14:41:34 -080053 /**
54 * {@inheritDoc}
55 *
56 * A child of a continuous-type resource is prohibited.
57 * {@link UnsupportedOperationException} is always thrown.
58 */
59 @Override
60 public DiscreteResourceId child(Object child) {
61 throw new UnsupportedOperationException();
62 }
63
64 /**
65 * {@inheritDoc}
66 *
67 * A child of a continuous-type resource is prohibited.
68 * {@link UnsupportedOperationException} is always thrown.
69 */
70 @Override
71 public ContinuousResourceId child(Class<?> child) {
72 throw new UnsupportedOperationException();
73 }
74
75 @Override
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080076 public Optional<DiscreteResourceId> parent() {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080077 if (components.size() == 0) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080078 return Optional.empty();
Sho SHIMIZU2a704512016-01-26 14:41:34 -080079 }
80 if (components.size() == 1) {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080081 return Optional.of(ROOT);
Sho SHIMIZU2a704512016-01-26 14:41:34 -080082 } else {
Sho SHIMIZU7dac8a42016-01-27 12:04:39 -080083 return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
Sho SHIMIZU2a704512016-01-26 14:41:34 -080084 }
85 }
86
87 @Override
88 public int hashCode() {
89 return components.hashCode();
90 }
91
92 @Override
93 public boolean equals(Object obj) {
94 if (this == obj) {
95 return true;
96 }
97 if (obj == null || getClass() != obj.getClass()) {
98 return false;
99 }
100 final ContinuousResourceId other = (ContinuousResourceId) obj;
101 return Objects.equals(this.components, other.components);
102 }
103
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800104 @Override
105 public String toString() {
106 // due to performance consideration, the value might need to be stored in a field
107 return ImmutableList.builder()
108 .addAll(components.subList(0, components.size() - 1))
109 .add(name)
110 .build().toString();
111 }
112}