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