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