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; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkArgument; |
| 24 | import static com.google.common.base.Preconditions.checkNotNull; |
| 25 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 26 | /** |
| 27 | * ResourceId for {@link DiscreteResource}. |
| 28 | * |
| 29 | * Note: This class is exposed to the public, but intended to be used in the resource API |
| 30 | * implementation only. It is not for resource API user. |
| 31 | */ |
| 32 | @Beta |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 33 | public final class DiscreteResourceId extends ResourceId { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame^] | 34 | final ImmutableList<Object> components; |
| 35 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 36 | DiscreteResourceId(ImmutableList<Object> components) { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame^] | 37 | this.components = components; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | DiscreteResourceId() { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame^] | 41 | this.components = ImmutableList.of(); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public DiscreteResourceId child(Object child) { |
| 46 | checkArgument(!(child instanceof Class<?>)); |
| 47 | |
| 48 | return new DiscreteResourceId(ImmutableList.builder() |
| 49 | .addAll(components) |
| 50 | .add(child) |
| 51 | .build()); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public ContinuousResourceId child(Class<?> child) { |
| 56 | checkNotNull(child); |
| 57 | |
| 58 | return new ContinuousResourceId(ImmutableList.builder().addAll(components), child); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | DiscreteResourceId parent() { |
| 63 | if (components.size() == 0) { |
| 64 | return null; |
| 65 | } |
| 66 | if (components.size() == 1) { |
| 67 | return ROOT; |
| 68 | } else { |
| 69 | return new DiscreteResourceId(components.subList(0, components.size() - 1)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int hashCode() { |
| 75 | return components.hashCode(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean equals(Object obj) { |
| 80 | if (this == obj) { |
| 81 | return true; |
| 82 | } |
| 83 | if (obj == null || getClass() != obj.getClass()) { |
| 84 | return false; |
| 85 | } |
| 86 | final DiscreteResourceId other = (DiscreteResourceId) obj; |
| 87 | return Objects.equals(this.components, other.components); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public String toString() { |
| 92 | return components.toString(); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 93 | } |
| 94 | } |