Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
package eu.europa.ted.eforms.sdk.entity;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fasterxml.jackson.databind.JsonNode;

/**
* Represents a notice subtype from the SDK's notice-types.json file.
*/
public abstract class SdkNoticeSubtype implements Comparable<SdkNoticeSubtype> {
private static final Pattern ID_PATTERN = Pattern.compile("^([A-Za-z_-]*)(\\d+)([A-Za-z_-][A-Za-z0-9_-]*)?$");

private final String subTypeId;
private final String documentType;
private final String type;
private final String prefix;
private final int number;
private final String suffix;

protected SdkNoticeSubtype(String subTypeId, String documentType, String type) {
this.subTypeId = subTypeId;
this.documentType = documentType;
this.type = type;

Matcher m = ID_PATTERN.matcher(subTypeId != null ? subTypeId : "");
if (m.matches()) {
this.prefix = m.group(1);
this.number = Integer.parseInt(m.group(2));
this.suffix = m.group(3) != null ? m.group(3) : "";
} else {
this.prefix = subTypeId != null ? subTypeId : "";
this.number = 0;
this.suffix = "";
}
}

protected SdkNoticeSubtype(JsonNode json) {
this.subTypeId = json.get("subTypeId").asText(null);
this.documentType = json.get("documentType").asText(null);
this.type = json.get("type").asText(null);
this(json.get("subTypeId").asText(null),
json.get("documentType").asText(null),
json.get("type").asText(null));
}

/**
Expand Down Expand Up @@ -60,7 +79,15 @@ public boolean equals(Object obj) {

@Override
public int compareTo(SdkNoticeSubtype o) {
return this.subTypeId.compareTo(o.subTypeId);
int cmp = this.prefix.compareTo(o.prefix);
if (cmp != 0) {
return cmp;
}
cmp = Integer.compare(this.number, o.number);
if (cmp != 0) {
return cmp;
}
return this.suffix.compareTo(o.suffix);
}

@Override
Expand Down