io.ipfs.api.MerkleNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-xpx-ipfs-api Show documentation
Show all versions of java-xpx-ipfs-api Show documentation
The ProximaX IPFS Java API is a Java library for interacting IPFS server.
The newest version!
package io.ipfs.api;
import io.ipfs.cid.*;
import io.ipfs.multihash.Multihash;
import java.util.*;
import java.util.stream.*;
public class MerkleNode {
public final Multihash hash;
public final Optional name;
public final Optional size;
public final Optional largeSize;
public final Optional type;
public final List links;
public final Optional data;
public MerkleNode(String hash,
Optional name,
Optional size,
Optional largeSize,
Optional type,
List links,
Optional data) {
this.name = name;
this.hash = Cid.decode(hash);
this.size = size;
this.largeSize = largeSize;
this.type = type;
this.links = links;
this.data = data;
}
public MerkleNode(String hash) {
this(hash, Optional.empty());
}
public MerkleNode(String hash, Optional name) {
this(hash, name, Optional.empty(), Optional.empty(), Optional.empty(), Arrays.asList(), Optional.empty());
}
@Override
public boolean equals(Object b) {
if (!(b instanceof MerkleNode))
return false;
MerkleNode other = (MerkleNode) b;
return hash.equals(other.hash); // ignore name hash says it all
}
@Override
public int hashCode() {
return hash.hashCode();
}
public static MerkleNode fromJSON(Object rawjson) {
if (rawjson instanceof String)
return new MerkleNode((String)rawjson);
Map json = (Map)rawjson;
String hash = (String)json.get("Hash");
if (hash == null)
hash = (String)json.get("Key");
Optional name = json.containsKey("Name") ?
Optional.of((String) json.get("Name")) :
Optional.empty();
Object rawSize = json.get("Size");
Optional size = rawSize instanceof Integer ?
Optional.of((Integer) rawSize) :
Optional.empty();
Optional largeSize = rawSize instanceof String ?
Optional.of((String) json.get("Size")) :
Optional.empty();
if ("error".equals(json.get("Type")))
throw new IllegalStateException("Remote IPFS error: " + json.get("Message"));
Optional type = json.containsKey("Type") ?
Optional.of((Integer) json.get("Type")) :
Optional.empty();
List