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

net.osslabz.crypto.CurrencyPair Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
package net.osslabz.crypto;


import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.Objects;

/**
 * Identifies a currency pair.
 *
 * @param baseCurrencyCode
 * @param counterCurrencyCode
 */
public record CurrencyPair(String baseCurrencyCode, String counterCurrencyCode) {

    public CurrencyPair {
        Objects.requireNonNull(baseCurrencyCode, "baseCurrencyCode cannot be null");
        Objects.requireNonNull(counterCurrencyCode, "counterCurrencyCode cannot be null");
    }

    public static CurrencyPair fromString(String currencyPair) {
        Objects.requireNonNull(currencyPair, "currencyPair cannot be null");
        if (!currencyPair.contains("-")) {
            throw new IllegalArgumentException("currencyPair must contain '-'");
        }
        String[] split = currencyPair.split("-");

        return new CurrencyPair(split[0], split[1]);
    }

    @JsonIgnore
    public String getLabel() {
        return "%s-%s".formatted(this.baseCurrencyCode, this.counterCurrencyCode);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy