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