Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -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 | |
| 17 | package org.onlab.util; |
| 18 | |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | /** |
| 24 | * Abstract identifier backed by another value, e.g. string, int. |
| 25 | */ |
| 26 | public class Identifier<T> { |
| 27 | |
| 28 | protected final T identifier; // backing identifier value |
| 29 | |
| 30 | /** |
| 31 | * Constructor for serialization. |
| 32 | */ |
| 33 | protected Identifier() { |
| 34 | this.identifier = null; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Constructs an identifier backed by the specified value. |
| 39 | * |
| 40 | * @param value the backing value |
| 41 | */ |
| 42 | protected Identifier(T value) { |
| 43 | this.identifier = checkNotNull(value, "Identifier cannot be null."); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns the backing identifier value. |
| 48 | * |
| 49 | * @return identifier |
| 50 | */ |
| 51 | public T id() { |
| 52 | return identifier; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the hashcode of the identifier. |
| 57 | * |
| 58 | * @return hashcode |
| 59 | */ |
| 60 | @Override |
| 61 | public int hashCode() { |
| 62 | return identifier.hashCode(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Compares two device key identifiers for equality. |
| 67 | * |
| 68 | * @param obj to compare against |
| 69 | * @return true if the objects are equal, false otherwise. |
| 70 | */ |
| 71 | @Override |
| 72 | public boolean equals(Object obj) { |
| 73 | if (this == obj) { |
| 74 | return true; |
| 75 | } |
| 76 | if (obj instanceof Identifier) { |
| 77 | Identifier that = (Identifier) obj; |
| 78 | return this.getClass() == that.getClass() && |
| 79 | Objects.equals(this.identifier, that.identifier); |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns a string representation of a DeviceKeyId. |
| 86 | * |
| 87 | * @return string |
| 88 | */ |
| 89 | public String toString() { |
| 90 | return identifier.toString(); |
| 91 | } |
| 92 | |
| 93 | } |