/*
   * (non-Javadoc)
   * @see org.springframework.data.repository.query.RepositoryQuery#execute(java.lang.Object[])
   */
  @Override
  public Object execute(Object[] parameters) {
    ParametersParameterAccessor accessor =
        new ParametersParameterAccessor(method.getParameters(), parameters);

    QueryString query =
        (isUserDefinedQuery()
            ? this.query
            : this.query.forRegion(
                method.getEntityInformation().getJavaType(), template.getRegion()));

    for (Integer index : query.getInParameterIndexes()) {
      query = query.bindIn(toCollection(accessor.getBindableValue(index - 1)));
    }

    Collection<?> result = toCollection(template.find(query.toString(), parameters));

    if (method.isCollectionQuery()) {
      return result;
    } else if (method.isQueryForEntity()) {
      if (result.isEmpty()) {
        return null;
      } else if (result.size() == 1) {
        return result.iterator().next();
      } else {
        throw new IncorrectResultSizeDataAccessException(1, result.size());
      }
    } else {
      throw new IllegalStateException("Unsupported query: " + query.toString());
    }
  }