Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.newresource; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | /** |
Sho SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 25 | * Default implementation of a class representing resource which belongs to a particular subject. |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 26 | * |
| 27 | * @param <S> type of the subject |
| 28 | * @param <T> type of the resource |
| 29 | */ |
| 30 | @Beta |
Sho SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 31 | public class DefaultResource<S, T> implements Resource<S, T> { |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 32 | |
| 33 | private final S subject; |
| 34 | private final T resource; |
| 35 | |
| 36 | /** |
Sho SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 37 | * Creates a resource with the specified subject and resource. |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 38 | * |
| 39 | * @param subject identifier which this resource belongs to |
| 40 | * @param resource resource of the subject |
| 41 | */ |
Sho SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 42 | public DefaultResource(S subject, T resource) { |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 43 | this.subject = checkNotNull(subject); |
| 44 | this.resource = checkNotNull(resource); |
| 45 | } |
| 46 | |
Sho SHIMIZU | 7727175 | 2015-07-22 15:48:03 -0700 | [diff] [blame] | 47 | // for serialization |
| 48 | private DefaultResource() { |
| 49 | this.subject = null; |
| 50 | this.resource = null; |
| 51 | } |
| 52 | |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 53 | @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 SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 73 | if (!(obj instanceof DefaultResource)) { |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
Sho SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 76 | final DefaultResource that = (DefaultResource) obj; |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 77 | return Objects.equals(this.subject, that.subject) |
| 78 | && Objects.equals(this.resource, that.resource); |
| 79 | } |
| 80 | } |