2 * Copyright 2016-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.utils;
18 import java.util.Objects;
20 import static com.google.common.base.Preconditions.checkNotNull;
23 * Abstract identifier backed by another value, e.g. string, int.
25 public class AbstractIdentifier<T extends Comparable<T>> implements Identifier<T> {
27 protected final T identifier; // backing identifier value
30 * Constructor for serialization.
32 protected AbstractIdentifier() {
33 this.identifier = null;
37 * Constructs an identifier backed by the specified value.
39 * @param value the backing value
41 protected AbstractIdentifier(T value) {
42 this.identifier = checkNotNull(value, "Identifier cannot be null.");
46 * Returns the backing identifier value.
55 * Returns the hashcode of the identifier.
60 public int hashCode() {
61 return identifier.hashCode();
65 * Compares two device key identifiers for equality.
67 * @param obj to compare against
68 * @return true if the objects are equal, false otherwise.
71 public boolean equals(Object obj) {
75 if (obj instanceof AbstractIdentifier) {
76 AbstractIdentifier that = (AbstractIdentifier) obj;
77 return this.getClass() == that.getClass()
78 && Objects.equals(this.identifier, that.identifier);
84 * Returns a string representation of a DeviceKeyId.
88 public String toString() {
89 return identifier.toString();