blob: ab0b7fee7aff1dfc079e60d1242a516c94bb2b94 [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/**
25 * Base implementation of a class representing allocation of resource which belongs to a particular subject.
26 *
27 * @param <S> type of the subject
28 * @param <T> type of the resource
29 */
30@Beta
31public abstract class AbstractResourceAllocation<S, T> implements ResourceAllocation<S, T> {
32
33 private final S subject;
34 private final T resource;
35 private final ResourceConsumer consumer;
36
37 /**
38 * Constructor expected to be called by constructors of the sub-classes.
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 */
44 protected AbstractResourceAllocation(S subject, T resource, ResourceConsumer consumer) {
45 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 }
75 if (!(obj instanceof AbstractResourceAllocation)) {
76 return false;
77 }
78 final AbstractResourceAllocation that = (AbstractResourceAllocation) obj;
79 return Objects.equals(this.subject, that.subject)
80 && Objects.equals(this.resource, that.resource)
81 && Objects.equals(this.consumer, that.consumer);
82 }
83}