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

com.cognitect.transit.impl.RatioImpl Maven / Gradle / Ivy

Go to download

Transit is a data format and a set of libraries for conveying values between applications written in different languages. This library provides support for marshalling Transit data to/from Java.

There is a newer version: 1.0.371
Show newest version
// Copyright (c) Cognitect, Inc.
// All rights reserved.

package com.cognitect.transit.impl;

import com.cognitect.transit.Ratio;

import java.math.BigDecimal;
import java.math.BigInteger;

public class RatioImpl implements Ratio {

    private final BigInteger numerator;
    private final BigInteger denominator;

    public RatioImpl(BigInteger numerator, BigInteger denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    @Override
    public boolean equals(Object o) {

        if(o instanceof Ratio && ((Ratio)o).getNumerator() == numerator && ((Ratio)o).getDenominator() == denominator)
            return true;
        else

            return false;
    }

    @Override
    public Double getValue() {
        BigDecimal n = new BigDecimal(numerator);
        BigDecimal d = new BigDecimal(denominator);
        return n.divide(d).doubleValue();
    }

    @Override
    public BigInteger getNumerator() {
        return numerator;
    }

    @Override
    public BigInteger getDenominator() {
        return denominator;
    }

    @Override
    public int compareTo(Ratio o) {
        return getValue().compareTo(o.getValue());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy