blob: b0937bc1a299f57428da3928a732776b2ad0e7ac [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;
22
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080023import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * ResourceId for {@link ContinuousResource}
27 *
28 * Note: This class is exposed to the public, but intended to be used in the resource API
29 * implementation only. It is not for resource API user.
30 */
31@Beta
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080032public final class ContinuousResourceId extends ResourceId {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080033 final ImmutableList<Object> components;
34
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080035 // for printing purpose only (used in toString() implementation)
36 private final String name;
37
38 ContinuousResourceId(ImmutableList<Object> components, String name) {
Sho SHIMIZU2a704512016-01-26 14:41:34 -080039 this.components = components;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080040 this.name = checkNotNull(name);
41 }
42
Sho SHIMIZU2a704512016-01-26 14:41:34 -080043 ContinuousResourceId(ImmutableList.Builder<Object> parentComponents, Class<?> last) {
44 this.components = parentComponents.add(last.getCanonicalName()).build();
45 this.name = last.getSimpleName();
46 }
47
48 /**
49 * {@inheritDoc}
50 *
51 * A child of a continuous-type resource is prohibited.
52 * {@link UnsupportedOperationException} is always thrown.
53 */
54 @Override
55 public DiscreteResourceId child(Object child) {
56 throw new UnsupportedOperationException();
57 }
58
59 /**
60 * {@inheritDoc}
61 *
62 * A child of a continuous-type resource is prohibited.
63 * {@link UnsupportedOperationException} is always thrown.
64 */
65 @Override
66 public ContinuousResourceId child(Class<?> child) {
67 throw new UnsupportedOperationException();
68 }
69
70 @Override
71 DiscreteResourceId parent() {
72 if (components.size() == 0) {
73 return null;
74 }
75 if (components.size() == 1) {
76 return ROOT;
77 } else {
78 return new DiscreteResourceId(components.subList(0, components.size() - 1));
79 }
80 }
81
82 @Override
83 public int hashCode() {
84 return components.hashCode();
85 }
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this == obj) {
90 return true;
91 }
92 if (obj == null || getClass() != obj.getClass()) {
93 return false;
94 }
95 final ContinuousResourceId other = (ContinuousResourceId) obj;
96 return Objects.equals(this.components, other.components);
97 }
98
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080099 @Override
100 public String toString() {
101 // due to performance consideration, the value might need to be stored in a field
102 return ImmutableList.builder()
103 .addAll(components.subList(0, components.size() - 1))
104 .add(name)
105 .build().toString();
106 }
107}