Skip to content

Commit

Permalink
Switch to Pattern match
Browse files Browse the repository at this point in the history
  • Loading branch information
samleeflang committed Nov 19, 2024
1 parent 9ba31d3 commit d695ab1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package eu.dissco.core.translator.terms.utils;

import static eu.dissco.core.translator.schema.Identifier.DctermsType.ARK;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.DOI;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.HANDLE;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.PURL;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.URL;
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE;
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE;
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT;
import static java.util.regex.Pattern.compile;

import eu.dissco.core.translator.schema.Identifier;
import eu.dissco.core.translator.schema.Identifier.DctermsType;
Expand All @@ -15,31 +18,38 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Triple;

@Slf4j
public class IdentifierUtils {

private static final Map<List<String>, Triple<DctermsType, String, OdsGupriLevel>> map = getPrefixMap();
private static final Map<List<Pattern>, Triple<DctermsType, String, OdsGupriLevel>> map = getPrefixMap();

private IdentifierUtils() {
// This is a Utility class
}

private static Map<List<String>, Triple<DctermsType, String, OdsGupriLevel>> getPrefixMap() {
var linkedMap = new LinkedHashMap<List<String>, Triple<DctermsType, String, OdsGupriLevel>>();
linkedMap.put(List.of("https://doi.org"),
private static Map<List<Pattern>, Triple<DctermsType, String, OdsGupriLevel>> getPrefixMap() {
var linkedMap = new LinkedHashMap<List<Pattern>, Triple<DctermsType, String, OdsGupriLevel>>();
linkedMap.put(List.of(compile("^https?://doi.org")),
Triple.of(DOI, "DOI", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT));
linkedMap.put(List.of("https://hdl.handle.net"),
linkedMap.put(List.of(compile("^https?://hdl.handle.net")),
Triple.of(HANDLE, "Handle", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE_FDO_COMPLIANT));
linkedMap.put(List.of("http://www.wikidata.org", "https://www.wikidata.org"),
linkedMap.put(List.of(compile("^https?://www.wikidata.org")),
Triple.of(URL, "Wikidata", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE));
linkedMap.put(List.of("http://orcid.org", "https://orcid.org"),
linkedMap.put(List.of(compile("^https?://orcid.org")),
Triple.of(URL, "ORCID", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE));
linkedMap.put(List.of("http", "https"),
linkedMap.put(List.of(compile("^https?://orcid.org")),
Triple.of(URL, "ORCID", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE));
linkedMap.put(List.of(compile("^https?://\\w+.\\w+/ark:/\\w+/\\w+")),
Triple.of(ARK, "ARK", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE));
linkedMap.put(List.of(compile("https?://purl.org")),
Triple.of(PURL, "PURL", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE));
linkedMap.put(List.of(compile("^https?")),
Triple.of(URL, "URL", GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE));
linkedMap.put(List.of("urn:uuid"),
linkedMap.put(List.of(compile("^urn:uuid")),
Triple.of(DctermsType.UUID, "UUID", GLOBALLY_UNIQUE_STABLE));
return linkedMap;
}
Expand All @@ -64,7 +74,7 @@ public static Identifier addIdentifier(String identifierString, String identifie
.withOdsIdentifierStatus(identifierStatus);
for (var entry : map.entrySet()) {
for (var prefix : entry.getKey()) {
if (identifierString.startsWith(prefix)) {
if (prefix.matcher(identifierString).find()) {
identifier.setDctermsType(entry.getValue().getLeft());
identifier.setDctermsTitle(getDcTermsTitle(identifierName, entry.getValue().getMiddle()));
identifier.setOdsGupriLevel(entry.getValue().getRight());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package eu.dissco.core.translator.terms.utils;

import static eu.dissco.core.translator.schema.Identifier.DctermsType.ARK;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.HANDLE;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.LOCALLY_UNIQUE_IDENTIFIER;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.PURL;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.URL;
import static eu.dissco.core.translator.schema.Identifier.DctermsType.UUID;
import static eu.dissco.core.translator.schema.Identifier.OdsGupriLevel.GLOBALLY_UNIQUE_STABLE;
Expand Down Expand Up @@ -44,6 +46,12 @@ public static Stream<Arguments> identifierProvider() {
Arguments.of("https://geocollections.info/specimen/126758", "abcd:unitGUID", null,
createIdentifier("https://geocollections.info/specimen/126758", URL, "abcd:unitGUID",
GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE, null)),
Arguments.of("http://n2t.net/ark:/65665/3173bef93-f5c6-4534-bd31-42289606938b", "dwc:catalogueNumber", null,
createIdentifier("http://n2t.net/ark:/65665/3173bef93-f5c6-4534-bd31-42289606938b", ARK, "dwc:catalogueNumber",
GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE, null)),
Arguments.of("http://purl.org/dc/terms/accessRights", null, null,
createIdentifier("http://purl.org/dc/terms/accessRights", PURL, "PURL",
GLOBALLY_UNIQUE_STABLE_PERSISTENT_RESOLVABLE, null)),
Arguments.of("AVES-071259", "dwc:occurrenceID", null,
createIdentifier("AVES-071259", LOCALLY_UNIQUE_IDENTIFIER, "dwc:occurrenceID",
LOCALLY_UNIQUE_STABLE, null)),
Expand Down

0 comments on commit d695ab1

Please sign in to comment.