Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 542
  • Last Modified:

Java Generics Implementation.

I have an interface which is like this

public interface OXTOrderDAO< T1 extends Map<String, T2>, T2 extends Map<String, BigInteger> >  {
      public T1 getWorkingOXTOrders() throws Exception;
}

Implemented like this:

public class OXTOrderDAOImpl< T1 extends Map<String, T2>, T2 extends Map<String, BigInteger> > extends JdbcDaoSupport implements OXTOrderDAO <T1, T2>  {
   
      public T1 getWorkingOXTOrders() {
            return (T1) getJdbcTemplate().query(SELECT_ORDERS, new ResultSetExtractor() {

                  public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                        T1 map = (T1) new HashMap();
                        
                        while (rs.next()) {
                              T2 values = (T2) new HashMap();
                              values.put(PENDING, new BigInteger(rs.getString(PENDING)));
                              map.put(rs.getString(ORDER_ID), values);
                        }
                        
                        return map;
                  }
                  
            });
      }
      
      
}


This is giving me lot of warnings. Please suggest the right way to implement the generics interface
0
srikanthrad
Asked:
srikanthrad
3 Solutions
 
ozlevanonCommented:
Are you sure you need generics for this? The only reason you would need generics is if you want different implementations of your code to create OXTOrderDAOImpl using various types of Map, AND, be aware of the specific type of map, so that you can call specific methods of that implementation.
Usually, when using a map you don't want to do that, and would rather define the interface as a plain Map, so that whoever calls your method only knows they're getting a Map back.

This is why I don't think you need generics, and the code should look like below
public interface OXTOrderDAO
{
	public Map<String, Map<String, BigInteger>> getWorkingOXTOrders() throws Exception;
}


public class OXTOrderDAOImpl implements OXTOrderDAO
{
	public Map<String, Map<String, BigInteger>> getWorkingOXTOrders() throws Exception
	{
		 return (Map<String, Map<String, BigInteger>>) getJdbcTemplate().query(SELECT_ORDERS, new ResultSetExtractor() {

                  public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                        Map<String, Map<String, BigInteger>> map = new HashMap<String, Map<String, BigInteger>>();
                        
                        while (rs.next()) {
                              Map<String, BigInteger> values = new HashMap<String, BigInteger>();
                              values.put(PENDING, new BigInteger(rs.getString(PENDING)));
                              map.put(rs.getString(ORDER_ID), values);
                        }
                        
                        return map;
                  }
                  
            });
	}
}

Open in new window

0
 
srikanthradAuthor Commented:
When returning Map it is giving the following warning: Unchecked cast from Object to Map<String,Map<String,BigInteger>> I want the program to be devoid of any warnings. Is it possible?
0
 
CEHJCommented:
Sometimes it's not possible to avoid warnings. Try using the annotation

@SuppressWarnings( "unchecked" )

if so


0
 
Roger NeymanRetired Software Engineer, Author, TeacherCommented:
In order to minimize the scope of the warnings suppression, I tend to isolate unsupressible warnings (like the instantiation of generic classes, or the unchecked cast above) in its own (often one-line)  method and add the annotation there.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now