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 | |
| 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}. |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 29 | */ |
| 30 | @Beta |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 31 | public final class DiscreteResourceId 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 | DiscreteResourceId(ImmutableList<Object> components) { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 35 | this.components = components; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | DiscreteResourceId() { |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 39 | this.components = ImmutableList.of(); |
| 40 | } |
| 41 | |
Sho SHIMIZU | 2486ddd | 2016-01-29 11:51:12 -0800 | [diff] [blame] | 42 | @Override |
Sho SHIMIZU | 42ac51f | 2016-01-27 12:59:31 -0800 | [diff] [blame] | 43 | ImmutableList<Object> components() { |
| 44 | return components; |
| 45 | } |
| 46 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 47 | @Override |
Sho SHIMIZU | 74ac901 | 2016-02-12 10:03:21 -0800 | [diff] [blame] | 48 | String simpleTypeName() { |
Sho SHIMIZU | caa2f7b | 2016-02-12 17:38:49 -0800 | [diff] [blame] | 49 | if (isRoot()) { |
Sho SHIMIZU | 74ac901 | 2016-02-12 10:03:21 -0800 | [diff] [blame] | 50 | return "Root"; |
| 51 | } |
| 52 | |
Sho SHIMIZU | caa2f7b | 2016-02-12 17:38:49 -0800 | [diff] [blame] | 53 | return lastComponent().getClass().getSimpleName(); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | boolean isTypeOf(Class<?> type) { |
| 58 | if (isRoot()) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return type.isAssignableFrom(lastComponent().getClass()); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | boolean isSubTypeOf(Class<?> ancestor) { |
| 67 | return components.stream() |
| 68 | .filter(x -> ancestor.isAssignableFrom(x.getClass())) |
| 69 | .findAny() |
| 70 | .isPresent(); |
Sho SHIMIZU | 74ac901 | 2016-02-12 10:03:21 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 74 | public DiscreteResourceId child(Object child) { |
| 75 | checkArgument(!(child instanceof Class<?>)); |
| 76 | |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 77 | return Resources.discrete(this, child).id(); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public ContinuousResourceId child(Class<?> child) { |
| 82 | checkNotNull(child); |
| 83 | |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 84 | return Resources.continuous(this, child).id(); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 88 | public Optional<DiscreteResourceId> parent() { |
Sho SHIMIZU | caa2f7b | 2016-02-12 17:38:49 -0800 | [diff] [blame] | 89 | if (isRoot()) { |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 90 | return Optional.empty(); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 91 | } |
| 92 | if (components.size() == 1) { |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 93 | return Optional.of(ROOT); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 94 | } else { |
Sho SHIMIZU | 7dac8a4 | 2016-01-27 12:04:39 -0800 | [diff] [blame] | 95 | return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1))); |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Sho SHIMIZU | caa2f7b | 2016-02-12 17:38:49 -0800 | [diff] [blame] | 99 | <T> Optional<T> lastComponentAs(Class<T> type) { |
| 100 | if (!isTypeOf(type)) { |
| 101 | return Optional.empty(); |
| 102 | } |
| 103 | |
| 104 | @SuppressWarnings("unchecked") |
| 105 | T value = (T) lastComponent(); |
| 106 | return Optional.of(value); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | private boolean isRoot() { |
| 111 | return components.isEmpty(); |
| 112 | } |
| 113 | |
| 114 | private Object lastComponent() { |
| 115 | return components.get(components.size() - 1); |
| 116 | } |
| 117 | |
Sho SHIMIZU | 2a70451 | 2016-01-26 14:41:34 -0800 | [diff] [blame] | 118 | @Override |
| 119 | public int hashCode() { |
| 120 | return components.hashCode(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean equals(Object obj) { |
| 125 | if (this == obj) { |
| 126 | return true; |
| 127 | } |
| 128 | if (obj == null || getClass() != obj.getClass()) { |
| 129 | return false; |
| 130 | } |
| 131 | final DiscreteResourceId other = (DiscreteResourceId) obj; |
| 132 | return Objects.equals(this.components, other.components); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public String toString() { |
| 137 | return components.toString(); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 138 | } |
| 139 | } |