ReorderingCursorWrapper.java revision 28f8857b1b46bde18b85c6d3c2a63ac44c3c2e1c
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16package com.android.providers.contacts;
17
18import android.database.AbstractCursor;
19import android.database.Cursor;
20
21/**
22 * Cursor wrapper that reorders rows according to supplied specific position mapping.
23 */
24public class ReorderingCursorWrapper extends AbstractCursor {
25
26    private final Cursor mCursor;
27    private final int[] mPositionMap;
28
29    /**
30     * Constructor.
31     *
32     * @param cursor wrapped cursor
33     * @param positionMap maps wrapper cursor positions to wrapped cursor positions
34     *            so that positionMap[wrapperPosition] == wrappedPosition
35     */
36    public ReorderingCursorWrapper(Cursor cursor, int[] positionMap) {
37        if (cursor.getCount() != positionMap.length) {
38            throw new IllegalArgumentException("Cursor and position map have different sizes.");
39        }
40
41        mCursor = cursor;
42        mPositionMap = positionMap;
43    }
44
45    @Override
46    public boolean onMove(int oldPosition, int newPosition) {
47        return mCursor.moveToPosition(mPositionMap[newPosition]);
48    }
49
50    @Override
51    public String[] getColumnNames() {
52        return mCursor.getColumnNames();
53    }
54
55    @Override
56    public int getCount() {
57        return mCursor.getCount();
58    }
59
60    @Override
61    public double getDouble(int column) {
62        return mCursor.getDouble(column);
63    }
64
65    @Override
66    public float getFloat(int column) {
67        return mCursor.getFloat(column);
68    }
69
70    @Override
71    public int getInt(int column) {
72        return mCursor.getInt(column);
73    }
74
75    @Override
76    public long getLong(int column) {
77        return mCursor.getLong(column);
78    }
79
80    @Override
81    public short getShort(int column) {
82        return mCursor.getShort(column);
83    }
84
85    @Override
86    public String getString(int column) {
87        return mCursor.getString(column);
88    }
89
90    @Override
91    public boolean isNull(int column) {
92        return mCursor.isNull(column);
93    }
94}