/**
   * Returns an entity in the given revision for the given entity-id.
   *
   * @param revisionNumber
   * @param id
   * @param reader
   * @return
   */
  @SuppressWarnings("unchecked")
  private Revision<N, T> getEntityForRevision(N revisionNumber, ID id, AuditReader reader) {

    Class<?> type = revisionEntityInformation.getRevisionEntityClass();

    T revision = (T) reader.findRevision(type, revisionNumber);
    Object entity = reader.find(entityInformation.getJavaType(), id, revisionNumber);

    return new Revision<N, T>((RevisionMetadata<N>) getRevisionMetadata(revision), (T) entity);
  }
  /**
   * Returns the entities in the given revisions for the entitiy with the given id.
   *
   * @param revisionNumbers
   * @param id
   * @param reader
   * @return
   */
  @SuppressWarnings("unchecked")
  private Revisions<N, T> getEntitiesForRevisions(
      List<N> revisionNumbers, ID id, AuditReader reader) {

    Class<T> type = entityInformation.getJavaType();
    Map<N, T> revisions = new HashMap<N, T>(revisionNumbers.size());

    Class<?> revisionEntityClass = revisionEntityInformation.getRevisionEntityClass();
    Map<Number, Object> revisionEntities =
        (Map<Number, Object>)
            reader.findRevisions(revisionEntityClass, new HashSet<Number>(revisionNumbers));

    for (Number number : revisionNumbers) {
      revisions.put((N) number, reader.find(type, id, number));
    }

    return new Revisions<N, T>(toRevisions(revisions, revisionEntities));
  }
  /*
   * (non-Javadoc)
   * @see org.springframework.data.repository.history.RevisionRepository#findLastChangeRevision(java.io.Serializable)
   */
  @SuppressWarnings("unchecked")
  public Revision<N, T> findLastChangeRevision(ID id) {

    Class<T> type = entityInformation.getJavaType();
    AuditReader reader = AuditReaderFactory.get(entityManager);

    List<Number> revisions = reader.getRevisions(type, id);

    if (revisions.isEmpty()) {
      return null;
    }

    N latestRevision = (N) revisions.get(revisions.size() - 1);

    Class<?> revisionEntityClass = revisionEntityInformation.getRevisionEntityClass();

    Object revisionEntity = reader.findRevision(revisionEntityClass, latestRevision);
    RevisionMetadata<N> metadata = (RevisionMetadata<N>) getRevisionMetadata(revisionEntity);
    return new Revision<N, T>(metadata, reader.find(type, id, latestRevision));
  }