How to implement a dynamic temperature conversion method based on given enums? I am making some basic temperature conversion methods, and I want the end result to look something like this: 

double celsius = TemperatureConvert.convert(10, Temperature.FAHRENHEIT, Temperature.CELSIUS);
// Where 'Temperature' is an enum with three values: FAHRENHEIT, CELSIUS, and KELVIN
// This mehtod is read logically as "convert 10 degrees fahrenheit to celsius"


Originally, I thought that the best approach was to have some private methods inside TemperatureConvert to do the conversion for me. I quickly discovered I was wrong. I want to make this method dynamic, so I used my Google Fu and found this answer. If you don't want to read it, the answers basically tell OP about the ability to have an abstract method in the enum class, and then have each enum override it.

I am having some trouble figuring out the best approach. Here are the two possibilities I came up with:


Create two abstract methods, convertA() and convertB(). Have convertA() convert to the unit that is first in alphabetical order, and have convertB() convert the other unit. In the TemperatureConvert.convert() method, I can figure out what method to use by comparing the enum name. This also brings up an issue if the same unit is passed into the convert() method, as there is no method to do any conversion. However, no conversion is needed so that might not be a problem.
Create three abstract methods, convertToCelsius(), convertToFahrenheit(), and convertToKelvin(). Use a switch statement to decide which method to use.
The java.util.concurrent.TimeUnit enum class provides both a generic conversion method and specific methods for each modeled unit, e.g., toHours. You could do something similar and still provide the third-party TemperatureConvert.convert method if you want.

Here's the implementation of SECONDS:

SECONDS {
    public long toNanos(long d)   { return x(d, C3/C0, MAX/(C3/C0)); }
    public long toMicros(long d)  { return x(d, C3/C1, MAX/(C3/C1)); }
    ...
    public long toHours(long d)   { return d/(C5/C3); }
    ...
    public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
},


Note that the author went for efficiency by precalculating many of the conversion constants. I wouldn't bother with that to start as it's probably premature for your needs. Your conversion methods should start simply:

CELCIUS {
    public double toFahrenheit(long t) {
        return t * 9 / 5 + 32;
    }
    public double toKelven(long t) {
        return t + 273;
    }
    public double convert(double t, TemperatureUnit u) {
        return u.toCelcius(v);
    }
}


Also, all of the conversion methods are stubs that throw an exception rather than abstract. Why? I don't know. If these weren't enums and could be extended, stub methods that fail only when called are handy for allowing optional operations--think Collection.remove(Object) which throws UnsupportedOperationException if not implemented by the concrete container.