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