Suppose you're maintaining an API that was originally released years ago (before java gained enum
support) and it defines a class with enumeration values as ints:
public class VitaminType {
public static final int RETINOL = 0;
public static final int THIAMIN = 1;
public static final int RIBOFLAVIN = 2;
}
Over the years the API has evolved and gained Java 5-specific features (generified interfaces, etc). Now you're about to add a new enumeration:
public enum NutrientType {
AMINO_ACID, SATURATED_FAT, UNSATURATED_FAT, CARBOHYDRATE;
}
The 'old style' int-enum pattern has no type safety, no possibility of adding behaviour or data, etc, but it's published and in use. I'm concerned that mixing two styles of enumeration is inconsistent for users of the API.
I see three possible approaches:
Give up and define the new enum (
NutrientType
in my fictitious example) as a series of ints like theVitaminType
class. You get consistency but you're not taking advantage of type safety and other modern features.Decide to live with an inconsistency in a published API: keep
VitaminType
around as is, and addNutrientType
as anenum
. Methods that take aVitaminType
are still declared as taking an int, methods that take aNutrientType
are declared as taking such.Deprecate the
VitaminType
class and introduce a newVitaminType2
enum. Define the newNutrientType
as an enum.
Congratulations, for the next 2-3 years until you can kill the deprecated type, you're going to deal with deprecated versions of every single method that took aVitaminType
as an int and adding a newfoo(VitaminType2 v)
version of each. You also need to write tests for each deprecatedfoo(int v)
method as well as its correspondingfoo(VitaminType2 v)
method, so you just multiplied your QA effort.
What is the best approach?