blob: 6bfc3cc6ff1dcf34851097fd0313c53c0ff1ec9c [file] [log] [blame]
Sho SHIMIZU76b30f72016-01-11 14:08:35 -08001/*
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 */
16package org.onosproject.net.newresource;
17
18import com.google.common.annotations.Beta;
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080019import com.google.common.collect.ImmutableList;
20import org.onosproject.net.DeviceId;
21import org.onosproject.net.PortNumber;
22
23import java.util.Objects;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Represents identifier of resource.
29 * This class is exposed to public, but intended to use only in ResourceStore implementations.
30 */
31@Beta
32public final class ResourceId {
33 static final ResourceId ROOT = new ResourceId();
34
35 final ImmutableList<Object> components;
36
37 static ResourceId of(DeviceId device, Object... components) {
38 return new ResourceId(ImmutableList.builder()
39 .add(device)
40 .add(components)
41 .build());
42 }
43
44 static ResourceId of(DeviceId device, PortNumber port, Object... components) {
45 return new ResourceId(ImmutableList.builder()
46 .add(device)
47 .add(port)
48 .add(components)
49 .build());
50 }
51
52 private ResourceId(ImmutableList<Object> components) {
53 this.components = checkNotNull(components);
54 }
55
56 // for serializer
57 private ResourceId() {
58 this.components = ImmutableList.of();
59 }
60
61 // IndexOutOfBoundsException is raised when the instance is equal to ROOT
62 ResourceId parent() {
63 if (components.size() == 1) {
64 return ROOT;
65 } else {
66 return new ResourceId(components.subList(0, components.size() - 1));
67 }
68 }
69
70 ResourceId child(Object child) {
71 return new ResourceId(ImmutableList.builder()
Sho SHIMIZU38f561c2016-01-14 09:07:47 -080072 .addAll(components)
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080073 .add(child)
74 .build());
75 }
76
77 @Override
78 public int hashCode() {
79 return components.hashCode();
80 }
81
82 @Override
83 public boolean equals(Object obj) {
84 if (this == obj) {
85 return true;
86 }
87 if (!(obj instanceof ResourceId)) {
88 return false;
89 }
90
91 ResourceId other = (ResourceId) obj;
92 return Objects.equals(this.components, other.components);
93 }
94
95 @Override
96 public String toString() {
HIGUCHI Yuta6f828c32016-01-20 18:11:05 -080097 return components.toString();
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080098 }
99}