Repository navigation
Expand file tree
/
Copy pathSyncedObject.java
More file actions
743 lines (637 loc) · 20.6 KB
/
Copy pathSyncedObject.java
File metadata and controls
743 lines (637 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package com.pubnub.api;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
public class SyncedObject implements Iterable {
private Pubnub pubnub;
private SyncedObjectManager syncedObjectManager;
private String path;
private String objectID;
private String location;
/**
* Is object ready
*
* false - just created
* true - when initial data object synchronization with server is done
*/
private AtomicBoolean isReady = new AtomicBoolean(false);
/**
* Is object connected
*
* false - just created
* true - when object is connected to updates channels
*/
private AtomicBoolean isConnected = new AtomicBoolean(false);
/**
* Is object explicitly unsubscribed
*
* false - just created
* true - after #unsubscribe() method invoked
*/
private AtomicBoolean isUnsubscribed = new AtomicBoolean(false);
public final static String TYPE_LIST = "List";
public final static String TYPE_OBJECT = "Object";
public final static String TYPE_INTEGER = "Integer";
public final static String TYPE_STRING = "String";
public final static String TYPE_BOOLEAN = "Boolean";
public SyncedObject(SyncedObjectManager manager, String objectID) {
this(manager, objectID, "");
}
public SyncedObject(SyncedObjectManager manager, String objectID, String path) {
this.syncedObjectManager = manager;
this.pubnub = manager.getPubnub();
this.objectID = objectID;
this.path = path;
this.location = SyncedObject.glue(objectID, path);
}
public String getPath() {
return path;
}
public String getObjectID() {
return objectID;
}
public String getLocation() {
return location;
}
public boolean getIsConnected() {
return isConnected.get();
}
public void setIsConnected(boolean isConnected) {
this.isConnected.set(isConnected);
}
public boolean getIsReady() {
return isReady.get();
}
public void setIsReady(boolean isReady) {
this.isReady.set(isReady);
}
public boolean getIsUnsubscribed() {
return this.isUnsubscribed.get();
}
/**
* Returns the value mapped by name, or throws if no such mapping exists.
*
* @param relativePath to element
* @return object
* @throws PubnubException
*/
public synchronized Object get(String relativePath) throws PubnubException {
return getValue(relativePath);
}
/**
* Returns the value mapped by name, or throws if no such mapping exists.
*
* @return object
*/
public synchronized Object get() {
try {
return getValue("");
} catch (PubnubException e) {
return null;
}
}
/**
* Returns the value mapped by name, or null if no such mapping exists.
*
* @param relativePath to element
* @return object or null
*/
public synchronized Object opt(String relativePath) {
try {
return getValue(relativePath);
} catch (PubnubException e) {
return null;
}
}
/**
* Returns the value mapped by name, or fallback if no such mapping exists.
*
* @param relativePath to element
* @return object
*/
public synchronized Object opt(String relativePath, Object fallback) {
try {
return getValue(relativePath);
} catch (PubnubException e) {
return fallback;
}
}
/**
* Returns the value mapped by name if it exists, coercing it if necessary, or throws if no such mapping exists.
*
* @param relativePath to value
* @return value as string
* @throws PubnubException
*/
public synchronized String getString(String relativePath) throws PubnubException {
return getValue(relativePath).toString();
}
/**
* Returns the mapped value, coercing it if necessary.
*
* @return value as string
*/
public synchronized String getString() {
try {
return getString("");
} catch (PubnubException e) {
return null;
}
}
/**
* Returns the value mapped by name if it exists, coercing it if necessary, or the empty string if no such mapping exists.
*
* @param relativePath to value
* @return value as string
*/
public synchronized String optString(String relativePath) {
try {
return getString(relativePath);
} catch (PubnubException e) {
return "";
}
}
/**
* Returns the value mapped by name if it exists, coercing it if necessary, or fallback if no such mapping exists.
*
* @param relativePath to value
* @param fallback value
* @return value as string
*/
public synchronized String optString(String relativePath, String fallback) {
try {
return getString(relativePath);
} catch (PubnubException e) {
return fallback;
}
}
/**
* Returns the value mapped by name if it exists, coercing it if necessary, or throws if no such mapping exists.
*
* @param relativePath to value
* @return value as int
*/
public synchronized int getInteger(String relativePath) throws PubnubException {
return Integer.parseInt(getValue(relativePath).toString());
}
/**
* Returns the mapped value, coercing it if necessary.
*
* @return value as int
*/
public synchronized int getInteger() {
try {
return Integer.parseInt(getValue("").toString());
} catch (PubnubException e) {
return 0;
}
}
/**
* Returns the value mapped by name if it exists and is an int or can be coerced to an int, or 0 otherwise.
*
* @param relativePath to value
* @return integer
*/
public synchronized int optInteger(String relativePath) {
try {
return getInteger(relativePath);
} catch (PubnubException e) {
return 0;
}
}
/**
* Returns the value mapped by name if it exists and is an int or can be coerced to an int, or fallback otherwise.
*
* @param relativePath to value
* @param fallback value
* @return integer
*/
public synchronized int optInteger(String relativePath, int fallback) {
try {
return getInteger(relativePath);
} catch (PubnubException e) {
return fallback;
}
}
/**
* Returns the value mapped by name if it exists and is a boolean or can be coerced to a boolean, or throws otherwise.
*
* @param relativePath to element
* @return value as boolean
* @throws PubnubException
*/
public synchronized boolean getBoolean(String relativePath) throws PubnubException {
Object value = getValue(relativePath);
if (value instanceof Boolean) {
return value.equals(Boolean.TRUE);
} else {
throw new PubnubException("Element by given location is not of Boolean type: \"" + location + "\"");
}
}
/**
* Returns the mapped value
*
* @return value as boolean
*/
public synchronized boolean getBoolean() {
try {
return getBoolean("");
} catch (PubnubException e) {
return false;
}
}
/**
* Returns the value mapped by name if it exists and is a boolean or can be coerced to a boolean, or false otherwise.
*
* @param relativePath to element
* @return value or false
*/
public synchronized boolean optBoolean(String relativePath) {
try {
return getBoolean(relativePath);
} catch (PubnubException e) {
return false;
}
}
/**
* Returns the value mapped by name if it exists and is a boolean or can be coerced to a boolean, or fallback otherwise.
*
* @param relativePath to element
* @param fallback value
* @return value or fallback
*/
public synchronized boolean optBoolean(String relativePath, boolean fallback) {
try {
return getBoolean(relativePath);
} catch (PubnubException e) {
return fallback;
}
}
public synchronized ArrayList getList() {
return getList(null);
}
public synchronized ArrayList getList(String relativePath) {
try {
return (ArrayList) getValue(relativePath);
} catch (PubnubException e) {
return null;
}
}
public synchronized HashMap getMap() {
return getMap(null);
}
public synchronized HashMap getMap(String relativePath) {
try {
return (HashMap) getValue(relativePath);
} catch (PubnubException e) {
return null;
}
}
private synchronized Object getValue(String relativePath) throws PubnubException {
try {
return syncedObjectManager.getValue(glue(location, relativePath));
} catch (JSONException e) {
throw new PubnubException("Unable to reach element by specified location: \"" + location + "\"");
}
}
public synchronized Object pop() {
return pop(null);
}
public synchronized Object pop(Callback callback) {
try {
String lastKey = syncedObjectManager.lastListKey(location);
Object result = syncedObjectManager.getValue(glue(location, lastKey));
if (callback == null) {
remove(lastKey);
} else {
remove(lastKey);
}
return result;
} catch (JSONException e) {
return null;
}
}
public synchronized Object shift() {
try {
String firstKey = syncedObjectManager.firstListKey(location);
Object result = syncedObjectManager.getValue(glue(location, firstKey));
remove(firstKey);
return result;
} catch (JSONException e) {
return null;
}
}
public synchronized Object removeByIndex(Integer index) {
String key = getKeyByIndex(index);
if (getKeyByIndex(index) != null) {
try {
Object result = syncedObjectManager.getValue(glue(location, key));
remove(key);
return result;
} catch (JSONException e) {
return null;
}
} else {
return null;
}
}
public synchronized Object replaceByIndex(Integer index, Object data) {
String key = getKeyByIndex(index);
if (getKeyByIndex(index) != null) {
try {
Object result = syncedObjectManager.getValue(glue(location, key));
replace(key, data);
return result;
} catch (JSONException e) {
return null;
}
} else {
return null;
}
}
public synchronized Object getByIndex(Integer index) {
String key = getKeyByIndex(index);
if (getKeyByIndex(index) != null) {
try {
return syncedObjectManager.getValue(glue(location, key));
} catch (JSONException e) {
return null;
}
} else {
return null;
}
}
public synchronized String getKeyByIndex(Integer index) {
try {
JSONObject value = syncedObjectManager.getRawValue(location);
if (isPnList(value)) {
int i = 0;
Iterator valueIterator = PubnubUtil.jsonObjectKeysSortedIterator(value);
while (valueIterator.hasNext()) {
if (index.intValue() == i) {
return (String) valueIterator.next();
} else {
valueIterator.next();
i++;
}
}
return null;
} else {
return null;
}
} catch (JSONException e) {
return null;
}
}
public synchronized Object removeByKey(String key) {
try {
Object value = syncedObjectManager.getValue(glue(location, key));
remove(key);
return value;
} catch (JSONException e) {
return null;
}
}
public synchronized Object replaceByKey(String key, Object data) {
try {
Object value = syncedObjectManager.getValue(glue(location, key));
replace(key, data);
return value;
} catch (JSONException e) {
return null;
}
}
public synchronized Object removeByValue(Object searchValue) {
String key = getKeyByValue(searchValue);
if (key != null) {
Object result;
try {
result = syncedObjectManager.getValue(glue(location, key));
} catch (JSONException e) {
return null;
}
remove(key);
return result;
} else {
return null;
}
}
public synchronized Object replaceByValue(Object searchValue, Object data) {
String key = getKeyByValue(searchValue);
if (key != null) {
Object result;
try {
result = syncedObjectManager.getValue(glue(location, key));
} catch (JSONException e) {
return null;
}
replace(key, data);
return result;
} else {
return null;
}
}
public synchronized String getKeyByValue(Object searchValue) {
if (searchValue == null) return null;
try {
JSONObject value = syncedObjectManager.getRawValue(location);
JSONArray valueKeys = value.names();
JSONObject currentObject;
String currentKey;
String resultKey = null;
for (int i = 0; i < valueKeys.length(); i++) {
currentKey = (String) valueKeys.get(i);
currentObject = value.getJSONObject(currentKey);
if (currentObject.has("pn_val") && currentObject.get("pn_val").equals(searchValue)) {
resultKey = currentKey;
break;
}
}
return resultKey;
} catch (JSONException e) {
return null;
}
}
public synchronized String getType() {
return getType("");
}
public synchronized String getType(String path) {
try {
JSONObject value = syncedObjectManager.getRawValue(glue(location, path));
if (value.has("pn_val")) {
Object rawValue = value.get("pn_val");
if (rawValue instanceof String) {
return TYPE_STRING;
} else if (rawValue instanceof Integer) {
return TYPE_INTEGER;
} else if (rawValue instanceof Boolean) {
return TYPE_BOOLEAN;
} else {
return null;
}
} else if (SyncedObject.isPnList(value)) {
return TYPE_LIST;
} else {
return TYPE_OBJECT;
}
} catch (JSONException e) {
return null;
}
}
public synchronized Integer size() {
return size("");
}
public synchronized Integer size(String path) {
try {
JSONObject value = syncedObjectManager.getRawValue(glue(location, path));
if (!value.has("pn_val")) {
return Integer.valueOf(value.length());
} else {
return null;
}
} catch (JSONException e) {
return null;
}
}
/**
* Return iterator if synced object instance represents list or object and null if it represents string, integer or boolean
*
* @return iterator or null
*/
public Iterator iterator() {
try {
final JSONObject rawValue = syncedObjectManager.getRawValue(location);
if (rawValue.has("pn_val")) {
return null;
}
if (isPnList(rawValue)) {
return new SyncedObjectIterator(this, PubnubUtil.jsonObjectKeysSortedIterator(rawValue));
} else {
return new SyncedObjectIterator(this, rawValue.keys());
}
} catch (JSONException e) {
return null;
}
}
/**
* Return child synced object.
*
* @param relativePath - relative path
* @param callback - callback
* @return child object
*/
public SyncedObject child(String relativePath, DataSyncCallback callback) {
return syncedObjectManager.add(objectID, glue(path, relativePath), callback);
}
public SyncedObject child(String relativePath) {
return child(relativePath, null);
}
public void merge(Object data) {
merge("", data);
}
public void merge(String path, Object data) {
merge(path, data, new Callback() {
});
}
public void merge(String path, Object data, final Callback callback) {
Hashtable args = new Hashtable();
args.put("location", glue(location, path));
args.put("data", data);
pubnub.merge(args, callback);
}
public void replace(Object data) {
replace("", data);
}
public void replace(String path, Object data) {
replace(path, data, new Callback() {
});
}
public void replace(String path, Object data, Callback callback) {
Hashtable args = new Hashtable();
args.put("location", glue(location, path));
args.put("data", data);
pubnub.replace(args, callback);
}
public void push(Object data) {
push("", data);
}
public void push(String path, Object data) {
push(path, data, new Callback() {
});
}
public void push(String path, Object data, Callback callback) {
Hashtable args = new Hashtable();
args.put("location", glue(location, path));
args.put("data", data);
pubnub.push(args, callback);
}
public void push(String path, Object data, String key) {
push(path, data, key, new Callback() {
});
}
public void push(Object data, String key, Callback callback) {
push("", data, key, callback);
}
public void push(String path, Object data, String key, Callback callback) {
Hashtable args = new Hashtable();
args.put("location", glue(location, path));
args.put("data", data);
args.put("sort_key", key);
pubnub.push(args, callback);
}
public void remove(String path) {
remove(path, new Callback() {
});
}
public void remove(String path, Callback callback) {
Hashtable args = new Hashtable();
args.put("location", glue(location, path));
pubnub.remove(args, callback);
}
public void remove() {
this.syncedObjectManager.remove(location);
this.isUnsubscribed.set(true);
}
public void unsubscribe() {
this.syncedObjectManager.unsubscribe(location);
this.isUnsubscribed.set(true);
}
public static String getURLizedObjectPath(String location) {
String[] path = PubnubUtil.splitString(location, ".");
for (int i = 0; i < path.length - 1; i++) {
path[i] = PubnubUtil.urlEncode(path[i]);
}
return PubnubUtil.joinString(path, "/");
}
public static String glue(String first, String second) {
if (PubnubUtil.isPresent(first) && PubnubUtil.isPresent(second)) {
return first + "." + second;
} else if (PubnubUtil.isPresent(first) && PubnubUtil.isBlank(second)) {
return first;
} else if (PubnubUtil.isBlank(first) && PubnubUtil.isPresent(second)) {
return second;
} else {
return "";
}
}
public static boolean isPnList(JSONObject item) {
if (item == null) {
return false;
}
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (item) {
JSONArray itemNames = item.names();
for (int i = 0; i < itemNames.length(); i++) {
try {
if (itemNames.getString(i).indexOf("-") == 0 && itemNames.getString(i).indexOf("!") > 0)
return true;
} catch (JSONException e) {
return false;
}
}
}
return false;
}
}