View Javadoc
1   /*
2    * Copyright (C) 2020-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   * Address Post office box number.
14   *
15   * DSGVO relevant.
16   */
17  public final class PoBoxNumber implements Comparable<PoBoxNumber>, IValueObject
18   {
19    /* *
20     * Cache for singletons.
21     */
22    // private static final Map<Long, PoBoxNumber> CACHE = new WeakHashMap<>();
23  
24    /**
25     * Post office box number.
26     */
27    private final long poBoxNumber;
28  
29  
30    /**
31     * Constructor.
32     *
33     * @param poBoxNumber PO box number 1-..
34     * @throws IndexOutOfBoundsException When the poBoxNumber is less than 1
35     */
36    private PoBoxNumber(final long poBoxNumber)
37     {
38      super();
39      if (poBoxNumber < 1)
40       {
41        throw new IndexOutOfBoundsException("POBoxNumber number out of range (1-..)!"); //$NON-NLS-1$
42       }
43      this.poBoxNumber = poBoxNumber;
44     }
45  
46  
47    /**
48     * PoBoxNumber factory.
49     *
50     * @param poBoxNumber PoBox number 1-..
51     * @return PoBoxNumber object
52     */
53    public static PoBoxNumber of(final long poBoxNumber)
54     {
55      /*
56      synchronized (PoBoxNumber.class)
57       {
58        PoBoxNumber obj = PoBoxNumber.CACHE.get(poBoxNumber);
59        if (obj != null)
60         {
61          return obj;
62         }
63        obj = new PoBoxNumber(poBoxNumber);
64        PoBoxNumber.CACHE.put(Long.valueOf(poBoxNumber), obj);
65        return obj;
66       }
67      */
68      return new PoBoxNumber(poBoxNumber);
69     }
70  
71  
72    /**
73     * PoBoxNumber factory.
74     *
75     * @param value PoBox number 1-.. string
76     * @return PoBoxNumber object
77     */
78    public static PoBoxNumber of(final String value)
79     {
80      return of(Long.parseLong(value));
81     }
82  
83  
84    /**
85     * Returns the value of this BFPONumber as a long.
86     *
87     * @return The numeric value represented by this object after conversion to type long.
88     */
89    public long longValue()
90     {
91      return this.poBoxNumber;
92     }
93  
94  
95    /**
96     * Returns the value of this PoBoNumber as a string.
97     *
98     * @return The text value represented by this object after conversion to type string.
99     */
100   @Override
101   public String stringValue()
102    {
103     return Long.toString(this.poBoxNumber);
104    }
105 
106 
107   /**
108    * Calculate hash code.
109    *
110    * @return Hash
111    * @see java.lang.Object#hashCode()
112    */
113   @Override
114   public int hashCode()
115    {
116     return Long.hashCode(this.poBoxNumber);
117    }
118 
119 
120   /**
121    * Is equal with another object.
122    *
123    * @param obj Object
124    * @return true when equal, false otherwise
125    * @see java.lang.Object#equals(java.lang.Object)
126    */
127   @Override
128   public boolean equals(final Object obj)
129    {
130     if (this == obj)
131      {
132       return true;
133      }
134     if (!(obj instanceof PoBoxNumber))
135      {
136       return false;
137      }
138     final PoBoxNumber other = (PoBoxNumber)obj;
139     return this.poBoxNumber == other.poBoxNumber;
140    }
141 
142 
143   /**
144    * Returns the string representation of this PoBoxNumber.
145    *
146    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
147    *
148    * "PoBoxNumber[poBoxNumber=4711]"
149    *
150    * @return String representation of this PoBoxNumber
151    * @see java.lang.Object#toString()
152    */
153   @Override
154   public String toString()
155    {
156     final var builder = new StringBuilder(25);
157     builder.append("PoBoxNumber[poBoxNumber=").append(this.poBoxNumber).append(']'); //$NON-NLS-1$
158     return builder.toString();
159    }
160 
161 
162   /**
163    * Compare with another object.
164    *
165    * @param obj Object to compare with
166    * @return 0: equal; 1: greater; -1: smaller
167    * @see java.lang.Comparable#compareTo(java.lang.Object)
168    */
169   @Override
170   public int compareTo(final PoBoxNumber obj)
171    {
172     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
173     return Long.compare(this.poBoxNumber, obj.poBoxNumber);
174    }
175 
176  }