blob: 0b496a82db58de8701675c204238e6e09679525d [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
50 @Override
51 public S subject() {
52 return subject;
53 }
54
55 @Override
56 public T resource() {
57 return resource;
58 }
59
60 @Override
61 public ResourceConsumer consumer() {
62 return consumer;
63 }
64
65 @Override
66 public int hashCode() {
67 return Objects.hash(subject, resource, consumer);
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 if (this == obj) {
73 return true;
74 }
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070075 if (!(obj instanceof DefaultResourceAllocation)) {
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070076 return false;
77 }
Sho SHIMIZU4ed25592015-07-20 09:59:49 -070078 final DefaultResourceAllocation that = (DefaultResourceAllocation) obj;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070079 return Objects.equals(this.subject, that.subject)
80 && Objects.equals(this.resource, that.resource)
81 && Objects.equals(this.consumer, that.consumer);
82 }
83}