public synchronized <V> V last(EntityCursor<V> cursor) {
   try {
     return cursor.last();
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
  private <T> void checkCursor(
      EntityCursor<T> cursor,
      Collection<T> collection,
      boolean collectionIsKeySet,
      List<List<Integer>> expected,
      Getter<T> getter)
      throws DatabaseException {

    boolean first;
    boolean firstDup;
    Iterator<T> iterator = collection.iterator();

    for (List<Integer> dups : expected) {
      for (int i : dups) {
        T o = cursor.next();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        /* Value iterator over duplicates. */
        if (!collectionIsKeySet) {
          assertTrue(iterator.hasNext());
          o = iterator.next();
          assertNotNull(o);
          assertEquals(i, getter.getKey(o));
        }
      }
    }

    first = true;
    for (List<Integer> dups : expected) {
      firstDup = true;
      for (int i : dups) {
        T o = first ? cursor.first() : (firstDup ? cursor.next() : cursor.nextDup());
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
        firstDup = false;
      }
    }

    first = true;
    for (List<Integer> dups : expected) {
      if (!dups.isEmpty()) {
        int i = dups.get(0);
        T o = first ? cursor.first() : cursor.nextNoDup();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        /* Key iterator over non-duplicates. */
        if (collectionIsKeySet) {
          assertTrue(iterator.hasNext());
          o = iterator.next();
          assertNotNull(o);
          assertEquals(i, getter.getKey(o));
        }
        first = false;
      }
    }

    List<List<Integer>> reversed = new ArrayList<List<Integer>>();
    for (List<Integer> dups : expected) {
      ArrayList<Integer> reversedDups = new ArrayList<Integer>(dups);
      Collections.reverse(reversedDups);
      reversed.add(reversedDups);
    }
    Collections.reverse(reversed);

    first = true;
    for (List<Integer> dups : reversed) {
      for (int i : dups) {
        T o = first ? cursor.last() : cursor.prev();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
      }
    }

    first = true;
    for (List<Integer> dups : reversed) {
      firstDup = true;
      for (int i : dups) {
        T o = first ? cursor.last() : (firstDup ? cursor.prev() : cursor.prevDup());
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
        firstDup = false;
      }
    }

    first = true;
    for (List<Integer> dups : reversed) {
      if (!dups.isEmpty()) {
        int i = dups.get(0);
        T o = first ? cursor.last() : cursor.prevNoDup();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
      }
    }

    cursor.close();
  }