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