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 allocation of 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 DefaultResourceAllocation<S, T> implements ResourceAllocation<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 | private final ResourceConsumer consumer; |
| 36 | |
| 37 | /** |
Sho SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 38 | * Creates an instance with the specified subject, resource and consumer. |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 39 | * |
| 40 | * @param subject identifier which this resource belongs to |
| 41 | * @param resource resource of the subject |
| 42 | * @param consumer consumer ot this resource |
| 43 | */ |
Sho SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 44 | public DefaultResourceAllocation(S subject, T resource, ResourceConsumer consumer) { |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 45 | this.subject = checkNotNull(subject); |
| 46 | this.resource = checkNotNull(resource); |
| 47 | this.consumer = consumer; |
| 48 | } |
| 49 | |
Sho SHIMIZU | 7727175 | 2015-07-22 15:48:03 -0700 | [diff] [blame] | 50 | // for serialization |
| 51 | private DefaultResourceAllocation() { |
| 52 | this.subject = null; |
| 53 | this.resource = null; |
| 54 | this.consumer = null; |
| 55 | } |
| 56 | |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 57 | @Override |
| 58 | public S subject() { |
| 59 | return subject; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public T resource() { |
| 64 | return resource; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public ResourceConsumer consumer() { |
| 69 | return consumer; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public int hashCode() { |
| 74 | return Objects.hash(subject, resource, consumer); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean equals(Object obj) { |
| 79 | if (this == obj) { |
| 80 | return true; |
| 81 | } |
Sho SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 82 | if (!(obj instanceof DefaultResourceAllocation)) { |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
Sho SHIMIZU | 4ed2559 | 2015-07-20 09:59:49 -0700 | [diff] [blame] | 85 | final DefaultResourceAllocation that = (DefaultResourceAllocation) obj; |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 86 | return Objects.equals(this.subject, that.subject) |
| 87 | && Objects.equals(this.resource, that.resource) |
| 88 | && Objects.equals(this.consumer, that.consumer); |
| 89 | } |
| 90 | } |