View Javadoc
1   /*
2    * Copyright (C) 2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values;
5   
6   
7   import java.util.Objects;
8   
9   import de.powerstat.validation.interfaces.IValueObject;
10  
11  
12  /**
13   * Universally Unique Identifier.
14   *
15   * Possibly DSGVO relevant.
16   */
17  public class UUID implements Comparable<UUID>, IValueObject
18   {
19    /**
20     * UUID.
21     */
22    private final java.util.UUID uuid;
23  
24  
25    /**
26     * Default constructor.
27     */
28    private UUID()
29     {
30      super();
31      this.uuid = java.util.UUID.randomUUID();
32     }
33  
34  
35    /**
36     * Constructor.
37     *
38     * @param value UUID string value
39     */
40    private UUID(final String value)
41     {
42      super();
43      this.uuid = java.util.UUID.fromString(value);
44     }
45  
46  
47    /**
48     * UUID factory.
49     *
50     * @return UUID object
51     */
52    public static UUID of()
53     {
54      return new UUID();
55     }
56  
57  
58    /**
59     * UUID factory.
60     *
61     * @param value UUID string
62     * @return UUID object
63     */
64    public static UUID of(final String value)
65     {
66      return new UUID(value);
67     }
68  
69  
70    /**
71     * Returns the value of this UUID as an String.
72     *
73     * @return The numeric value represented by this object after conversion to type String.
74     */
75    @Override
76    public String stringValue()
77     {
78      return String.valueOf(this.uuid.toString());
79     }
80  
81  
82    /**
83     * Calculate hash code.
84     *
85     * @return Hash
86     * @see java.lang.Object#hashCode()
87     */
88    @Override
89    public int hashCode()
90     {
91      return this.uuid.hashCode();
92     }
93  
94  
95    /**
96     * Is equal with another object.
97     *
98     * @param obj Object
99     * @return true when equal, false otherwise
100    * @see java.lang.Object#equals(java.lang.Object)
101    */
102   @Override
103   public boolean equals(final Object obj)
104    {
105     if (this == obj)
106      {
107       return true;
108      }
109     if (!(obj instanceof UUID))
110      {
111       return false;
112      }
113     final UUID other = (UUID)obj;
114     return this.uuid.equals(other.uuid);
115    }
116 
117 
118   /**
119    * Returns the string representation of this UUID.
120    *
121    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
122    *
123    * "UUID[uuid=e58ed763-928c-4155-bee9-fdbaaadc15f3]"
124    *
125    * @return String representation of this UUID
126    * @see java.lang.Object#toString()
127    */
128   @Override
129   public String toString()
130    {
131     final var builder = new StringBuilder();
132     builder.append("UUID[uuid=").append(this.uuid.toString()).append(']'); //$NON-NLS-1$
133     return builder.toString();
134    }
135 
136 
137   /**
138    * Compare with another object.
139    *
140    * @param obj Object to compare with
141    * @return 0: equal; 1: greater; -1: smaller
142    * @see java.lang.Comparable#compareTo(java.lang.Object)
143    */
144   @Override
145   public int compareTo(final UUID obj)
146    {
147     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
148     return this.uuid.compareTo(obj.uuid);
149    }
150 
151  }