From: <gca...@us...> - 2009-09-06 07:54:09
|
Revision: 1432 http://openutils.svn.sourceforge.net/openutils/?rev=1432&view=rev Author: gcatania Date: 2009-09-06 07:54:02 +0000 (Sun, 06 Sep 2009) Log Message: ----------- BSHD-1 - improve generics usage Modified Paths: -------------- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java Modified: trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2009-09-05 19:16:40 UTC (rev 1431) +++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2009-09-06 07:54:02 UTC (rev 1432) @@ -17,7 +17,7 @@ * @param <T> Persistence class * @param <K> Object Key */ -public interface HibernateDAO<T extends Object, K extends Serializable> +public interface HibernateDAO<T, K extends Serializable> { /** @@ -46,7 +46,7 @@ * @param criteria Additional Criterion conditions * @return a list of all instances */ - List<T> findAll(final Order[] orderProperties, List<Criterion> criteria); + List<T> findAll(final Order[] orderProperties, List< ? extends Criterion> criteria); /** * Execute a query. @@ -115,7 +115,8 @@ * @param page result page (first result is maxResults * page) * @return list of objects */ - List<T> findFiltered(final T filter, Map<String, FilterMetadata> metadata, final int maxResults, final int page); + List<T> findFiltered(final T filter, Map<String, ? extends FilterMetadata> metadata, final int maxResults, + final int page); /** * Return all objects related to the implementation of this DAO filtered using properties of the provided instance. @@ -126,8 +127,8 @@ * @param page result page (first result is maxResults * page) * @return list of objects */ - List<T> findFiltered(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata, - final int maxResults, final int page); + List<T> findFiltered(final T filter, final Order[] customOrder, + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page); /** * Return all objects related to the implementation of this DAO filtered using properties of the provided instance. @@ -139,8 +140,9 @@ * @param additionalCriteria additional criteria * @return list of objects */ - List<T> findFiltered(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata, - final int maxResults, final int page, List<Criterion> additionalCriteria); + List<T> findFiltered(final T filter, final Order[] customOrder, + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page, + List< ? extends Criterion> additionalCriteria); /** * Return properties from all objects related to the implementation of this DAO filtered using properties of the @@ -155,8 +157,8 @@ * @return list of properties from all objects */ List< ? > findFilteredProperties(final T filter, final Order[] customOrder, - final Map<String, FilterMetadata> metadata, final int maxResults, final int page, - List<Criterion> additionalCriteria, List<String> properties); + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page, + List< ? extends Criterion> additionalCriteria, List<String> properties); /** * Return all objects related to the implementation of this DAO filtered using properties of the provided instance. @@ -179,7 +181,7 @@ * @param metadata filter metadata * @return list of objects */ - List<T> findFiltered(final T filter, Map<String, FilterMetadata> metadata); + List<T> findFiltered(final T filter, Map<String, ? extends FilterMetadata> metadata); /** * Return the first object related to the implementation of this DAO filtered using properties of the provided @@ -204,7 +206,7 @@ * @param criteria additional criterion * @return first object in the collection */ - T findFilteredFirst(final T filter, final List<Criterion> criteria); + T findFilteredFirst(final T filter, final List< ? extends Criterion> criteria); /** * Used by the base DAO classes but here for your modification. Remove a persistent instance from the datastore. The Modified: trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2009-09-05 19:16:40 UTC (rev 1431) +++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2009-09-06 07:54:02 UTC (rev 1432) @@ -4,9 +4,8 @@ import it.openutils.hibernate.example.FilterMetadata; import java.io.Serializable; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -36,7 +35,7 @@ * @param <T> Persistence class * @param <K> Object Key */ -public abstract class HibernateDAOImpl<T extends Object, K extends Serializable> extends HibernateDaoSupport +public abstract class HibernateDAOImpl<T, K extends Serializable> extends HibernateDaoSupport implements HibernateDAO<T, K> { @@ -58,7 +57,7 @@ * @param referenceClass the class for the bean managed by this DAO */ @SuppressWarnings({"unchecked" }) - public HibernateDAOImpl(Class referenceClass) + public HibernateDAOImpl(final Class referenceClass) { super(); this.referenceClass = referenceClass; @@ -68,7 +67,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public List<T> find(String query) + public List<T> find(final String query) { return getHibernateTemplate().find(query); } @@ -86,19 +85,19 @@ */ public List<T> findAll(final Order[] orderProperties) { - return getThis().findAll(orderProperties, new ArrayList<Criterion>()); + return getThis().findAll(orderProperties, Collections.<Criterion> emptyList()); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") - public List<T> findAll(final Order[] orderProperties, final List<Criterion> criteria) + public List<T> findAll(final Order[] orderProperties, final List< ? extends Criterion> criteria) { return (List<T>) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { Criteria crit = ses.createCriteria(getReferenceClass()); if (null != orderProperties) @@ -124,7 +123,7 @@ /** * {@inheritDoc} */ - public List<T> find(String query, Object obj, Type type) + public List<T> find(final String query, final Object obj, final Type type) { return find(query, new Object[]{obj }, new Type[]{type }); } @@ -138,7 +137,7 @@ return (List<T>) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { // hibernate 3 return ses.createQuery(query).setParameters(obj, type).list(); @@ -150,7 +149,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public T load(K key) + public T load(final K key) { T result = (T) getHibernateTemplate().load(getReferenceClass(), key); Hibernate.initialize(result); @@ -161,7 +160,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public T loadIfAvailable(K key) + public T loadIfAvailable(final K key) { T result; try @@ -184,7 +183,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public T get(K key) + public T get(final K key) { return (T) getHibernateTemplate().get(getReferenceClass(), key); } @@ -200,7 +199,7 @@ /** * {@inheritDoc} */ - public void update(T obj) + public void update(final T obj) { getHibernateTemplate().update(obj); } @@ -214,7 +213,7 @@ return (Boolean) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { ses.delete(ses.load(getReferenceClass(), key)); return true; @@ -226,7 +225,7 @@ /** * {@inheritDoc} */ - public void refresh(T obj) + public void refresh(final T obj) { getHibernateTemplate().refresh(obj); } @@ -234,7 +233,7 @@ /** * {@inheritDoc} */ - public void evict(T obj) + public void evict(final T obj) { getHibernateTemplate().evict(obj); } @@ -248,7 +247,7 @@ return (T) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { return ses.merge(obj); } @@ -260,7 +259,7 @@ * {@inheritDoc} */ @SuppressWarnings("unchecked") - public K save(T obj) + public K save(final T obj) { return (K) getHibernateTemplate().save(obj); } @@ -281,11 +280,10 @@ return getFirstInCollection(findFiltered(filter, order, getDefaultFilterMetadata(), 1, 0)); } - /** * {@inheritDoc} */ - public T findFilteredFirst(final T filter, List<Criterion> criteria) + public T findFilteredFirst(final T filter, final List< ? extends Criterion> criteria) { return getFirstInCollection(findFiltered(filter, null, getDefaultFilterMetadata(), 1, 0, criteria)); } @@ -301,7 +299,7 @@ /** * {@inheritDoc} */ - public List<T> findFiltered(T filter, Order[] orderProperties) + public List<T> findFiltered(final T filter, final Order[] orderProperties) { return findFiltered(filter, orderProperties, getDefaultFilterMetadata(), Integer.MAX_VALUE, 0); } @@ -309,7 +307,7 @@ /** * {@inheritDoc} */ - public List<T> findFiltered(final T filter, final Map<String, FilterMetadata> metadata) + public List<T> findFiltered(final T filter, final Map<String, ? extends FilterMetadata> metadata) { return findFiltered(filter, metadata, Integer.MAX_VALUE, 0); } @@ -325,8 +323,8 @@ /** * {@inheritDoc} */ - public List<T> findFiltered(final T filter, final Map<String, FilterMetadata> metadata, final int maxResults, - final int page) + public List<T> findFiltered(final T filter, final Map<String, ? extends FilterMetadata> metadata, + final int maxResults, final int page) { return findFiltered(filter, null, metadata, maxResults, page); } @@ -334,18 +332,25 @@ /** * {@inheritDoc} */ - public List<T> findFiltered(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata, - final int maxResults, final int page) + public List<T> findFiltered(final T filter, final Order[] customOrder, + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page) { - return getThis().findFiltered(filter, customOrder, metadata, maxResults, page, new ArrayList<Criterion>()); + return getThis().findFiltered( + filter, + customOrder, + metadata, + maxResults, + page, + Collections.<Criterion> emptyList()); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") - public List<T> findFiltered(T filter, Order[] customOrder, Map<String, FilterMetadata> metadata, int maxResults, - int page, List<Criterion> additionalCriteria) + public List<T> findFiltered(final T filter, final Order[] customOrder, + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page, + final List< ? extends Criterion> additionalCriteria) { final Order[] orderProperties = customOrder != null && customOrder.length > 0 ? customOrder : this .getDefaultOrder(); @@ -365,8 +370,8 @@ * {@inheritDoc} */ public List< ? > findFilteredProperties(final T filter, final Order[] customOrder, - final Map<String, FilterMetadata> metadata, final int maxResults, final int page, - List<Criterion> additionalCriteria, List<String> properties) + final Map<String, ? extends FilterMetadata> metadata, final int maxResults, final int page, + final List< ? extends Criterion> additionalCriteria, final List<String> properties) { final Order[] orderProperties = customOrder != null && customOrder.length > 0 ? customOrder : this .getDefaultOrder(); @@ -391,7 +396,7 @@ return referenceClass; } - public void setReferenceClass(Class<T> referenceClass) + public void setReferenceClass(final Class<T> referenceClass) { this.referenceClass = referenceClass; } @@ -417,7 +422,7 @@ return (List< ? >) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { Query q = ses.getNamedQuery(name); if (maxResults != null) @@ -443,12 +448,12 @@ * @param maxResults max number of results * @return Query */ - protected List< ? > findByNamedQuery(final String name, final Map<String, Object> params, final Integer maxResults) + protected List< ? > findByNamedQuery(final String name, final Map<String, ? > params, final Integer maxResults) { return (List< ? >) getHibernateTemplate().execute(new HibernateCallback() { - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { Query q = ses.getNamedQuery(name); if (maxResults != null) @@ -458,7 +463,7 @@ if (params != null) { - for (Map.Entry<String, Object> entry : params.entrySet()) + for (Map.Entry<String, ? > entry : params.entrySet()) { setParameterValue(q, entry.getKey(), entry.getValue()); } @@ -477,7 +482,7 @@ * @deprecated use the better named <code>findByNamedQuery</code> method */ @Deprecated - protected List< ? > getNamedQuery(final String name, final Serializable[] params, int maxResults) + protected List< ? > getNamedQuery(final String name, final Serializable[] params, final int maxResults) { return findByNamedQuery(name, params, maxResults > 0 ? maxResults : Integer.MAX_VALUE); } @@ -491,7 +496,7 @@ * @deprecated use the better named <code>findByNamedQuery</code> method */ @Deprecated - protected List< ? > getNamedQuery(final String name, final Map<String, Object> params, int maxResults) + protected List< ? > getNamedQuery(final String name, final Map<String, ? > params, final int maxResults) { return findByNamedQuery(name, params, maxResults > 0 ? maxResults : Integer.MAX_VALUE); } @@ -503,7 +508,7 @@ * @param key the key name * @param value the object to set as the parameter */ - protected void setParameterValue(Query query, String key, Object value) + protected void setParameterValue(final Query query, final String key, final Object value) { if (null == key || null == value) { @@ -517,9 +522,9 @@ * set any default filter, subclasses may override this. * @return map of property name - filter metadata */ - protected Map<String, FilterMetadata> getDefaultFilterMetadata() + protected Map<String, ? extends FilterMetadata> getDefaultFilterMetadata() { - return new HashMap<String, FilterMetadata>(0); + return Collections.emptyMap(); } /** @@ -527,14 +532,13 @@ * @param list collection * @return first element in the list */ - @SuppressWarnings("unchecked") - private T getFirstInCollection(Collection<T> list) + private T getFirstInCollection(final Collection< ? extends T> list) { if (list != null && !list.isEmpty()) { - Object result = list.iterator().next(); + T result = list.iterator().next(); Hibernate.initialize(result); - return (T) result; + return result; } return null; } @@ -543,7 +547,7 @@ * Sets the aopenabled. * @param aopenabled the aopenabled to set */ - public void setAopenabled(boolean aopenabled) + public void setAopenabled(final boolean aopenabled) { this.aopenabled = aopenabled; } @@ -601,7 +605,7 @@ /** * */ - private final Map<String, FilterMetadata> metadata; + private final Map<String, ? extends FilterMetadata> metadata; /** * @@ -613,7 +617,7 @@ */ private final Order[] orderProperties; - private List<Criterion> additionalCriteria; + private List< ? extends Criterion> additionalCriteria; /** * @param filter @@ -623,13 +627,13 @@ * @param orderProperties */ private HibernateCallbackForExecution( - T filter, - int page, - int maxResults, - Map<String, FilterMetadata> metadata, - Order[] orderProperties, - List<Criterion> additionalCriteria, - List<String> properties) + final T filter, + final int page, + final int maxResults, + final Map<String, ? extends FilterMetadata> metadata, + final Order[] orderProperties, + final List< ? extends Criterion> additionalCriteria, + final List<String> properties) { this.filter = filter; this.page = page; @@ -640,7 +644,7 @@ this.properties = properties; } - public Object doInHibernate(Session ses) throws HibernateException + public Object doInHibernate(final Session ses) throws HibernateException { Criteria crit = ses.createCriteria(filter.getClass()); crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2009-09-05 19:16:40 UTC (rev 1431) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2009-09-06 07:54:02 UTC (rev 1432) @@ -5,8 +5,8 @@ import java.sql.Timestamp; import java.util.Calendar; import java.util.Collection; +import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -34,11 +34,14 @@ */ private static Logger log = LoggerFactory.getLogger(EnhancedExample.class); - private Map<String, FilterMetadata> metadata; + private Map<String, ? extends FilterMetadata> metadata; - private EnhancedExample(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) + private EnhancedExample( + final Criteria crit, + final Object filter, + final Map<String, ? extends FilterMetadata> metadata) { - this.metadata = metadata == null ? new HashMap<String, FilterMetadata>(0) : metadata; + this.metadata = metadata == null ? Collections.<String, FilterMetadata> emptyMap() : metadata; fillCriteria(null, crit, filter); } @@ -49,8 +52,8 @@ * @param metadata Map of property names - filter metadata * @throws HibernateException exception while building the criteria */ - public static void create(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) - throws HibernateException + public static void create(final Criteria crit, final Object filter, + final Map<String, ? extends FilterMetadata> metadata) throws HibernateException { new EnhancedExample(crit, filter, metadata); } @@ -62,8 +65,8 @@ * @param value property value * @throws HibernateException exception while building the criteria */ - private void addCondition(Criteria crit, String propertyName, Object value, Object parentObject) - throws HibernateException + private void addCondition(final Criteria crit, final String propertyName, final Object value, + final Object parentObject) throws HibernateException { String simplePropertyName = StringUtils.contains(propertyName, ".") ? StringUtils.substringAfterLast( @@ -95,9 +98,9 @@ { // @todo handle multiple associations in lists? // see http://opensource2.atlassian.com/projects/hibernate/browse/HHH-879 - if ((value instanceof Set || value instanceof List) && !((Collection< ? >) value).isEmpty()) + if ((value instanceof Set< ? > || value instanceof List< ? >) && !((Collection< ? >) value).isEmpty()) { - // collection: the new criteria has already been created, now we only nee to analize content + // collection: the new criteria has already been created, now we only need to analize content for (Object element : ((Collection< ? >) value)) { @@ -107,7 +110,7 @@ fillCriteria(propertyName, childrenCriteria, element); } } - else if ((value instanceof Map) && !((Map< ? , ? >) value).isEmpty()) + else if ((value instanceof Map< ? , ? >) && !((Map< ? , ? >) value).isEmpty()) { FilterMetadata fmd = metadata.get(propertyName); @@ -139,7 +142,7 @@ * @return <code>true</code> if the bean contains at least a valid property */ @SuppressWarnings("unchecked") - private boolean containsSomething(Object bean) + private boolean containsSomething(final Object bean) { if (bean == null) @@ -151,7 +154,7 @@ return true; } - if (bean instanceof Collection) + if (bean instanceof Collection< ? >) { Collection< ? > coll = ((Collection< ? >) bean); @@ -165,7 +168,7 @@ return true; } } - else if (bean instanceof Map) + else if (bean instanceof Map< ? , ? >) { Map< ? , ? > coll = ((Map< ? , ? >) bean); if (coll.isEmpty()) @@ -227,9 +230,10 @@ * @throws HibernateException exception while building the criteria */ @SuppressWarnings("unchecked") - private void fillCriteria(String parentPropertyName, Criteria crit, Object filter) throws HibernateException + private void fillCriteria(final String parentPropertyName, final Criteria crit, final Object filter) + throws HibernateException { - if ((filter instanceof Set || filter instanceof List) && !((Collection< ? >) filter).isEmpty()) + if ((filter instanceof Set< ? > || filter instanceof List< ? >) && !((Collection< ? >) filter).isEmpty()) { // collection: the new criteria has already been created, now we only need to analize content for (Object element : ((Collection< ? >) filter)) @@ -281,7 +285,7 @@ * @param object object to check * @return <code>true</code>if the given object is a simple type */ - private boolean isSimpleType(Object object) + private boolean isSimpleType(final Object object) { Class< ? extends Object> objClass = object.getClass(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |