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   import java.util.regex.Pattern;
9   
10  import de.powerstat.validation.interfaces.IValueObject;
11  
12  
13  /**
14   * Address Street.
15   *
16   * Not DSGVO relevant.
17   *
18   * TODO Verify with openstreetmap
19   */
20  public final class Street implements Comparable<Street>, IValueObject
21   {
22    /* *
23     * Cache for singletons.
24     */
25    // private static final Map<String, Street> CACHE = new WeakHashMap<>();
26  
27    /**
28     * Street regexp.
29     */
30    private static final Pattern STREET_REGEXP = Pattern.compile("^[\\p{L}][\\p{L}. -]*$"); //$NON-NLS-1$
31  
32    /**
33     * Street.
34     */
35    private final String street;
36  
37  
38    /**
39     * Constructor.
40     *
41     * @param street Street name
42     * @throws NullPointerException if street is null
43     * @throws IllegalArgumentException if street is not a correct Street name
44     */
45    private Street(final String street)
46     {
47      super();
48      Objects.requireNonNull(street, "street"); //$NON-NLS-1$
49      if ((street.length() < 1) || (street.length() > 32))
50       {
51        throw new IllegalArgumentException("Street with wrong length"); //$NON-NLS-1$
52       }
53      if (!Street.STREET_REGEXP.matcher(street).matches())
54       {
55        throw new IllegalArgumentException("Street with wrong format"); //$NON-NLS-1$
56       }
57      this.street = street;
58     }
59  
60  
61    /**
62     * Street factory.
63     *
64     * @param street Street
65     * @return Street object
66     */
67    public static Street of(final String street)
68     {
69      /*
70      synchronized (Street.class)
71       {
72        Street obj = Street.CACHE.get(street);
73        if (obj != null)
74         {
75          return obj;
76         }
77        obj = new Street(street);
78        Street.CACHE.put(street, obj);
79        return obj;
80       }
81      */
82      return new Street(street);
83     }
84  
85  
86    /**
87     * Returns the value of this Street as a string.
88     *
89     * @return The text value represented by this object after conversion to type string.
90     */
91    @Override
92    public String stringValue()
93     {
94      return this.street;
95     }
96  
97  
98    /**
99     * Calculate hash code.
100    *
101    * @return Hash
102    * @see java.lang.Object#hashCode()
103    */
104   @Override
105   public int hashCode()
106    {
107     return this.street.hashCode();
108    }
109 
110 
111   /**
112    * Is equal with another object.
113    *
114    * @param obj Object
115    * @return true when equal, false otherwise
116    * @see java.lang.Object#equals(java.lang.Object)
117    */
118   @Override
119   public boolean equals(final Object obj)
120    {
121     if (this == obj)
122      {
123       return true;
124      }
125     if (!(obj instanceof Street))
126      {
127       return false;
128      }
129     final Street other = (Street)obj;
130     return this.street.equals(other.street);
131    }
132 
133 
134   /**
135    * Returns the string representation of this Street.
136    *
137    * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
138    *
139    * "Street[street=Hemelinger Heerstraße]"
140    *
141    * @return String representation of this Street
142    * @see java.lang.Object#toString()
143    */
144   @Override
145   public String toString()
146    {
147     final var builder = new StringBuilder();
148     builder.append("Street[street=").append(this.street).append(']'); //$NON-NLS-1$
149     return builder.toString();
150    }
151 
152 
153   /**
154    * Compare with another object.
155    *
156    * @param obj Object to compare with
157    * @return 0: equal; 1: greater; -1: smaller
158    * @see java.lang.Comparable#compareTo(java.lang.Object)
159    */
160   @Override
161   public int compareTo(final Street obj)
162    {
163     Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
164     return this.street.compareTo(obj.street);
165    }
166 
167  }