Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [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 | package org.onosproject.net; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.hash.HashFunction; |
| 20 | import com.google.common.hash.Hashing; |
| 21 | import org.onlab.util.Identifier; |
| 22 | import org.onosproject.net.resource.ResourceConsumer; |
| 23 | import org.onosproject.net.resource.ResourceConsumerId; |
| 24 | |
| 25 | import java.nio.charset.StandardCharsets; |
| 26 | |
| 27 | /** |
| 28 | * Intent identifier suitable as an external key. |
| 29 | * <p>This class is immutable.</p> |
| 30 | */ |
| 31 | @Beta |
| 32 | public final class ResourceGroup extends Identifier<Long> implements ResourceConsumer { |
| 33 | |
| 34 | private static final String HEX_PREFIX = "0x"; |
| 35 | private static final HashFunction HASH_FN = Hashing.md5(); |
| 36 | |
| 37 | /** |
| 38 | * Creates a resource group identifier from the specified long |
| 39 | * representation. |
| 40 | * |
| 41 | * @param value long value |
| 42 | * @return resource group identifier |
| 43 | */ |
| 44 | public static ResourceGroup of(long value) { |
| 45 | return new ResourceGroup(value); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Creates a resource group identifier from the specified string |
| 50 | * representation. |
| 51 | * Warning: it is caller responsibility to make sure the hashed value of |
| 52 | * {@code value} is unique. |
| 53 | * |
| 54 | * @param value string value |
| 55 | * @return resource group identifier |
| 56 | */ |
| 57 | public static ResourceGroup of(String value) { |
| 58 | return new ResourceGroup(HASH_FN.newHasher() |
| 59 | .putString(value, StandardCharsets.UTF_8) |
| 60 | .hash() |
| 61 | .asLong()); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Constructor for serializer. |
| 66 | */ |
| 67 | protected ResourceGroup() { |
| 68 | super(0L); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Constructs the ID corresponding to a given long value. |
| 73 | * |
| 74 | * @param value the underlying value of this ID |
| 75 | */ |
| 76 | protected ResourceGroup(long value) { |
| 77 | super(value); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the backing value. |
| 82 | * |
| 83 | * @return the value |
| 84 | */ |
| 85 | public long fingerprint() { |
| 86 | return identifier; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public String toString() { |
| 91 | return HEX_PREFIX + Long.toHexString(identifier); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public ResourceConsumerId consumerId() { |
| 96 | return ResourceConsumerId.of(this); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public int hashCode() { |
| 101 | return super.hashCode(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean equals(Object obj) { |
| 106 | if (this == obj) { |
| 107 | return true; |
| 108 | } |
| 109 | if (obj == null || getClass() != obj.getClass()) { |
| 110 | return false; |
| 111 | } |
| 112 | final ResourceGroup other = (ResourceGroup) obj; |
| 113 | if (this.fingerprint() != other.fingerprint()) { |
| 114 | return false; |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | } |