Prev: Is there any ActiveRecord implementation for java?
Next: CFP: PPPJ 2010 -- Principles and Practice of Programming in Java (Vienna, Sep 10)
From: markspace on 19 Dec 2009 12:16 Daniel Pitts wrote: > // Exposing this as a public final field. > // One of the few times I would actually do that. > public final int columnValue; This bit here I had to comment on. The whole point of enums is that you replace int, which is a homogeneous type that appears everywhere in a program, with a specific class type, which only appears in your API where it is a valid parameter. The idea that you then goes back in the other direction and use enums to store a public integer value... ow! brain hurt bad...
From: Lew on 19 Dec 2009 16:38 Tom Anderson wrote: > You can also trick the constructor-put idea into working by moving the > map into a different class, which can even be an inner class of the enum: > > public enum SomeType { > FOO(101), > BAR(100), > GORK(4001); > > private static class ByColumnValue { > public static final Map<Integer, SomeType> MAP = new > HashMap<Integer, SomeType>(); > } > > private final int columnValue; > > private SomeType(int columnValue) { > this.columnValue = columnValue; > ByColumnValue.MAP.put(columnValue, this); > } > > public SomeType getEnum(int columnValue) { > return ByColumnValue.MAP.get(columnValue); > } > } 'getEnum()' should be static. -- Lew
From: Lew on 19 Dec 2009 16:41 Daniel Pitts wrote: >> // Exposing this as a public final field. >> // One of the few times I would actually do that. >> public final int columnValue; markspace wrote: > This bit here I had to comment on. The whole point of enums is that you > replace int, which is a homogeneous type that appears everywhere in a > program, with a specific class type, which only appears in your API > where it is a valid parameter. > > The idea that you then goes back in the other direction and use enums to > store a public integer value... ow! brain hurt bad... But it's a type-safe int! -- Lew
From: Lew on 19 Dec 2009 19:19 Tom Anderson wrote: >>> public enum SomeType { >>> FOO(101), >>> BAR(100), >>> GORK(4001); >>> >>> private static class ByColumnValue { >>> public static final Map<Integer, SomeType> MAP = new >>> HashMap<Integer, SomeType>(); >>> } >>> >>> private final int columnValue; >>> >>> private SomeType(int columnValue) { >>> this.columnValue = columnValue; >>> ByColumnValue.MAP.put(columnValue, this); >>> } >>> >>> public SomeType getEnum(int columnValue) { >>> return ByColumnValue.MAP.get(columnValue); >>> } >>> } Lew wrote: >> 'getEnum()' should be static. Tom Anderson wrote: > Whoops, you're quite right, it should. Aside: there is at least one approach to Hibernate serialization of enums where the programmer provides for reflective use the names of the instance method to convert an enum to an external value, and the static method for the other direction. (IIRC they default to 'toString()' and 'valueOf()'.) -- Lew
From: Daniel Pitts on 19 Dec 2009 23:22
Lew wrote: > Daniel Pitts wrote: >>> // Exposing this as a public final field. >>> // One of the few times I would actually do that. >>> public final int columnValue; > > markspace wrote: >> This bit here I had to comment on. The whole point of enums is that >> you replace int, which is a homogeneous type that appears everywhere >> in a program, with a specific class type, which only appears in your >> API where it is a valid parameter. >> >> The idea that you then goes back in the other direction and use enums >> to store a public integer value... ow! brain hurt bad... > > But it's a type-safe int! > Actually, the way I see it is the public integer is an *external* key. It is not controlled by the program, and therefor must be declared as a magic number somewhere. The fact that it is being converted to and from type-safe enums is at least a partial win. The best practices for this situation would be to maintain the enum value for as much of the time as possible. For better control over this "best practice", it might make more sense to make that a package protected value, and force the data-access code to do the conversions immediately after read, and prior to write. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/> |