Changeset View
Changeset View
Standalone View
Standalone View
java/server/src/main/java/org/softwareheritage/graph/SwhPID.java
- This file was moved from java/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; | import org.softwareheritage.graph.Node; | ||||
/** | /** | ||||
* A Software Heritage PID, see <a | * A Software Heritage PID, see <a | ||||
* href="https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html#persistent-identifiers">persistent | * href="https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html#persistent-identifiers">persistent | ||||
* identifier documentation</a>. | * identifier documentation</a>. | ||||
* | * | ||||
* @author Thibault Allançon | * @author Thibault Allançon | ||||
* @version 0.0.1 | * @version 0.0.1 | ||||
* @since 0.0.1 | * @since 0.0.1 | ||||
*/ | */ | ||||
public class SwhId { | public class SwhPID { | ||||
/** Fixed hash length of the PID */ | /** Fixed hash length of the PID */ | ||||
public static final int HASH_LENGTH = 40; | public static final int HASH_LENGTH = 40; | ||||
/** Full PID as a string */ | /** Full PID as a string */ | ||||
String swhId; | String swhPID; | ||||
/** PID node type */ | /** PID node type */ | ||||
Node.Type type; | Node.Type type; | ||||
/** PID hex-encoded SHA1 hash */ | /** PID hex-encoded SHA1 hash */ | ||||
String hash; | String hash; | ||||
/** | /** | ||||
* Constructor. | * Constructor. | ||||
* | * | ||||
* @param swhId full PID as a string | * @param swhPID full PID as a string | ||||
*/ | */ | ||||
public SwhId(String swhId) { | public SwhPID(String swhPID) { | ||||
this.swhId = swhId; | this.swhPID = swhPID; | ||||
// PID format: 'swh:1:type:hash' | // PID format: 'swh:1:type:hash' | ||||
String[] parts = swhId.split(":"); | String[] parts = swhPID.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 PID format to be 'swh:1:type:hash', got: " + swhPID); | ||||
} | } | ||||
this.type = Node.Type.fromStr(parts[2]); | this.type = Node.Type.fromStr(parts[2]); | ||||
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 PID hash format in: " + swhPID); | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public boolean equals(Object otherObj) { | public boolean equals(Object otherObj) { | ||||
if (otherObj == this) return true; | if (otherObj == this) return true; | ||||
if (!(otherObj instanceof SwhId)) return false; | if (!(otherObj instanceof SwhPID)) return false; | ||||
SwhId other = (SwhId) otherObj; | SwhPID other = (SwhPID) otherObj; | ||||
return swhId.equals(other.getSwhId()); | return swhPID.equals(other.getSwhPID()); | ||||
} | } | ||||
@Override | @Override | ||||
public int hashCode() { | public int hashCode() { | ||||
return swhId.hashCode(); | return swhPID.hashCode(); | ||||
} | } | ||||
@Override | @Override | ||||
public String toString() { | public String toString() { | ||||
return swhId; | return swhPID; | ||||
} | } | ||||
/** | /** | ||||
* Returns full PID as a string. | * Returns full PID as a string. | ||||
* | * | ||||
* @return full PID string | * @return full PID string | ||||
*/ | */ | ||||
@JsonValue | @JsonValue | ||||
public String getSwhId() { | public String getSwhPID() { | ||||
return swhId; | return swhPID; | ||||
} | } | ||||
/** | /** | ||||
* Returns PID node type. | * Returns PID node type. | ||||
* | * | ||||
* @return PID corresponding {@link Node.Type} | * @return PID corresponding {@link Node.Type} | ||||
* @see org.softwareheritage.graph.Node.Type | * @see org.softwareheritage.graph.Node.Type | ||||
*/ | */ | ||||
Show All 13 Lines |