blob: 1db5eb7b8d075d327ee00a89fff51426e8ce7d3d [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
Sho SHIMIZU77271752015-07-22 15:48:03 -070047 // for serialization
48 private DefaultResource() {
49 this.subject = null;
50 this.resource = null;
51 }
52
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070053 @Override
54 public S subject() {
55 return subject;
56 }
57
58 @Override
59 public T resource() {
60 return resource;
61 }
62
63 @Override
64 public int hashCode() {
65 return Objects.hash(subject, resource);
66 }
67
68 @Override
69 public boolean equals(Object obj) {
70 if (this == obj) {
71 return true;
72 }
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070073 if (!(obj instanceof DefaultResource)) {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070074 return false;
75 }
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070076 final DefaultResource that = (DefaultResource) obj;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070077 return Objects.equals(this.subject, that.subject)
78 && Objects.equals(this.resource, that.resource);
79 }
80}