@SuppressWarnings("unchecked")
	@Override
	public <K> Map<K, E> toResultMap(ResultSet resultSet, boolean sorted) throws ResultMapException {
		Map<K,E> resultMap = null;
		if( sorted ) {
			resultMap = new TreeMap<K,E>();
		} else {
			resultMap = new HashMap<K,E>();
		}
		if(resultSet == null) {
			return resultMap;
		}
		try {
			if(resultSet.isClosed()) {
				return resultMap;
			}
		} catch (SQLException e) {
			logger.debug("Failed while checking whether the ResultSet is closed",e);
			return resultMap;
		}
		logger.debug("Searching " + this.classInspector.getInspectedClass().getCanonicalName() + " for fields that are marked with @Key annotation");
		Field keyField = classInspector.findFieldWithAnnotation(Key.class);
		if(keyField == null) {
			throw new ResultMapException("Could not find a field annotated with the @Key annotation in " + classInspector.getInspectedClass().getName());
		}
		logger.debug("Field<" + keyField.getName() + " is marked with the @Key");
		Method keyGetMethod = classInspector.findGetMethod(keyField);
		logger.debug("Using get method<" + keyGetMethod.getName() + "> to get value of Key");
		try {
			while(resultSet.next()) {
				E instance = toResult(resultSet);
				K key = (K) classInspector.executeGetMethod(instance, keyGetMethod);
				logger.debug("Inserting mapped result with key<" + key + " into map");
				resultMap.put(key ,instance);
			}
		} catch (SQLException e) {
			throw new ResultMapException("Failed to map an instance while traversing the ResultSet",e);
		}
		return resultMap;
	}