View Javadoc
1   /*
2    *  Copyright 2022 Búraló Technologies
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 com.buralotech.oss.identifier.uuid;
18  
19  import com.buralotech.oss.identifier.api.Identifier;
20  
21  import java.util.Arrays;
22  import java.util.Objects;
23  
24  /**
25   * And identifier has a textual and binary representation.
26   *
27   * @param text   The textual representation.
28   * @param binary The binary representation.
29   */
30  public record UUIDIdentifier(String text, byte[] binary) implements Identifier {
31  
32      /**
33       * Determine if two identifiers are equivalent.
34       *
35       * @param other The other identifier.
36       * @return {@code true} if the two identifiers are equivalent. Otherwise, {@code false}.
37       */
38      @Override
39      public boolean equals(final Object other) {
40          return (this == other) || (other instanceof UUIDIdentifier that && text.equals(that.text) && Arrays.equals(binary, that.binary));
41      }
42  
43      /**
44       * Calculate a hash code for an identifier.
45       *
46       * @return The hash code.
47       */
48      @Override
49      public int hashCode() {
50          return Objects.hash(text) * 31 + Arrays.hashCode(binary);
51      }
52  
53      /**
54       * Compare based on the binary representation.
55       *
56       * @param other the object to be compared.
57       * @return <ul>
58       * <li>0 - if the two identifiers are equivalent</li>
59       * <li>&lt; 0 - </li>
60       * <li>&gt; 0 - </li>
61       * </ul>
62       */
63      @Override
64      public int compareTo(final Identifier other) {
65          return Arrays.compareUnsigned(binary, other.binary());
66      }
67  
68      /**
69       * Get a string representation.
70       *
71       * @return The string representation.
72       */
73      @Override
74      public String toString() {
75          return text;
76      }
77  }