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; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 26 | * Represents allocation of resource which is identified by the specifier. |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 27 | */ |
| 28 | @Beta |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 29 | public class ResourceAllocation { |
| 30 | |
| 31 | private final ResourcePath resource; |
| 32 | private final ResourceConsumer consumer; |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 35 | * Creates an instance with the specified subject, resource and consumer. |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 36 | * |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 37 | * @param resource resource of the subject |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 38 | * @param consumer consumer of this resource |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 39 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 40 | public ResourceAllocation(ResourcePath resource, ResourceConsumer consumer) { |
| 41 | this.resource = checkNotNull(resource); |
| 42 | this.consumer = consumer; |
| 43 | } |
| 44 | |
| 45 | // for serialization |
| 46 | private ResourceAllocation() { |
| 47 | this.resource = null; |
| 48 | this.consumer = null; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the specifier of the resource this allocation uses. |
| 53 | * |
| 54 | * @return the specifier of the resource this allocation uses |
| 55 | */ |
| 56 | public ResourcePath resource() { |
| 57 | return resource; |
| 58 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Returns the consumer of this resource. |
| 62 | * |
| 63 | * @return the consumer of this resource |
| 64 | */ |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 65 | public ResourceConsumer consumer() { |
| 66 | return consumer; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public int hashCode() { |
| 71 | return Objects.hash(resource, consumer); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean equals(Object obj) { |
| 76 | if (this == obj) { |
| 77 | return true; |
| 78 | } |
| 79 | if (!(obj instanceof ResourceAllocation)) { |
| 80 | return false; |
| 81 | } |
| 82 | final ResourceAllocation that = (ResourceAllocation) obj; |
| 83 | return Objects.equals(this.resource, that.resource) |
| 84 | && Objects.equals(this.consumer, that.consumer); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public String toString() { |
| 89 | return MoreObjects.toStringHelper(this) |
| 90 | .add("resource", resource) |
| 91 | .add("consumer", consumer) |
| 92 | .toString(); |
| 93 | } |
Sho SHIMIZU | abd849c | 2015-07-14 09:14:12 -0700 | [diff] [blame] | 94 | } |