2


I want to build a small API for myself to make Unit converting in Java easier becuase I couldn't find any good tools fot that. Therefore I wanted to ask If you think that my approach is any good. Btw I'm not very good in Java.

File Structure:

+--converter
|  +--types
|  |  +--Length.java
|  |  +--UnitType.java
|  +--units
|  |  +--length
|  |  |  +--Feet.java
|  |  |  +--Meter.java
|  |  +--Unit.java
|  +--Converter.java

Converter.java:

public class Converter<T extends UnitType> {

    private MathContext mc;

    private Unit<T> from;
    private Unit<T> to;

    public Converter() {
        mc = new MathContext(1000, RoundingMode.HALF_UP);
    }

    public Converter<T> from(Unit<T> from) {
        this.from = from;
        return this;
    }

    public Converter<T> to(Unit<T> to) {
        this.to = to;
        return this;
    }

    public BigDecimal calc() {
        return from.getValue().multiply(from.getRate(to.getName()), mc);
    }
}

UnitType.java:

public class UnitType {

    private String name;

    protected UnitType(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Length.java

public class Length extends UnitType {

    public Length() {
        super("Length");
    }
}

Unit.java:

public abstract class Unit<T extends UnitType> {

    private HashMap<String , BigDecimal> convertRates;
    private BigDecimal value;
    private String name;

    protected Unit(BigDecimal value, String name) {
        this.name = name;
        this.value = value;
        convertRates = new HashMap<String, BigDecimal>();
    }

    public abstract void setRates(HashMap<String , BigDecimal> rates);

    public BigDecimal getRate(String key) {
        return convertRates.get(key);
    }

    public BigDecimal getValue() {
        return value;
    }

    public HashMap<String , BigDecimal> getMapRates() {
        return convertRates;
    }

    public void setMapRates(HashMap<String , BigDecimal> convertRates) {
        this.convertRates = convertRates;
    }

    public String getName() {
        return name;
    }
}

For Exmaple Meter.java:

public class Meter extends Unit<Length> {

    public Meter() {
        super(new BigDecimal(0), "Meter");
    }

    public Meter(BigDecimal value) {
        super(value, "Meter");
        initRates();
    }

    public Meter(String value) {
        super(new BigDecimal(value), "Meter");
        initRates();
    }

    private void initRates() {
        HashMap<String, BigDecimal> convertRates = new HashMap<String, BigDecimal>();

        convertRates.put("Meter", new BigDecimal("1"));
        convertRates.put("Feet", new BigDecimal("3.280839895"));
        convertRates.put("Inch", new BigDecimal("39.3700787402"));

        this.setRates(convertRates);
    }

    @Override
    public void setRates(HashMap<String , BigDecimal> rates) {
        setMapRates(rates);
    }
}

Testing:

BigDecimal test = new Converter<Length>().from(new Meter("45.65"))
                                         .to(new Feet())
                                         .calc();

System.out.println(test);
Output: 149.77034120675

Therefore it works fine but I'm unsure if this is a good Java approach because I have to add all rates seperatly for every unit class.

Thanks in advance.

  • 1
    Maybe look at how [JScience](http://jscience.org/api/javax/measure/unit/package-summary.html) has approached this problem. – Andy Turner Mar 27 '16 at 09:34
  • Have you seen this [stack overflow question](http://stackoverflow.com/questions/1193810/java-metric-unit-conversion-library) ? – Imran Ali Mar 27 '16 at 09:35
  • Well I thank your for your answers but would be my approach bad? Besides that my results are not 100% exact –  Mar 27 '16 at 11:53

0 Answers0