1
2
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
15
16
17
18 public final class BIC implements Comparable<BIC>, IValueObject
19 {
20
21
22
23
24
25
26
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})?$");
30
31
32
33
34 private final String bic;
35
36
37
38
39
40
41
42
43
44 private BIC(final String bic)
45 {
46 super();
47 Objects.requireNonNull(bic, "bic");
48 if ((bic.length() != 8) && (bic.length() != 11))
49 {
50 throw new IllegalArgumentException("BIC with wrong length");
51 }
52 if (!BIC.BIC_REGEXP.matcher(bic).matches())
53 {
54 throw new IllegalArgumentException("BIC with wrong format");
55 }
56 Country.of(bic.substring(4, 6));
57 this.bic = bic;
58 }
59
60
61
62
63
64
65
66
67 public static BIC of(final String bic)
68 {
69
70
71
72
73
74
75
76
77
78
79
80
81
82 return new BIC(bic);
83 }
84
85
86
87
88
89
90
91 @Override
92 public String stringValue()
93 {
94 return this.bic;
95 }
96
97
98
99
100
101
102
103
104 @Override
105 public int hashCode()
106 {
107 return this.bic.hashCode();
108 }
109
110
111
112
113
114
115
116
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
136
137
138
139
140
141
142
143
144 @Override
145 public String toString()
146 {
147 final var builder = new StringBuilder();
148 builder.append("BIC[bic=").append(this.bic).append(']');
149 return builder.toString();
150 }
151
152
153
154
155
156
157
158
159
160 @Override
161 public int compareTo(final BIC obj)
162 {
163 Objects.requireNonNull(obj, "obj");
164 return this.bic.compareTo(obj.bic);
165 }
166
167 }