You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Dipanjan Laha (JIRA)" <ji...@apache.org> on 2014/03/02 18:04:31 UTC

[jira] [Comment Edited] (COLLECTIONS-508) MultiMap's methods are not strongly typed even though the interface supports generics

    [ https://issues.apache.org/jira/browse/COLLECTIONS-508?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13917486#comment-13917486 ] 

Dipanjan Laha edited comment on COLLECTIONS-508 at 3/2/14 5:03 PM:
-------------------------------------------------------------------

As per my discussions with Thomas and Matt on the commons mailing list, I have implemented the MultiValuedMap interface, the MultiValuedHashMap and a MultiValuedHashMapTest. I haven't completed the documentation yet. If the implementations look fine, I will add the remaining documentations. 

A few more points regarding the implementation

1. I have added a few methods to the MultiValuedMap interface which were not there in the MultiMap. I think they would be a good addition to the interface IMHO. They are
    boolean containsValue(Object key, Object value);
    int totalSize();
    void putAll(MultiValuedMap<? extends K, ? extends V> map);
2. I have added an AbstractMultiValuedMapDecoractor on the lines of AbstractMapDecorator, which can be extended by other MultiValuedMap implementations like say a MultiValuedTreeMap
3. I have created MultiValuedGet and MultiValuedPut to honor the Get/Put split concepts. It was not possible for MultiValuedMap to extend the Get & Put directly due to the limitations I had mentioned in my earlier mail.
4. I have marked the incomplete documentations with TODO tags. 



was (Author: dipanjan21):
As per my discussions with Thomas and Matt on the commons mailing list, I have implemented the MultiValuedMap interface, the MultiValuedHashMap and a MultiValuedHashMapTest. I haven't completed the documentation yet. If the implementations look fine, I will add the remaining documentations. 

A few more points regarding the implementation

1. I have added a few methods to the MultiValuedMap interface which were not there in the MultiMap. I think they would be a good addition to the interface IMHO. They are
    boolean containsValue(Object key, Object value);
    int totalSize();
    void putAll(MultiValuedMap<? extends K, ? extends V> map);
2. I have added an AbstractMultiValuedMapDecoractor on the lines of AbstractMapDecorator, which can be extended by other MultiValuedMap implementations like say a MultiValuedTreeMap
3. I have created MultiValuedGet and MultiValuedPut to honor the Get/Put split concepts. It was not possible for MultiValuedMap to extend the Get & Put directly due to the limitations I had mentioned in my earlier mail.
4. I have marked the incomplete documentations with TODO tags. 

Please ignore the earlier attachment

> MultiMap's methods are not strongly typed even though the interface supports generics
> -------------------------------------------------------------------------------------
>
>                 Key: COLLECTIONS-508
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-508
>             Project: Commons Collections
>          Issue Type: Improvement
>          Components: Map
>    Affects Versions: 4.0
>            Reporter: Dipanjan Laha
>         Attachments: MultiValuedMap.patch
>
>
> Recently I had the need of using a MultiMap in one of my projects. While using the same, I found that the MultiMap interface  has methods that are not strongly typed even though the interface supports generics. For example if I have a MultiMap like so
> MultiMap<String, User> multiMap = new MultiValueMap<String, User>();
> where User is a custom  Class, then the get(key) method would return me an Object which I would need to cast to a Collection like so
> Collection<User> userCol = (Collection<User>) multiMap.get(key);
> I understand that this limitation comes from that fact that the MultiMap extends IterableMap which in turn extends Map and other interfaces. Hence the MultiMap cannot have a get method which returns a Collection instead of Object as that would mean implementing IterableMap with the Generics set to be <K,Collection<V>>. In that case the put method's signature would become
> public Collection<V> put(K key, Collection<V> value); 
> which we do not want.The same problem would arise with other methods as well, ex: containsValue method. 
> My proposal is why carry on the signatures of a Map and put it on MultiMap. Where as I do agree that it is a Map after all and has very similar implementation and functionality, it is very different at other levels. And even though the MultiMap interface supports generics, the methods are not strongly typed, which defeats the purpose of having generics. So why can't we have a separate set of interfaces for MultiMap which do not extend Map. That way we can have strongly typed methods on the MultiMap.
> I have included a a patch for these changes. It is not fully complete and has some gaps in some TestCases and the documentation but gives a fairly good idea of what I am talking about. Please let me know your thoughts on taking this approach. Then i will improve the implementation and submit another patch.
> The other way could be that we let MultiMap extend the interfaces it does today, but with proper types rather than Object. I mean something like this
> public interface MultiMap<K,V> extends IterableMap<K,Collection<V>> instead of 
> public interface MultiMap<K,V> extends IterableMap<K,Object>
> And then have a separate set of methods on the MultiMap interface which supports the specific MultiMap functionality. For example, the put method with the above implementation would become 
> Collection<V>	put(K key, Collection<V> value)
> and we can have another method as 
> V putValue(K key, V value)
> This way the functionality of Map is preserved along with strongly typed MultiMap methods. If you feel that this approach is better than the earlier one, i will implement the same and submit a patch



--
This message was sent by Atlassian JIRA
(v6.2#6252)