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