|
1 | 1 | package eu.europa.ted.eforms.sdk.entity; |
2 | 2 |
|
3 | 3 | import java.util.Objects; |
| 4 | +import java.util.regex.Matcher; |
| 5 | +import java.util.regex.Pattern; |
| 6 | + |
4 | 7 | import com.fasterxml.jackson.databind.JsonNode; |
5 | 8 |
|
6 | 9 | /** |
7 | 10 | * Represents a notice subtype from the SDK's notice-types.json file. |
8 | 11 | */ |
9 | 12 | public abstract class SdkNoticeSubtype implements Comparable<SdkNoticeSubtype> { |
| 13 | + private static final Pattern ID_PATTERN = Pattern.compile("^([A-Za-z_-]*)(\\d+)([A-Za-z_-][A-Za-z0-9_-]*)?$"); |
| 14 | + |
10 | 15 | private final String subTypeId; |
11 | 16 | private final String documentType; |
12 | 17 | private final String type; |
| 18 | + private final String prefix; |
| 19 | + private final int number; |
| 20 | + private final String suffix; |
13 | 21 |
|
14 | 22 | protected SdkNoticeSubtype(String subTypeId, String documentType, String type) { |
15 | 23 | this.subTypeId = subTypeId; |
16 | 24 | this.documentType = documentType; |
17 | 25 | this.type = type; |
| 26 | + |
| 27 | + Matcher m = ID_PATTERN.matcher(subTypeId != null ? subTypeId : ""); |
| 28 | + if (m.matches()) { |
| 29 | + this.prefix = m.group(1); |
| 30 | + this.number = Integer.parseInt(m.group(2)); |
| 31 | + this.suffix = m.group(3) != null ? m.group(3) : ""; |
| 32 | + } else { |
| 33 | + this.prefix = subTypeId != null ? subTypeId : ""; |
| 34 | + this.number = 0; |
| 35 | + this.suffix = ""; |
| 36 | + } |
18 | 37 | } |
19 | 38 |
|
20 | 39 | protected SdkNoticeSubtype(JsonNode json) { |
21 | | - this.subTypeId = json.get("subTypeId").asText(null); |
22 | | - this.documentType = json.get("documentType").asText(null); |
23 | | - this.type = json.get("type").asText(null); |
| 40 | + this(json.get("subTypeId").asText(null), |
| 41 | + json.get("documentType").asText(null), |
| 42 | + json.get("type").asText(null)); |
24 | 43 | } |
25 | 44 |
|
26 | 45 | /** |
@@ -60,7 +79,15 @@ public boolean equals(Object obj) { |
60 | 79 |
|
61 | 80 | @Override |
62 | 81 | public int compareTo(SdkNoticeSubtype o) { |
63 | | - return this.subTypeId.compareTo(o.subTypeId); |
| 82 | + int cmp = this.prefix.compareTo(o.prefix); |
| 83 | + if (cmp != 0) { |
| 84 | + return cmp; |
| 85 | + } |
| 86 | + cmp = Integer.compare(this.number, o.number); |
| 87 | + if (cmp != 0) { |
| 88 | + return cmp; |
| 89 | + } |
| 90 | + return this.suffix.compareTo(o.suffix); |
64 | 91 | } |
65 | 92 |
|
66 | 93 | @Override |
|
0 commit comments