blob: 06b6ddaab069a2395779d0476e652b4e1653aa75 [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 allocation of 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 DefaultResourceAllocation<S, T> implements ResourceAllocation<S, T> {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070032
33 private final S subject;
34 private final T resource;
35 private final ResourceConsumer consumer;
36
37 /**
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070038 * Creates an instance with the specified subject, resource and consumer.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070039 *
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 SHIMIZU4ed25592015-07-20 09:59:49 -070044 public DefaultResourceAllocation(S subject, T resource, ResourceConsumer consumer) {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070045 this.subject = checkNotNull(subject);
46 this.resource = checkNotNull(resource);
47 this.consumer = consumer;
48 }
49
Sho SHIMIZU77271752015-07-22 15:48:03 -070050 // for serialization
51 private DefaultResourceAllocation() {
52 this.subject = null;
53 this.resource = null;
54 this.consumer = null;
55 }
56
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070057 @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 SHIMIZU4ed25592015-07-20 09:59:49 -070082 if (!(obj instanceof DefaultResourceAllocation)) {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070083 return false;
84 }
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070085 final DefaultResourceAllocation that = (DefaultResourceAllocation) obj;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070086 return Objects.equals(this.subject, that.subject)
87 && Objects.equals(this.resource, that.resource)
88 && Objects.equals(this.consumer, that.consumer);
89 }
90}