Why was FormItem.setValueMap() Changed from Map to LinkedHashMap?

With version 1.0b1 FormItem.setValueMap() accepted a generic Map, but now is looking explicitly for a LinkedHashMap. It would seem to me that unless the implementation is exploiting something unique in LinkedHashMap outside of the Map API, this would make sense otherwise should be generic.

Can anyone shed some light?

Thanks,

 Kevin Hardiman

To maintain the order in which the items are added to the map.

I strongly recommend keeping the API consistent by having another method signature for Map<String, String>(). Easy enough to create a LinkedHashMap with new LinkedHashMap<String, String>(otherMapImpl).

 Kevin Hardiman

Hi Kevin,
While in most cases having Map in a method signature makes sense, it is not the case with setValueMap(). Such maps need to preserve the order in which the user specifies entires and this contract is specifically met by LinkedHashMap. If the signature accepted Map as before, the user could pass in an instance of HashMap but the order of entries specified by the user would not be preserved and appear to be random when they show up in a select list, or as radio group options. This does not make sense under any circumstance for the intended use of setValueMap. Therefore from an API perspecive LinkedHashMap is the right type for this method.

Hope this clarifies,
Sanjiv

Hello Sanjiv,

We had a discussion about this method change and while what you say is true, there could be other Map implementations other than LinkedHashMap that could preserve the order.

It would probably be better to use the interface Map and specify in the Javadoc that to preserve the order that an ordered Map implementation be used such as LinkedHashMap.

We have utilities that do this exact thing and return the Map as a LinkedHashMap. Changing back to Map would not break peoples code either. Order could be a preference rather than a mandate.

EnumUtils.java
	public static <E extends Enum<E>> Map<String,String> createNameToStringMap(Class<E> clazz){
		
		Map<String,String> map = CollectionsUtils.newLinkedHashMap();
		for(Enum<?> e:EnumSet.allOf(clazz))
			map.put(e.name(), e.toString());
		
		return map;
		
	}
CollectionsUtils.java
   public static<K,V> LinkedHashMap<K,V> newLinkedHashMap(){
       return new LinkedHashMap<K,V>();
   }

selectItem.setValueMap(EnumUtil.createNameToStringMap(SomeEnum.class));

Thanks,
Eric