javaextensions/midprms_db/javasrc/com/nokia/mj/impl/rms/RecordIdCache.java
author hgs
Thu, 02 Sep 2010 13:22:59 +0300
changeset 67 63b81d807542
parent 21 2a9601315dfc
child 83 26b2b12093af
permissions -rw-r--r--
v2.2.13_1

/*
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: RecordIdCache
*
*/


package com.nokia.mj.impl.rms;
import javax.microedition.rms.*;

/**
 * RecordIdCache saves all record id's in the store, which is used by
 * record enumerations.
 */
class RecordIdCache implements RecordListener
{
    private int[] iRecordIds;

    public RecordIdCache(Rms aRms)
    {
        iRecordIds = new int[0];
        // cache listener must be the first RecordListener registered
        // so that the other listeners get up-to-date information
        aRms.addRecordListener(this);
    }

    public int[] getRecordIds()
    {
        if (iRecordIds.length == 0)
        {
            return null;
        }
        return iRecordIds;
    }

    public void saveRecordIds(int[] aRecordIds)
    {
        if (aRecordIds == null)
        {
            iRecordIds = new int[0];
        }
        else
        {
            iRecordIds = aRecordIds;
        }
    }

    public void recordAdded(RecordStore aRecordStore, int aRecordId)
    {
        removeRecordId(aRecordId);
        insertRecordId(aRecordId);
    }

    public void recordChanged(RecordStore aRecordStore, int aRecordId)
    {
    }

    public void recordDeleted(RecordStore aRecordStore, int aRecordId)
    {
        removeRecordId(aRecordId);
    }

    private void removeRecordId(int aRecordId)
    {
        for (int i = 0; i < iRecordIds.length; i++)
        {
            if (aRecordId == iRecordIds[i])
            {
                int[] temp = new int[iRecordIds.length - 1];
                System.arraycopy(iRecordIds, 0, temp, 0, i);
                System.arraycopy(iRecordIds, i+1, temp, i, iRecordIds.length - i - 1);
                iRecordIds = temp;
            }
        }
    }

    private void insertRecordId(int aRecordId)
    {
        int[] temp = new int[iRecordIds.length + 1];
        temp[iRecordIds.length] = aRecordId;
        System.arraycopy(iRecordIds, 0, temp, 0, iRecordIds.length);
        iRecordIds = temp;
    }

}