blob: 630c845f15f2515e81ccec699d8b220913116ae9 [file] [log] [blame]
Naoki Shiotabd1974c2016-04-29 18:44:17 -07001/*
2 * Copyright 2016 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
17package org.onosproject.net.resource;
18
19import com.google.common.base.Objects;
20import org.onlab.util.Identifier;
21
22import static com.google.common.base.Preconditions.checkNotNull;
23
24/**
25 * Representation of global unique ID for ResourceConsumer object.
26 */
27public class ResourceConsumerId {
28 private final String className;
29 private final long value;
30
31 // Constructor for serializer.
32 protected ResourceConsumerId() {
33 this.className = null;
34 this.value = 0L;
35 }
36
37 /**
38 * Constructor with specifying every fields.
39 *
40 * @param value ID value unique within the given class
41 * @param cls class of ResourceConsumer implementation
42 */
43 ResourceConsumerId(long value, Class<?> cls) {
44 this.className = checkNotNull(cls.getName());
45 this.value = value;
46 }
47
48 /**
49 * Checks if the consumer is an instance of given class.
50 *
51 * @param cls class object
52 * @return result of check
53 */
54 public boolean isClassOf(Class<?> cls) {
55 return checkNotNull(cls).getName().equals(className);
56 }
57
58 /**
59 * Returns class name of the consumer.
60 *
61 * @return class name of the consumer in String
62 */
63 public String consumerClass() {
64 return className;
65 }
66
67 /**
68 * Returns ID value.
69 *
70 * @return ID value
71 */
72 public long value() {
73 return value;
74 }
75
76 @Override
77 public boolean equals(Object o) {
78 if (this == o) {
79 return true;
80 }
81 if (o == null || getClass() != o.getClass()) {
82 return false;
83 }
84 ResourceConsumerId that = (ResourceConsumerId) o;
85 return Objects.equal(className, that.className) &&
86 Objects.equal(value, that.value);
87 }
88
89 @Override
90 public int hashCode() {
91 return Objects.hashCode(className, value);
92 }
93
94 /**
95 * Creates ResourceConsumerId from given value and class.
96 *
97 * @param value ID value unique within the given class
98 * @param cls class of ResourceConsumer implementation
99 * @return created ResourceConsumerId object
100 */
101 public static <T extends ResourceConsumer> ResourceConsumerId of(long value, Class<T> cls) {
102 return new ResourceConsumerId(value, cls);
103 }
104
105 /**
106 * Creates ResourceConsumerId instance from Identifier object.
107 *
108 * @param id identifier object backed by Long value
109 * @return created ResourceConsumerId object
110 */
111 public static <T extends Identifier<Long> & ResourceConsumer> ResourceConsumerId of(T id) {
112 return new ResourceConsumerId(id.id(), id.getClass());
113 }
114}