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.generated.GeneratedISO4217;
11 import de.powerstat.validation.interfaces.IValueObject;
12
13
14
15
16
17
18
19
20
21 public final class Currency implements Comparable<Currency>, IValueObject
22 {
23
24
25
26
27
28
29
30
31 @SuppressWarnings("java:S5867")
32 private static final Pattern CURRENCY_REGEXP = Pattern.compile("^[A-Z]{3}$");
33
34
35
36
37 private final String code;
38
39
40
41
42
43
44
45
46
47 private Currency(final String code)
48 {
49 super();
50 Objects.requireNonNull(code, "code");
51 if (code.length() != 3)
52 {
53 throw new IllegalArgumentException("Length is not 3");
54 }
55 if (!Currency.CURRENCY_REGEXP.matcher(code).matches())
56 {
57 throw new IllegalArgumentException("Code contains illegal character");
58 }
59 if (!GeneratedISO4217.contains(code))
60 {
61 throw new IllegalArgumentException("Unknown ISO4217 code: " + code);
62 }
63 this.code = code;
64 }
65
66
67
68
69
70
71
72
73 public static Currency of(final String code)
74 {
75
76
77
78
79
80
81
82
83
84
85
86
87
88 return new Currency(code);
89 }
90
91
92
93
94
95
96
97 @Override
98 public String stringValue()
99 {
100 return this.code;
101 }
102
103
104
105
106
107
108
109
110 @Override
111 public int hashCode()
112 {
113 return this.code.hashCode();
114 }
115
116
117
118
119
120
121
122
123
124 @Override
125 public boolean equals(final Object obj)
126 {
127 if (this == obj)
128 {
129 return true;
130 }
131 if (!(obj instanceof Currency))
132 {
133 return false;
134 }
135 final Currency other = (Currency)obj;
136 return this.code.equals(other.code);
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150 @Override
151 public String toString()
152 {
153 final var builder = new StringBuilder();
154 builder.append("Currency[code=").append(this.code).append(']');
155 return builder.toString();
156 }
157
158
159
160
161
162
163
164
165
166 @Override
167 public int compareTo(final Currency obj)
168 {
169 Objects.requireNonNull(obj, "obj");
170 return this.code.compareTo(obj.code);
171 }
172
173 }