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 * Business Identifier Code (BIC) ISO 9362.
15 *
16 * Not DSGVO relevant.
17 */
18 public final class BIC implements Comparable<BIC>, IValueObject
19 {
20 /* *
21 * Cache for singletons.
22 */
23 // private static final Map<String, BIC> CACHE = new WeakHashMap<>();
24
25 /**
26 * BIC regexp.
27 */
28 @SuppressWarnings("java:S5867")
29 private static final Pattern BIC_REGEXP = Pattern.compile("^[A-Z0-9]{4}[A-Z]{2}[A-Z2-9][0-9A-NP-Z](XXX|[0-9A-WY-Z][0-9A-Z]{2})?$"); //$NON-NLS-1$
30
31 /**
32 * BIC.
33 */
34 private final String bic;
35
36
37 /**
38 * Constructor.
39 *
40 * @param bic BIC
41 * @throws NullPointerException if bic is null
42 * @throws IllegalArgumentException if bic is not a correct bic
43 */
44 private BIC(final String bic)
45 {
46 super();
47 Objects.requireNonNull(bic, "bic"); //$NON-NLS-1$
48 if ((bic.length() != 8) && (bic.length() != 11))
49 {
50 throw new IllegalArgumentException("BIC with wrong length"); //$NON-NLS-1$
51 }
52 if (!BIC.BIC_REGEXP.matcher(bic).matches())
53 {
54 throw new IllegalArgumentException("BIC with wrong format"); //$NON-NLS-1$
55 }
56 /* final Country country = */ Country.of(bic.substring(4, 6));
57 this.bic = bic;
58 }
59
60
61 /**
62 * BIC factory.
63 *
64 * @param bic BIC
65 * @return BIC object
66 */
67 public static BIC of(final String bic)
68 {
69 /*
70 synchronized (BIC.class)
71 {
72 BIC obj = BIC.CACHE.get(bic);
73 if (obj != null)
74 {
75 return obj;
76 }
77 obj = new BIC(bic);
78 BIC.CACHE.put(bic, obj);
79 return obj;
80 }
81 */
82 return new BIC(bic);
83 }
84
85
86 /**
87 * Returns the value of this BIC 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.bic;
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.bic.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 BIC))
126 {
127 return false;
128 }
129 final BIC other = (BIC)obj;
130 return this.bic.equals(other.bic);
131 }
132
133
134 /**
135 * Returns the string representation of this BIC.
136 *
137 * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
138 *
139 * "BIC[bic=BELADEBEXXX]"
140 *
141 * @return String representation of this BIC
142 * @see java.lang.Object#toString()
143 */
144 @Override
145 public String toString()
146 {
147 final var builder = new StringBuilder();
148 builder.append("BIC[bic=").append(this.bic).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 BIC obj)
162 {
163 Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
164 return this.bic.compareTo(obj.bic);
165 }
166
167 }