blob: 5d0efe40476aedc2064ad80e5b8cf84ef64a425f [file] [log] [blame]
Sho SHIMIZUabd849c2015-07-14 09:14:12 -07001/*
2 * Copyright 2015 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;
19
20import java.util.Objects;
21
22import static com.google.common.base.Preconditions.checkNotNull;
23
24/**
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070025 * Default implementation of a class representing resource which belongs to a particular subject.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070026 *
27 * @param <S> type of the subject
28 * @param <T> type of the resource
29 */
30@Beta
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070031public class DefaultResource<S, T> implements Resource<S, T> {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070032
33 private final S subject;
34 private final T resource;
35
36 /**
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070037 * Creates a resource with the specified subject and resource.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070038 *
39 * @param subject identifier which this resource belongs to
40 * @param resource resource of the subject
41 */
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070042 public DefaultResource(S subject, T resource) {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070043 this.subject = checkNotNull(subject);
44 this.resource = checkNotNull(resource);
45 }
46
47 @Override
48 public S subject() {
49 return subject;
50 }
51
52 @Override
53 public T resource() {
54 return resource;
55 }
56
57 @Override
58 public int hashCode() {
59 return Objects.hash(subject, resource);
60 }
61
62 @Override
63 public boolean equals(Object obj) {
64 if (this == obj) {
65 return true;
66 }
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070067 if (!(obj instanceof DefaultResource)) {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070068 return false;
69 }
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070070 final DefaultResource that = (DefaultResource) obj;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070071 return Objects.equals(this.subject, that.subject)
72 && Objects.equals(this.resource, that.resource);
73 }
74}