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 void close() {
47        super.close();
48        mCursor.close();
49    }
50
51    @Override
52    public boolean onMove(int oldPosition, int newPosition) {
53        return mCursor.moveToPosition(mPositionMap[newPosition]);
54    }
55
56    @Override
57    public String[] getColumnNames() {
58        return mCursor.getColumnNames();
59    }
60
61    @Override
62    public int getCount() {
63        return mCursor.getCount();
64    }
65
66    @Override
67    public double getDouble(int column) {
68        return mCursor.getDouble(column);
69    }
70
71    @Override
72    public float getFloat(int column) {
73        return mCursor.getFloat(column);
74    }
75
76    @Override
77    public int getInt(int column) {
78        return mCursor.getInt(column);
79    }
80
81    @Override
82    public long getLong(int column) {
83        return mCursor.getLong(column);
84    }
85
86    @Override
87    public short getShort(int column) {
88        return mCursor.getShort(column);
89    }
90
91    @Override
92    public String getString(int column) {
93        return mCursor.getString(column);
94    }
95
96    @Override
97    public int getType(int column) {
98        return mCursor.getType(column);
99    }
100
101    @Override
102    public boolean isNull(int column) {
103        return mCursor.isNull(column);
104    }
105}