@SuppressWarnings("unchecked") private static <K, V, C extends Collection<V>, CIMPL extends C> Map<K, C> mapToValueCollection( Iterable<V> values, String property, Class<K> keyType, Class<CIMPL> collectionType) { Map<K, C> byKey = new HashMap<K, C>(); SimplePropertyQuery query = new SimplePropertyQuery(property); for (V value : values) { K key = (K) query.invoke(value); C valuesForKey = byKey.get(key); if (valuesForKey == null) { try { valuesForKey = collectionType.newInstance(); } catch (Exception ex) { throw new EntityInstantiationException(collectionType, ex); } byKey.put(key, valuesForKey); } valuesForKey.add(value); } return byKey; }
@SuppressWarnings("unchecked") private static <K, V, C extends Collection<V>, CIMPL extends C> Map<K, C> mapToValueCollection( Iterable<V> values, String property, Class<K> keyType, Class<CIMPL> collectionType) { Map<K, C> byKey = new HashMap<K, C>(); SimplePropertyQuery query = new SimplePropertyQuery(property); for (V value : values) { K key = (K) query.invoke(value); C valuesForKey = byKey.get(key); if (valuesForKey == null) { try { valuesForKey = collectionType.newInstance(); } catch (Exception ex) { throw new EntityInstantiationException(collectionType, ex); } byKey.put(key, valuesForKey); } valuesForKey.add(value); } return byKey; }