Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.newresource; |
| 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 | /** |
| 25 | * ResourceId for {@link ContinuousResource} |
| 26 | * |
| 27 | * Note: This class is exposed to the public, but intended to be used in the resource API |
| 28 | * implementation only. It is not for resource API user. |
| 29 | */ |
| 30 | @Beta |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 31 | public final class ContinuousResourceId extends ResourceId { |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 32 | private final ImmutableList<Object> components; |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 33 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 34 | // for printing purpose only (used in toString() implementation) |
| 35 | private final String name; |
| 36 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 37 | ContinuousResourceId(ImmutableList.Builder<Object> parentComponents, Class<?> last) { |
| 38 | this.components = parentComponents.add(last.getCanonicalName()).build(); |
| 39 | this.name = last.getSimpleName(); |
| 40 | } |
| 41 | |
Sho SHIMIZU | 7f8a7cb | 2016-01-27 15:09:13 -0800 | [diff] [blame] | 42 | // for serializer |
| 43 | ContinuousResourceId() { |
| 44 | this.components = ImmutableList.of(); |
| 45 | this.name = ""; |
| 46 | } |
| 47 | |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 48 | ImmutableList<Object> components() { |
| 49 | return components; |
| 50 | } |
| 51 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 52 | /** |
| 53 | * {@inheritDoc} |
| 54 | * |
| 55 | * A child of a continuous-type resource is prohibited. |
| 56 | * {@link UnsupportedOperationException} is always thrown. |
| 57 | */ |
| 58 | @Override |
| 59 | public DiscreteResourceId child(Object child) { |
| 60 | throw new UnsupportedOperationException(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritDoc} |
| 65 | * |
| 66 | * A child of a continuous-type resource is prohibited. |
| 67 | * {@link UnsupportedOperationException} is always thrown. |
| 68 | */ |
| 69 | @Override |
| 70 | public ContinuousResourceId child(Class<?> child) { |
| 71 | throw new UnsupportedOperationException(); |
| 72 | } |
| 73 | |
| 74 | @Override |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 75 | public Optional<DiscreteResourceId> parent() { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 76 | if (components.size() == 0) { |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 77 | return Optional.empty(); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 78 | } |
| 79 | if (components.size() == 1) { |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 80 | return Optional.of(ROOT); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 81 | } else { |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 82 | return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1))); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int hashCode() { |
| 88 | return components.hashCode(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public boolean equals(Object obj) { |
| 93 | if (this == obj) { |
| 94 | return true; |
| 95 | } |
| 96 | if (obj == null || getClass() != obj.getClass()) { |
| 97 | return false; |
| 98 | } |
| 99 | final ContinuousResourceId other = (ContinuousResourceId) obj; |
| 100 | return Objects.equals(this.components, other.components); |
| 101 | } |
| 102 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 103 | @Override |
| 104 | public String toString() { |
| 105 | // due to performance consideration, the value might need to be stored in a field |
| 106 | return ImmutableList.builder() |
| 107 | .addAll(components.subList(0, components.size() - 1)) |
| 108 | .add(name) |
| 109 | .build().toString(); |
| 110 | } |
| 111 | } |