Changeset View
Changeset View
Standalone View
Standalone View
java/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.apache.commons.codec.DecoderException; | import org.apache.commons.codec.DecoderException; | ||||
import org.apache.commons.codec.binary.Hex; | import org.apache.commons.codec.binary.Hex; | ||||
/** | /** | ||||
* A Software Heritage persistent identifier (SWHID), see <a | * A Software Heritage persistent identifier (SWHID), see <a href= | ||||
* href="https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html#persistent-identifiers">persistent | * "https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html#persistent-identifiers">persistent | ||||
* identifier documentation</a>. | * identifier documentation</a>. | ||||
* | * | ||||
* @author The Software Heritage developers | * @author The Software Heritage developers | ||||
*/ | */ | ||||
public class SWHID { | public class SWHID { | ||||
/** Fixed hash length of the SWHID */ | /** Fixed hash length of the SWHID */ | ||||
public static final int HASH_LENGTH = 40; | public static final int HASH_LENGTH = 40; | ||||
Show All 20 Lines | public SWHID(String swhid) { | ||||
if (!parts[3].matches("[0-9a-f]{" + HASH_LENGTH + "}")) { | if (!parts[3].matches("[0-9a-f]{" + HASH_LENGTH + "}")) { | ||||
throw new IllegalArgumentException("malformed SWHID: " + swhid); | throw new IllegalArgumentException("malformed SWHID: " + swhid); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Creates a SWHID from a compact binary representation. | * Creates a SWHID from a compact binary representation. | ||||
* <p> | * <p> | ||||
* The binary format is specified in the Python module | * The binary format is specified in the Python module swh.graph.swhid:str_to_bytes . | ||||
* swh.graph.swhid:str_to_bytes . | |||||
*/ | */ | ||||
public static SWHID fromBytes(byte[] input) { | public static SWHID fromBytes(byte[] input) { | ||||
byte[] digest = new byte[20]; | byte[] digest = new byte[20]; | ||||
System.arraycopy(input, 2, digest, 0, digest.length); | System.arraycopy(input, 2, digest, 0, digest.length); | ||||
String swhidStr = String.format( | String swhidStr = String.format("swh:%d:%s:%s", input[0], Node.Type.fromInt(input[1]).toString().toLowerCase(), | ||||
"swh:%d:%s:%s", | Hex.encodeHexString(digest)); | ||||
input[0], | |||||
Node.Type.fromInt(input[1]).toString().toLowerCase(), | |||||
Hex.encodeHexString(digest) | |||||
); | |||||
return new SWHID(swhidStr); | return new SWHID(swhidStr); | ||||
} | } | ||||
@Override | @Override | ||||
public boolean equals(Object otherObj) { | public boolean equals(Object otherObj) { | ||||
if (otherObj == this) | if (otherObj == this) | ||||
return true; | return true; | ||||
if (!(otherObj instanceof SWHID)) | if (!(otherObj instanceof SWHID)) | ||||
Show All 11 Lines | public class SWHID { | ||||
@Override | @Override | ||||
public String toString() { | public String toString() { | ||||
return swhid; | return swhid; | ||||
} | } | ||||
/** | /** | ||||
* Converts SWHID to a compact binary representation. | * Converts SWHID to a compact binary representation. | ||||
* <p> | * <p> | ||||
* The binary format is specified in the Python module | * The binary format is specified in the Python module swh.graph.swhid:str_to_bytes . | ||||
* swh.graph.swhid:str_to_bytes . | |||||
*/ | */ | ||||
public byte[] toBytes() { | public byte[] toBytes() { | ||||
byte[] bytes = new byte[22]; | byte[] bytes = new byte[22]; | ||||
byte[] digest; | byte[] digest; | ||||
bytes[0] = (byte) 1; // namespace version | bytes[0] = (byte) 1; // namespace version | ||||
bytes[1] = (byte) Node.Type.toInt(this.type); // SWHID type | bytes[1] = (byte) Node.Type.toInt(this.type); // SWHID type | ||||
try { | try { | ||||
digest = Hex.decodeHex(this.swhid.substring(10)); // SHA1 hash | digest = Hex.decodeHex(this.swhid.substring(10)); // SHA1 hash | ||||
System.arraycopy(digest, 0, bytes, 2, digest.length); | System.arraycopy(digest, 0, bytes, 2, digest.length); | ||||
} catch (DecoderException e) { | } catch (DecoderException e) { | ||||
throw new IllegalArgumentException("invalid hex sequence in SWHID: " + this.swhid); | throw new IllegalArgumentException("invalid hex sequence in SWHID: " + this.swhid); | ||||
} | } | ||||
return bytes; | return bytes; | ||||
} | } | ||||
Show All 20 Lines |