Here is a universal converter that uses hollow matrices, and this makes a transitive closure to combine well-known converters. In this case, it converts units of length:
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Converter { private static final Map<String, Map<String, Double>> FACTOR_MAP = new HashMap<String, Map<String, Double>>(); public static double convert(String from, String to, double value) { List<Step> stack = new ArrayList<Step>(); stack.add(new Step(from, value)); while (!stack.isEmpty()) { Step s = stack.remove(0); double val = s.value; String source = s.unit; if (source.equals(to)) { return val; } Map<String, Double> map = FACTOR_MAP.get(source); if (map != null) { for (Map.Entry<String,Double> entry: map.entrySet()) { stack.add(new Step(entry.getKey(), val*entry.getValue())); } } } throw new IllegalArgumentException("Cannot not convert from " + from + " to " + to); } public static void registerFactor(String from, String to, double factor) { putFactor(from, to, factor); putFactor(to, from, 1.0/factor); } private static void putFactor(String from, String to, double factor) { Map<String, Double> map = FACTOR_MAP.get(from); if (map == null) { map = new HashMap<String, Double>(); FACTOR_MAP.put(from, map); } map.put(to, factor); } static { registerFactor("cm", "mm", 10); registerFactor("in", "cm", 2.54); registerFactor("in", "pt", 72); registerFactor("pc", "pt", 12); registerFactor("px", "mm", 0.28); } private static class Step { private String unit; private double value; Step(String unit, double value) { this.unit = unit; this.value = value; } } }
The following program:
public class Main { private static final String UNITS[] = {"cm", "mm", "in", "pt", "pc", "px"}; public static void main(String[] args) { for (String unit1: UNITS) { for (String unit2: UNITS) { System.out.println("1" + unit1 + " = " + Converter.convert(unit1, unit2, 1) + unit2); } } } }
gives the following results:
1cm = 1.0cm 1cm = 10.0mm 1cm = 0.39370078740157477in 1cm = 28.346456692913385pt 1cm = 2.3622047244094486pc 1cm = 35.71428571428571px 1mm = 0.1cm 1mm = 1.0mm 1mm = 0.03937007874015748in 1mm = 2.8346456692913384pt 1mm = 0.23622047244094485pc 1mm = 3.571428571428571px 1in = 2.54cm 1in = 25.4mm 1in = 1.0in 1in = 72.0pt 1in = 6.0pc 1in = 90.71428571428571px 1pt = 0.035277777777777776cm 1pt = 0.35277777777777775mm 1pt = 0.013888888888888888in 1pt = 1.0pt 1pt = 0.08333333333333333pc 1pt = 1.2599206349206347px 1pc = 0.42333333333333334cm 1pc = 4.233333333333333mm 1pc = 0.16666666666666666in 1pc = 12.0pt 1pc = 1.0pc 1pc = 15.119047619047619px 1px = 0.028000000000000004cm 1px = 0.28mm 1px = 0.011023622047244094in 1px = 0.7937007874015748pt 1px = 0.06614173228346457pc 1px = 1.0px