mardi 9 juin 2015

Is this Java Enumeration Used/Designed Correctly?

I was tasked with converting some Java code to C# and came across the following enumeration (I had to generalize it, because the code is proprietary, sorry). The general purpose of the code is to convert from a "non-base unit" to a "base unit" (like converting from say kilometers to millimeters, or whatever. They have a ton of conversions). The only things I changed were the variable names. The pattern is exactly like this in the code...

public enum ThisIsAnEnum implements someInterface
{        
    typeKilom( MetricType.kilometer ),

    typeMillm( MetricType.millimeter );

    public double convertSomething(double dbl1, double dbl2)
    {
        // do stuff
        return a double
    }
}

This is then called in the code as follows:

public static void main( String[] args )
{
    ThisIsAnEnum.typeKilom.convertSomething(aDouble, bDouble);
}

I have a couple questions:

  • Is this use of enumerations a good practice in Java?
  • If yes or if no, what approach should I take in C#? Can you do something similar? Even if I can, I'm not sure that this approach is correct.

I'm not asking for someone to convert this for me... just whether this is a good approach and (if so) should I try to do the same thing in C#. If it's not, then what approach should be taken?

Aucun commentaire:

Enregistrer un commentaire