Skip to content

Commit fe25e6e

Browse files
committed
add *byIndex() methods
1 parent 5e4e26e commit fe25e6e

2 files changed

Lines changed: 146 additions & 4 deletions

File tree

java/srcPubnubApi/com/pubnub/api/SyncedObject.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.HashMap;
99
import java.util.Hashtable;
10+
import java.util.Iterator;
1011
import java.util.concurrent.atomic.AtomicBoolean;
1112

1213
public class SyncedObject {
@@ -149,6 +150,75 @@ public Object shift() {
149150
}
150151
}
151152

153+
public Object removeByIndex(Integer index) {
154+
String key = _keyByIndex(index);
155+
156+
if (_keyByIndex(index) != null) {
157+
try {
158+
Object result = syncedObjectManager.getValue(glue(location, key));
159+
remove(key);
160+
return result;
161+
} catch (JSONException e) {
162+
return null;
163+
}
164+
} else {
165+
return null;
166+
}
167+
}
168+
169+
public Object replaceByIndex(Integer index, Object data) {
170+
String key = _keyByIndex(index);
171+
172+
if (_keyByIndex(index) != null) {
173+
try {
174+
Object result = syncedObjectManager.getValue(glue(location, key));
175+
replace(key, data);
176+
return result;
177+
} catch (JSONException e) {
178+
return null;
179+
}
180+
} else {
181+
return null;
182+
}
183+
}
184+
185+
public Object getByIndex(Integer index) {
186+
String key = _keyByIndex(index);
187+
188+
if (_keyByIndex(index) != null) {
189+
try {
190+
return syncedObjectManager.getValue(glue(location, key));
191+
} catch (JSONException e) {
192+
return null;
193+
}
194+
} else {
195+
return null;
196+
}
197+
}
198+
199+
private String _keyByIndex(Integer index) {
200+
try {
201+
JSONObject value = syncedObjectManager.getRawValue(location);
202+
if (isPnList(value)) {
203+
Integer i = 0;
204+
Iterator valueIterator = value.sortedKeys();
205+
while (valueIterator.hasNext()) {
206+
if (index.equals(i)) {
207+
return (String) valueIterator.next();
208+
} else {
209+
valueIterator.next();
210+
i++;
211+
}
212+
}
213+
return null;
214+
} else {
215+
return null;
216+
}
217+
} catch (JSONException e) {
218+
return null;
219+
}
220+
}
221+
152222
public String getType() {
153223
return getType("");
154224
}

java/srcTest/com/pubnub/api/datasync/ListMethodsTest.java

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.pubnub.api.datasync;
22

3-
import com.pubnub.api.DataSyncCallback;
4-
import com.pubnub.api.Pubnub;
5-
import com.pubnub.api.SyncedObject;
6-
import com.pubnub.api.TestHelper;
3+
import com.pubnub.api.*;
74
import org.json.JSONException;
85
import org.junit.Before;
96
import org.junit.Test;
@@ -97,4 +94,79 @@ public void removeCallback(List updates, String path) {
9794
assertEquals(0, latch1.getCount());
9895
assertEquals(0, latch2.getCount());
9996
}
97+
98+
@Test
99+
public void testRemoveByIndex() throws InterruptedException, JSONException {
100+
DataSyncTestHelper.setupSettingsOn(playerString, pubnub, true);
101+
102+
final CountDownLatch latch1 = new CountDownLatch(1);
103+
final CountDownLatch latch2 = new CountDownLatch(1);
104+
105+
DataSyncCallback cb1 = new DataSyncCallback() {
106+
@Override
107+
public void readyCallback(SyncedObject syncedObject) {
108+
latch1.countDown();
109+
}
110+
111+
@Override
112+
public void removeCallback(List updates, String path) {
113+
latch2.countDown();
114+
}
115+
};
116+
117+
SyncedObject player = pubnub.sync(playerString, cb1);
118+
119+
latch1.await(5, TimeUnit.SECONDS);
120+
121+
SyncedObject tracks = player.child("tracks");
122+
assertEquals(3, tracks.getList().size());
123+
124+
String track = (String) tracks.removeByIndex(1);
125+
assertEquals("track#2", track);
126+
127+
latch2.await(5, TimeUnit.SECONDS);
128+
129+
assertEquals(2, tracks.getList().size());
130+
assertEquals("track#3", tracks.getByIndex(1));
131+
assertEquals(0, latch1.getCount());
132+
assertEquals(0, latch2.getCount());
133+
}
134+
135+
@Test
136+
public void testReplaceByIndex() throws InterruptedException, JSONException {
137+
DataSyncTestHelper.setupSettingsOn(playerString, pubnub, true);
138+
139+
final CountDownLatch latch1 = new CountDownLatch(1);
140+
final CountDownLatch latch2 = new CountDownLatch(1);
141+
142+
TestHelper.SimpleDataSyncCallback cb1 = new TestHelper.SimpleDataSyncCallback() {
143+
@Override
144+
public void readyCallback(SyncedObject syncedObject) {
145+
latch1.countDown();
146+
}
147+
148+
@Override
149+
public void replaceCallback(List updates, String path) {
150+
result = updates;
151+
latch2.countDown();
152+
}
153+
};
154+
155+
SyncedObject player = pubnub.sync(playerString, cb1);
156+
157+
latch1.await(5, TimeUnit.SECONDS);
158+
159+
SyncedObject tracks = player.child("tracks");
160+
String track2 = (String) tracks.replaceByIndex(1, "hi");
161+
assertEquals("track#2", track2);
162+
163+
latch2.await(5, TimeUnit.SECONDS);
164+
List updates = (List) cb1.getResult();
165+
assertEquals(1, updates.size());
166+
assertEquals("hi", ((SyncedObjectDelta) updates.get(0)).getValue());
167+
assertEquals("hi", tracks.getByIndex(1));
168+
169+
assertEquals(Integer.valueOf(3), tracks.size());
170+
assertEquals(0, latch1.getCount());
171+
}
100172
}

0 commit comments

Comments
 (0)