Changeset View
Changeset View
Standalone View
Standalone View
api/server/src/main/java/org/softwareheritage/graph/SwhId.java
package org.softwareheritage.graph; | package org.softwareheritage.graph; | ||||
import com.fasterxml.jackson.annotation.JsonValue; | import com.fasterxml.jackson.annotation.JsonValue; | ||||
import org.softwareheritage.graph.Node; | |||||
public class SwhId { | public class SwhId { | ||||
public static final int HASH_LENGTH = 40; | public static final int HASH_LENGTH = 40; | ||||
String swhId; | String swhId; | ||||
String type; | Node.Type type; | ||||
String hash; | String hash; | ||||
// SWH ID format: 'swh:1:type:hash' | // SWH ID format: 'swh:1:type:hash' | ||||
// https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html | // https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html | ||||
public SwhId(String swhId) { | public SwhId(String swhId) { | ||||
this.swhId = swhId; | this.swhId = swhId; | ||||
String[] parts = swhId.split(":"); | String[] parts = swhId.split(":"); | ||||
if (parts.length != 4 || !parts[0].equals("swh") || !parts[1].equals("1")) { | if (parts.length != 4 || !parts[0].equals("swh") || !parts[1].equals("1")) { | ||||
throw new IllegalArgumentException("Expected SWH ID format to be 'swh:1:type:hash', got: " + swhId); | throw new IllegalArgumentException("Expected SWH ID format to be 'swh:1:type:hash', got: " + swhId); | ||||
} | } | ||||
this.type = parts[2]; | String type = parts[2]; | ||||
if (!type.matches("cnt|dir|rel|rev|snp")) { | if (!type.matches("cnt|dir|rel|rev|snp")) { | ||||
throw new IllegalArgumentException("Unknown SWH ID type in: " + swhId); | throw new IllegalArgumentException("Unknown SWH ID type in: " + swhId); | ||||
} | } | ||||
this.type = Node.Type.fromStr(type); | |||||
this.hash = parts[3]; | this.hash = parts[3]; | ||||
if (!hash.matches("[0-9a-f]{" + HASH_LENGTH + "}")) { | if (!hash.matches("[0-9a-f]{" + HASH_LENGTH + "}")) { | ||||
throw new IllegalArgumentException("Wrong SWH ID hash format in: " + swhId); | throw new IllegalArgumentException("Wrong SWH ID hash format in: " + swhId); | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
Show All 10 Lines | public String toString() { | ||||
return swhId; | return swhId; | ||||
} | } | ||||
@JsonValue | @JsonValue | ||||
public String getSwhId() { | public String getSwhId() { | ||||
return swhId; | return swhId; | ||||
} | } | ||||
public String getType() { | public Node.Type getType() { | ||||
return type; | return type; | ||||
} | } | ||||
public String getHash() { | public String getHash() { | ||||
return hash; | return hash; | ||||
} | } | ||||
} | } |