TimurMahammadov / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

ImmutableEnumMap, Builder and Exhaustive Builder #91

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Effective Java's 'Item 33: Use EnumMap instead of ordinal indexing' suggests 
the use of EnumMap for associating data with enums.  
Often times the data in such maps doesn't change and in the spirit of 'Item 15: 
Minimize mutability', an ImmutableEnumMap would 
be ideal.

To go with an ImmutableEnumMap, an ImmutableEnumMap.Builder like 
ImmutableMap.Builder would be great.

Finally, it is often the case that you want to ensure that there is a value 
associated with every value in an enum.  A builder that 
ensures that all enum values are keys would be useful because in the following 
example, if you intend to have a Bar associated with 
every value of Foo, the exhaustive builder would always fail at 1 while the 
standard builder would only fail at two with values of 
someFooEnum that are not keys in the map.

ImmutableEnumMap<FooEnum, Bar> enumMap = 
ImmutableEnumMap.exhaustiveBuilder().put(foo1, bar1)...build(); //1
...
Bar value = checkNotNull(enumMap.get(someFooEnum));  //2

Original issue reported on code.google.com by gk5...@gmail.com on 15 Jul 2008 at 4:38

GoogleCodeExporter commented 9 years ago
I agree with all this in concept.

In practice it could be ugly.  Either we return an unmodifiable wrapper of a 
plain
EnumMap, or we'll have to re-engineer portions of EnumMap.

We might not do this because I think Josh plans to support immutable EnumMaps 
in JDK
7.  If he does, I will suggest that he add the optional exhaustive check.

Original comment by kevin...@gmail.com on 17 Mar 2009 at 5:30

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 13 Aug 2009 at 2:52

GoogleCodeExporter commented 9 years ago
For this ImmutableEnumMap and its exhaustive Builder I suggest a support method
somewhere, that takes as an argument a Function<K extends Enum<K>, V> to 
compute the
values of the ImmutableEnumMap during creation. Suppose something like this:

ImmutableEnumMap.<MyEnum, Value>builder()
.forEnum(MyEnum.class) // makes it exhaustive
.computeValues(new Function<MyEnum, Value>() { ... })
.build();

or maybe:

ImmutableEnumMap.<MyEnum, Value> create(MyEnum.class, new Function<MyEnum, 
Value>()
{...});

Original comment by Robin...@gmail.com on 13 Aug 2009 at 5:07

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:45

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 17 Sep 2009 at 5:57

GoogleCodeExporter commented 9 years ago
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09