### Question:
FluentCursor extends CrossProcessCursorWrapper { public <TKey, TValue> LinkedHashMap<TKey, TValue> toMap(Function<? super Cursor, TKey> keyTransform, Function<? super Cursor, TValue> valueTransform) { try { LinkedHashMap<TKey, TValue> result = new LinkedHashMap<>(getCount(), 1); for (moveToFirst(); !isAfterLast(); moveToNext()) { final TKey key = keyTransform.apply(this); final TValue value = valueTransform.apply(this); final TValue previousValue = result.put(key, value); Preconditions.checkArgument(previousValue == null, "Duplicate key %s found on position %s", key, getPosition()); } return result; } finally { close(); } } FluentCursor(Cursor cursor); FluentIterable<T> toFluentIterable(Function<? super Cursor, T> singleRowTransform); LazyCursorList<T> toLazyCursorList(Function<? super Cursor, T> singleRowTransform); LinkedHashMultimap<TKey, TValue> toMultimap(Function<? super Cursor, TKey> keyTransform, Function<? super Cursor, TValue> valueTransform); LinkedHashMap<TKey, TValue> toMap(Function<? super Cursor, TKey> keyTransform, Function<? super Cursor, TValue> valueTransform); T toOnlyElement(Function<? super Cursor, T> singleRowTransform); T toOnlyElement(Function<? super Cursor, T> singleRowTransform, T defaultValue); int toRowCount(); FluentCursor withNotificationUri(ContentResolver resolver, Uri uri);  }### Answer:
@Test public void shouldSuccessfullyTransformToMap() throws Exception { final MatrixCursor cursor = buildMatrixCursor(10); final FluentCursor fluentCursor = new FluentCursor(cursor); final Map<Integer, Long> transformed = fluentCursor.toMap( SingleRowTransforms.getColumn(OTHER_COLUMN).asInteger(), SingleRowTransforms.getColumn(TEST_COLUMN).asLong()); assertThat(transformed.size()).isEqualTo(cursor.getCount()); assertThat(transformed.keySet()).containsExactly(ContiguousSet.create(Range.closed(0, 9), DiscreteDomain.integers()).toArray()); assertThat(transformed.values()).containsExactlyElementsIn(Collections.nCopies(10, 18L)); }
@Test(expected = IllegalArgumentException.class) public void shouldFailIfCursorTransformedToMapContainsDuplicateKey() throws Exception { final MatrixCursor cursor = new MatrixCursor(new String[] { OTHER_COLUMN, TEST_COLUMN }); cursor.addRow(new Object[] { 10, 18L }); cursor.addRow(new Object[] { 10, 18L }); final FluentCursor fluentCursor = new FluentCursor(cursor); fluentCursor.toMap( SingleRowTransforms.getColumn(OTHER_COLUMN).asInteger(), SingleRowTransforms.getColumn(TEST_COLUMN).asLong()); }
@Test public void shouldTransformToMapWithTheSameIterationOrderAsCursorRows() throws Exception { final MatrixCursor cursor = buildMatrixCursor(3); final FluentCursor fluentCursor = new FluentCursor(cursor); final Map<Integer, Long> transformed = fluentCursor.toMap( SingleRowTransforms.getColumn(OTHER_COLUMN).asInteger(), SingleRowTransforms.getColumn(TEST_COLUMN).asLong()); assertThat(Lists.newArrayList(transformed.keySet())).containsExactly(0, 1, 2); }
@Test public void shouldCloseCursorAfterItIsTransformedToMap() throws Exception { final MatrixCursor cursor = new MatrixCursor(new String[] { TEST_COLUMN }); final FluentCursor fluentCursor = new FluentCursor(cursor); fluentCursor.toMap(Functions.constant(null), Functions.constant(null)); assertThat(fluentCursor.isClosed()).isTrue(); }
@Test public void shouldAlwaysCloseCursorAfterCallingToMap() throws Exception { final FluentCursor fluentCursor = new FluentCursor(buildMatrixCursor(10)); try { fluentCursor.toMap(KABOOM, KABOOM); } catch (Throwable t) { } assertThat(fluentCursor.isClosed()).isTrue(); }