All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ipfs.api.MerkleNode Maven / Gradle / Ivy

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 linksRaw = (List) json.get("Links");
        List links = linksRaw == null ?
                Collections.emptyList() :
                linksRaw.stream().map(x -> MerkleNode.fromJSON(x)).collect(Collectors.toList());
        Optional data = json.containsKey("Data") ? Optional.of(((String)json.get("Data")).getBytes()): Optional.empty();
        return new MerkleNode(hash, name, size, largeSize, type, links, data);
    }

    public Object toJSON() {
        Map res = new TreeMap<>();
        res.put("Hash", hash);
        res.put("Links", links.stream().map(x -> x.hash).collect(Collectors.toList()));
        data.ifPresent(bytes -> res.put("Data", bytes));
        name.ifPresent(s -> res.put("Name", s));
        if (size.isPresent()) {
            res.put("Size", size.get());
        } else {
            largeSize.ifPresent(s -> res.put("Size", s));
        }
        type.ifPresent(integer -> res.put("Type", integer));
        return res;
    }

    public String toJSONString() {
        return JSONParser.toString(toJSON());
    }
}