Skip to content

Commit 168cb1f

Browse files
committed
Add new ParseACL(acl) for copying
1 parent 638b7d4 commit 168cb1f

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

Parse/src/main/java/com/parse/ParseACL.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,41 @@ public static void setDefaultACL(ParseACL acl, boolean withAccessForCurrentUser)
103103
return getDefaultACLController().get();
104104
}
105105

106+
// State
107+
private final Map<String, Permissions> permissionsById = new HashMap<>();
106108
private boolean shared;
109+
107110
/**
108111
* A lazy user that hasn't been saved to Parse.
109112
*/
110113
//TODO (grantland): This should be a list for multiple lazy users with read/write permissions.
111114
private ParseUser unresolvedUser;
112115

113-
private Map<String, Permissions> permissionsById;
114-
115116
/**
116117
* Creates an ACL with no permissions granted.
117118
*/
118119
public ParseACL() {
119-
permissionsById = new HashMap<>();
120+
// do nothing
120121
}
121122

122-
/* package */ ParseACL copy() {
123-
ParseACL copy = new ParseACL();
124-
for (String id : permissionsById.keySet()) {
125-
copy.permissionsById.put(id, new Permissions(permissionsById.get(id)));
123+
/**
124+
* Creates a copy of {@code acl}.
125+
*
126+
* @param acl
127+
* The acl to copy.
128+
*/
129+
public ParseACL(ParseACL acl) {
130+
for (String id : acl.permissionsById.keySet()) {
131+
permissionsById.put(id, new Permissions(acl.permissionsById.get(id)));
126132
}
127-
copy.unresolvedUser = unresolvedUser;
133+
unresolvedUser = acl.unresolvedUser;
128134
if (unresolvedUser != null) {
129-
unresolvedUser.registerSaveListener(new UserResolutionListener(copy));
135+
unresolvedUser.registerSaveListener(new UserResolutionListener(this));
130136
}
131-
return copy;
137+
}
138+
139+
/* package for tests */ ParseACL copy() {
140+
return new ParseACL(this);
132141
}
133142

134143
boolean isShared() {

Parse/src/main/java/com/parse/ParseObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3397,7 +3397,7 @@ private ParseACL getACL(boolean mayCopy) {
33973397
throw new RuntimeException("only ACLs can be stored in the ACL key");
33983398
}
33993399
if (mayCopy && ((ParseACL) acl).isShared()) {
3400-
ParseACL copy = ((ParseACL) acl).copy();
3400+
ParseACL copy = new ParseACL((ParseACL) acl);
34013401
estimatedData.put(KEY_ACL, copy);
34023402
return copy;
34033403
}

Parse/src/main/java/com/parse/ParsePush.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ private static void checkArgument(boolean expression, Object errorMessage) {
5555
private Boolean pushToAndroid;
5656
private JSONObject data;
5757

58+
public Builder() {
59+
// do nothing
60+
}
61+
62+
private Builder(State state) {
63+
this.channelSet = state.channelSet == null
64+
? null
65+
: Collections.unmodifiableSet(new HashSet<>(state.channelSet));
66+
this.query = state.queryState == null
67+
? null
68+
: new ParseQuery<>(new ParseQuery.State.Builder<ParseInstallation>(state.queryState));
69+
this.expirationTime = state.expirationTime;
70+
this.expirationTimeInterval = state.expirationTimeInterval;
71+
this.pushToIOS = state.pushToIOS;
72+
this.pushToAndroid = state.pushToAndroid;
73+
// Since in state.build() we check data is not null, we do not need to check it again here.
74+
JSONObject copyData = null;
75+
try {
76+
copyData = new JSONObject(state.data.toString());
77+
} catch (JSONException e) {
78+
// Swallow this silently since it is impossible to happen
79+
}
80+
this.data = copyData;
81+
}
82+
5883
public Builder expirationTime(Long expirationTime) {
5984
this.expirationTime = expirationTime;
6085
expirationTimeInterval = null;
@@ -184,15 +209,27 @@ public JSONObject data() {
184209
/* package for test */ final State.Builder builder;
185210

186211
/**
187-
* Creates a new push notification. The default channel is the empty string, also known as the
188-
* global broadcast channel, but this value can be overridden using {@link #setChannel(String)},
189-
* {@link #setChannels(Collection)} or {@link #setQuery(ParseQuery)}. Before sending the push
190-
* notification you must call either {@link #setMessage(String)} or {@link #setData(JSONObject)}.
212+
* Creates a new push notification.
213+
*
214+
* The default channel is the empty string, also known as the global broadcast channel, but this
215+
* value can be overridden using {@link #setChannel(String)}, {@link #setChannels(Collection)} or
216+
* {@link #setQuery(ParseQuery)}. Before sending the push notification you must call either
217+
* {@link #setMessage(String)} or {@link #setData(JSONObject)}.
191218
*/
192219
public ParsePush() {
193220
this(new State.Builder());
194221
}
195222

223+
/**
224+
* Creates a copy of {@code push}.
225+
*
226+
* @param push
227+
* The push to copy.
228+
*/
229+
public ParsePush(ParsePush push) {
230+
this(new State.Builder(push.builder.build()));
231+
}
232+
196233
private ParsePush(State.Builder builder) {
197234
this.builder = builder;
198235
}

Parse/src/test/java/com/parse/ParseACLTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void testCopy() throws Exception {
8888
// setReadAccess()
8989
reset(unresolvedUser);
9090

91-
ParseACL copiedACL = acl.copy();
91+
ParseACL copiedACL = new ParseACL(acl);
9292

9393
assertEquals(1, copiedACL.getPermissionsById().size());
9494
assertTrue(copiedACL.getPermissionsById().containsKey(UNRESOLVED_KEY));
@@ -111,7 +111,7 @@ public void testCopyWithSaveListener() throws Exception {
111111
// setReadAccess()
112112
reset(unresolvedUser);
113113

114-
ParseACL copiedACL = acl.copy();
114+
ParseACL copiedACL = new ParseACL(acl);
115115

116116
// Make sure the callback is called
117117
ArgumentCaptor<GetCallback> callbackCaptor = ArgumentCaptor.forClass(GetCallback.class);

0 commit comments

Comments
 (0)