-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParseFieldOperations.java
143 lines (126 loc) · 5.62 KB
/
ParseFieldOperations.java
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
package com.parse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
/**
* Utility methods to deal with {@link ParseFieldOperation} decoding, both from JSON objects.
*/
/* package */ final class ParseFieldOperations {
// A map of all known decoders.
private static Map<String, ParseFieldOperationFactory> opDecoderMap = new HashMap<>();
private ParseFieldOperations() {
}
/**
* Registers a single factory for a given __op field value.
*/
private static void registerDecoder(String opName, ParseFieldOperationFactory factory) {
opDecoderMap.put(opName, factory);
}
/**
* Registers a list of default decoder functions that convert a JSONObject with an __op field,
* or a Parcel with a op name string, into a ParseFieldOperation.
*/
static void registerDefaultDecoders() {
registerDecoder(ParseRelationOperation.OP_NAME_BATCH, new ParseFieldOperationFactory() {
@Override
public ParseFieldOperation decode(JSONObject object, ParseDecoder decoder)
throws JSONException {
ParseFieldOperation op = null;
JSONArray ops = object.getJSONArray("ops");
for (int i = 0; i < ops.length(); ++i) {
ParseFieldOperation nextOp = ParseFieldOperations.decode(ops.getJSONObject(i), decoder);
op = nextOp.mergeWithPrevious(op);
}
return op;
}
});
registerDecoder(ParseDeleteOperation.OP_NAME, new ParseFieldOperationFactory() {
@Override
public ParseFieldOperation decode(JSONObject object, ParseDecoder decoder) {
return ParseDeleteOperation.getInstance();
}
});
registerDecoder(ParseIncrementOperation.OP_NAME, new ParseFieldOperationFactory() {
@Override
public ParseFieldOperation decode(JSONObject object, ParseDecoder decoder) {
return new ParseIncrementOperation((Number) decoder.decode(object.opt("amount")));
}
});
registerDecoder(ParseAddOperation.OP_NAME, new ParseFieldOperationFactory() {
@Override
public ParseFieldOperation decode(JSONObject object, ParseDecoder decoder) {
return new ParseAddOperation((Collection) decoder.decode(object.opt("objects")));
}
});
registerDecoder(ParseAddUniqueOperation.OP_NAME, new ParseFieldOperationFactory() {
@Override
public ParseFieldOperation decode(JSONObject object, ParseDecoder decoder) {
return new ParseAddUniqueOperation((Collection) decoder.decode(object.opt("objects")));
}
});
registerDecoder(ParseRemoveOperation.OP_NAME, new ParseFieldOperationFactory() {
@Override
public ParseFieldOperation decode(JSONObject object, ParseDecoder decoder) {
return new ParseRemoveOperation((Collection) decoder.decode(object.opt("objects")));
}
});
registerDecoder(ParseRelationOperation.OP_NAME_ADD, new ParseFieldOperationFactory() {
@Override
public ParseFieldOperation decode(JSONObject object, ParseDecoder decoder) {
JSONArray objectsArray = object.optJSONArray("objects");
List<ParseObject> objectsList = (List<ParseObject>) decoder.decode(objectsArray);
return new ParseRelationOperation<>(new HashSet<>(objectsList), null);
}
});
registerDecoder(ParseRelationOperation.OP_NAME_REMOVE, new ParseFieldOperationFactory() {
@Override
public ParseFieldOperation decode(JSONObject object, ParseDecoder decoder) {
JSONArray objectsArray = object.optJSONArray("objects");
List<ParseObject> objectsList = (List<ParseObject>) decoder.decode(objectsArray);
return new ParseRelationOperation<>(null, new HashSet<>(objectsList));
}
});
registerDecoder(ParseSetOperation.OP_NAME, new ParseFieldOperationFactory() {
@Override
public ParseFieldOperation decode(JSONObject object, ParseDecoder decoder) {
return null; // Not called.
}
});
}
/**
* Converts a parsed JSON object into a ParseFieldOperation.
*
* @param encoded A JSONObject containing an __op field.
* @return A ParseFieldOperation.
*/
static ParseFieldOperation decode(JSONObject encoded, ParseDecoder decoder) throws JSONException {
String op = encoded.optString("__op");
ParseFieldOperationFactory factory = opDecoderMap.get(op);
if (factory == null) {
throw new RuntimeException("Unable to decode operation of type " + op);
}
return factory.decode(encoded, decoder);
}
/**
* Converts a JSONArray into an ArrayList.
*/
static ArrayList<Object> jsonArrayAsArrayList(JSONArray array) {
ArrayList<Object> result = new ArrayList<>(array.length());
for (int i = 0; i < array.length(); ++i) {
try {
result.add(array.get(i));
} catch (JSONException e) {
// This can't actually happen.
throw new RuntimeException(e);
}
}
return result;
}
/**
* A function that creates a ParseFieldOperation from a JSONObject or a Parcel.
*/
private interface ParseFieldOperationFactory {
ParseFieldOperation decode(JSONObject object, ParseDecoder decoder) throws JSONException;
}
}