|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: RecordIdCache |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.mj.impl.rms; |
|
20 import javax.microedition.rms.*; |
|
21 |
|
22 /** |
|
23 * RecordIdCache saves all record id's in the store, which is used by |
|
24 * record enumerations. |
|
25 */ |
|
26 class RecordIdCache implements RecordListener |
|
27 { |
|
28 private int[] iRecordIds; |
|
29 |
|
30 public RecordIdCache(Rms aRms) |
|
31 { |
|
32 iRecordIds = new int[0]; |
|
33 // cache listener must be the first RecordListener registered |
|
34 // so that the other listeners get up-to-date information |
|
35 aRms.addRecordListener(this); |
|
36 } |
|
37 |
|
38 public int[] getRecordIds() |
|
39 { |
|
40 if (iRecordIds.length == 0) |
|
41 { |
|
42 return null; |
|
43 } |
|
44 return iRecordIds; |
|
45 } |
|
46 |
|
47 public void saveRecordIds(int[] aRecordIds) |
|
48 { |
|
49 if (aRecordIds == null) |
|
50 { |
|
51 iRecordIds = new int[0]; |
|
52 } |
|
53 else |
|
54 { |
|
55 iRecordIds = aRecordIds; |
|
56 } |
|
57 } |
|
58 |
|
59 public void recordAdded(RecordStore aRecordStore, int aRecordId) |
|
60 { |
|
61 removeRecordId(aRecordId); |
|
62 insertRecordId(aRecordId); |
|
63 } |
|
64 |
|
65 public void recordChanged(RecordStore aRecordStore, int aRecordId) |
|
66 { |
|
67 } |
|
68 |
|
69 public void recordDeleted(RecordStore aRecordStore, int aRecordId) |
|
70 { |
|
71 removeRecordId(aRecordId); |
|
72 } |
|
73 |
|
74 private void removeRecordId(int aRecordId) |
|
75 { |
|
76 for (int i = 0; i < iRecordIds.length; i++) |
|
77 { |
|
78 if (aRecordId == iRecordIds[i]) |
|
79 { |
|
80 int[] temp = new int[iRecordIds.length - 1]; |
|
81 System.arraycopy(iRecordIds, 0, temp, 0, i); |
|
82 System.arraycopy(iRecordIds, i+1, temp, i, iRecordIds.length - i - 1); |
|
83 iRecordIds = temp; |
|
84 } |
|
85 } |
|
86 } |
|
87 |
|
88 private void insertRecordId(int aRecordId) |
|
89 { |
|
90 int[] temp = new int[iRecordIds.length + 1]; |
|
91 temp[0] = aRecordId; |
|
92 System.arraycopy(iRecordIds, 0, temp, 1, iRecordIds.length); |
|
93 iRecordIds = temp; |
|
94 } |
|
95 |
|
96 } |
|
97 |