blob: 07976a93177ae13b0b4f58359c5254902713e5cf [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;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070019import com.google.common.base.MoreObjects;
20
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070024
25/**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070026 * Represents allocation of resource which is identified by the specifier.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070027 */
28@Beta
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070029public class ResourceAllocation {
30
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080031 private final Resource resource;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070032 private final ResourceConsumer consumer;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070033
34 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070035 * Creates an instance with the specified subject, resource and consumer.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070036 *
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070037 * @param resource resource of the subject
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080038 * @param consumer consumer of this resource
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070039 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080040 public ResourceAllocation(Resource resource, ResourceConsumer consumer) {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070041 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 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080056 public Resource resource() {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070057 return resource;
58 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070059
60 /**
61 * Returns the consumer of this resource.
62 *
63 * @return the consumer of this resource
64 */
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070065 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 SHIMIZUabd849c2015-07-14 09:14:12 -070094}