Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 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 | */ |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 21 | import java.util.Objects; |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 22 | import java.util.Optional; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 23 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 24 | /** |
Sho SHIMIZU | 37a038b | 2016-02-12 17:45:59 -0800 | [diff] [blame] | 25 | * ResourceId for {@link ContinuousResource}. |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 26 | */ |
| 27 | @Beta |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 28 | public final class ContinuousResourceId extends ResourceId { |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 29 | private final ImmutableList<Object> components; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 30 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 31 | // for printing purpose only (used in toString() implementation) |
| 32 | private final String name; |
| 33 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 34 | ContinuousResourceId(ImmutableList.Builder<Object> parentComponents, Class<?> last) { |
| 35 | this.components = parentComponents.add(last.getCanonicalName()).build(); |
| 36 | this.name = last.getSimpleName(); |
| 37 | } |
| 38 | |
Sho SHIMIZU | 7f8a7cb | 2016-01-27 15:09:13 -0800 | [diff] [blame] | 39 | // for serializer |
| 40 | ContinuousResourceId() { |
| 41 | this.components = ImmutableList.of(); |
| 42 | this.name = ""; |
| 43 | } |
| 44 | |
Sho SHIMIZU | 2486ddd | 2016-01-29 11:51:12 -0800 | [diff] [blame] | 45 | @Override |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 46 | ImmutableList<Object> components() { |
| 47 | return components; |
| 48 | } |
| 49 | |
Sho SHIMIZU | 74ac901 | 2016-02-12 10:03:21 -0800 | [diff] [blame] | 50 | @Override |
| 51 | String simpleTypeName() { |
| 52 | return name; |
| 53 | } |
| 54 | |
Sho SHIMIZU | caa2f7b | 2016-02-12 17:38:49 -0800 | [diff] [blame] | 55 | @Override |
| 56 | boolean isTypeOf(Class<?> type) { |
| 57 | String typeName = (String) lastComponent(); |
| 58 | return typeName.equals(type.getCanonicalName()); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | boolean isSubTypeOf(Class<?> ancestor) { |
| 63 | String typeName = (String) lastComponent(); |
| 64 | boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName()); |
| 65 | boolean foundInAncestor = components.subList(0, components.size()).stream() |
| 66 | .filter(x -> ancestor.isAssignableFrom(x.getClass())) |
| 67 | .findAny() |
| 68 | .isPresent(); |
| 69 | return foundInAncestor || foundInLeaf; |
| 70 | } |
| 71 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 72 | /** |
| 73 | * {@inheritDoc} |
| 74 | * |
| 75 | * A child of a continuous-type resource is prohibited. |
| 76 | * {@link UnsupportedOperationException} is always thrown. |
| 77 | */ |
| 78 | @Override |
| 79 | public DiscreteResourceId child(Object child) { |
| 80 | throw new UnsupportedOperationException(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * {@inheritDoc} |
| 85 | * |
| 86 | * A child of a continuous-type resource is prohibited. |
| 87 | * {@link UnsupportedOperationException} is always thrown. |
| 88 | */ |
| 89 | @Override |
| 90 | public ContinuousResourceId child(Class<?> child) { |
| 91 | throw new UnsupportedOperationException(); |
| 92 | } |
| 93 | |
| 94 | @Override |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 95 | public Optional<DiscreteResourceId> parent() { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 96 | if (components.size() == 0) { |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 97 | return Optional.empty(); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 98 | } |
| 99 | if (components.size() == 1) { |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 100 | return Optional.of(ROOT); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 101 | } else { |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 102 | return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1))); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Sho SHIMIZU | caa2f7b | 2016-02-12 17:38:49 -0800 | [diff] [blame] | 106 | private Object lastComponent() { |
| 107 | return components.get(components.size() - 1); |
| 108 | } |
| 109 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 110 | @Override |
| 111 | public int hashCode() { |
| 112 | return components.hashCode(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean equals(Object obj) { |
| 117 | if (this == obj) { |
| 118 | return true; |
| 119 | } |
| 120 | if (obj == null || getClass() != obj.getClass()) { |
| 121 | return false; |
| 122 | } |
| 123 | final ContinuousResourceId other = (ContinuousResourceId) obj; |
| 124 | return Objects.equals(this.components, other.components); |
| 125 | } |
| 126 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 127 | @Override |
| 128 | public String toString() { |
| 129 | // due to performance consideration, the value might need to be stored in a field |
| 130 | return ImmutableList.builder() |
| 131 | .addAll(components.subList(0, components.size() - 1)) |
| 132 | .add(name) |
| 133 | .build().toString(); |
| 134 | } |
| 135 | } |