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

com.savl.ripple.client.payments.Alternative Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.savl.ripple.client.payments;

import com.savl.ripple.core.coretypes.Amount;
import com.savl.ripple.core.coretypes.PathSet;
import com.savl.ripple.core.coretypes.hash.HalfSha512;
import com.savl.ripple.core.coretypes.hash.Hash256;
import org.json.JSONObject;

public class Alternative implements Comparable {

    public Amount sourceAmount;
    public PathSet paths;
    public Hash256 hash;

    public Alternative(PathSet paths, Amount sourceAmount) {
        this.paths = paths;
        this.sourceAmount = sourceAmount;
        this.hash = calculateHash(paths, sourceAmount);
    }

    private Hash256 calculateHash(PathSet paths, Amount sourceAmount) {
        HalfSha512 half = new HalfSha512();
        half.update(paths.toBytes());
        half.update(sourceAmount.toBytes());
        return half.finish();
    }

    @Override
    public int compareTo(Alternative another) {
        return hash.compareTo(another.hash);
    }

    public boolean directXRP() {
        return !hasPaths() && sourceAmount.isNative();
    }

    boolean hasPaths() {
        return paths.size() > 0;
    }

    @Override
    public String toString() {
        JSONObject o = toJSON();
        return o.toString(2);
    }

    public JSONObject toJSON() {
        JSONObject o = new JSONObject();
        o.put("source_amount", sourceAmount.toJSON());
        o.put("paths", paths.toJSON());
        return o;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy