Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.net.resource; |
| 18 | |
| 19 | import com.google.common.base.Objects; |
| 20 | import org.onlab.util.Identifier; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | /** |
| 25 | * Representation of global unique ID for ResourceConsumer object. |
| 26 | */ |
| 27 | public 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 | * |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 97 | * @param <T> resource consumer class type |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 98 | * @param value ID value unique within the given class |
| 99 | * @param cls class of ResourceConsumer implementation |
| 100 | * @return created ResourceConsumerId object |
| 101 | */ |
| 102 | public static <T extends ResourceConsumer> ResourceConsumerId of(long value, Class<T> cls) { |
| 103 | return new ResourceConsumerId(value, cls); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Creates ResourceConsumerId instance from Identifier object. |
| 108 | * |
Ray Milkey | bb23e0b | 2016-08-02 17:00:21 -0700 | [diff] [blame] | 109 | * @param <T> resource consumer class type |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 110 | * @param id identifier object backed by Long value |
| 111 | * @return created ResourceConsumerId object |
| 112 | */ |
| 113 | public static <T extends Identifier<Long> & ResourceConsumer> ResourceConsumerId of(T id) { |
| 114 | return new ResourceConsumerId(id.id(), id.getClass()); |
| 115 | } |
| 116 | } |