Please rethink extended enums inclusion

Jose Antonio Illescas Del Olmo jantonio.illescas at rbcdexia-is.es
Fri Oct 7 07:09:01 PDT 2011


Hello Kai,

  See this as "generic" example, on my project exist more duplicate code 
on related enums that can not refactorize into superclass (by java 
limitations)

     Your option is good (considering java limitations) but is not clean 
from a object-oriented view...

Thank you, Kai




On 07/10/2011 15:48, Kai Kunstmann wrote:
> Hi,
>
> I think, options are limitless, if you out-factor everything into a
> helper class with some static magic:
>
>
> public class Enums
> {
>    private final static Cache CACHE = new Cache();
>
>    private static class Cache
>    {
>      ...
>    }
>
>    public static<E extends Enum<?>>  void register(E const, Object code)
>    {
>      CACHE.put(const, code);
>    }
>
>    public static<E extends Enum<?>>  E get(Class<E>  type, Object code)
>    {
>      return CACHE.get(type, code);
>    }
>
>    // also, have a look at java.util.EnumSet
>    public static boolean in(E needle, E ... haystack)
>    {
>      ...
>    }
> }
>
>
> public enum Sample
> {
>    ONE("one"), TWO("two"), TEN("ten");
>
>    private Sample(String code)
>    {
>      Enums.register(this, code);
>    }
> }
>
>
> public class Client
> {
>    public void doSomething(Object code)
>    {
>      Enums.get(Sample.class, code);
>    }
> }
>
>
> You can probably adopt the example to your needs. Just make sure you
> don't get any race conditions or memory leaks in the Cache code.
>
> By the way, since the registration() code should be executed rarely, you
> can make it expensive and check for what ever conditions you like (e.g.
> call-stack analyses). You can also force a certain interface upon the
> cachable enums or their code type by simply defining it:
>
>
> public static<E extends Enum<E>  &  Special>  void put(E const, Code code)
>
>
> The caveat is, not to register() your constants results in a runtime
> exception on the client side rather than a compile-time exception. This
> can happen, if you simply forget to put the register() call in your
> constructor code, or if you try to access a constant in a fancy way
> before the corresponding enum class is initialized (which usually
> happens the first time you "mention" the class).
>
>
> Cheers,
> Kai
>
>
>
> Am Freitag, den 07.10.2011, 13:34 +0200 schrieb Jose Antonio Illescas
> Del Olmo:
>> Now we have hundred of enums on our project (finantial application with
>> 120.000 lines) an repeat same code on any enum... (*mark as red*)
>>
>>
>> * Map our enums with old legacy system codes:
>>
>>      public enum Type {
>>
>>         ONE("01), TWO("03"), THREE("03"), ...;
>>
>>         private Type(String code) {
>>             setCode(code);
>>         }
>>
>> *      private String code;
>> *
>> *    public String code() {
>>             return code;
>>         }
>>
>>         public void setCode(String code) {
>>             this.code = code;
>>         }
>>
>> **      public static Type fromCode(String code) {
>>             if (code == null) return null;
>>             for(Type type: values()) {
>>                if (type.code.equals(code) return type;
>>             }
>>             return null;
>>         }
>>
>> **      public boolean in(Type... types) {  // IMO all enums must have
>> this method
>>             if (types == null) return false;
>>             for(Type type: types) {
>>                if (this == type) return true;
>>             }
>>             return false;
>>         }
>> *
>> Or with cache to avoid for iteration on fromCode(String) method =>  more
>> code is necesary
>>
>> public enum Type {
>>
>>         ONE("01), TWO("03"), THREE("03"), ...;
>>
>>         private Type(String code) {
>>             setCode(code);
>>         }
>>
>> *      private String code;
>>
>> **      private static Map<String, Type>  cache= new HashMap<String,Type>();
>>
>>         static {
>>             for(Type type: Type.values()) {
>>                cache.put(type.code(), type);
>>             }
>>         }
>> *
>> *    public String code() {
>>             return code;
>>         }
>>
>>         public void setCode(String code) {
>>             this.code = code;
>>         }
>>
>> **      public static Type fromCode(String code) {
>>             return cache.get(code);
>>         }
>>
>> **      public boolean in(Type... types) {  // IMO all enums must have
>> this method
>>             if (types == null) return false;
>>             for(Type type: types) {
>>                if (this == type) return true;
>>             }
>>             return false;
>>         }*
>>
>> /Abstract enums with Generic support reduce dramatically the code of
>> enums, see next code that use abstract enums:
>> /
>> *public enum Type extends MyAbstractEnum<String>  {
>>
>>         ONE("01), TWO("03"), THREE("03"), ...;
>>
>>         private Type(String code) {
>>             super(code);
>>         }
>>     }*
>>
>>
>
>
>




More information about the coin-dev mailing list