Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit b271355

Browse files
authored
Extracted and renamed ReplicationsCompletedListener (#511)
* Extracted and renamed `ReplicationsCompletedListener` Moved `ReplicationPolicyManager.ReplicationsCompletedListener` -> `PolicyReplicationsCompletedListener`. Moved `ReplicationPolicyManager.SimpleReplicationsCompleteListener` -> `PolicyReplicationsCompletedListener.SimpleListener`.
1 parent f46ee6e commit b271355

File tree

6 files changed

+82
-65
lines changed

6 files changed

+82
-65
lines changed

cloudant-sync-datastore-android/src/main/java/com/cloudant/sync/replication/ReplicationService.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* lifecycle and handle being killed or restarted by the operating system
3939
*/
4040
public abstract class ReplicationService extends Service
41-
implements ReplicationPolicyManager.ReplicationsCompletedListener {
41+
implements PolicyReplicationsCompletedListener {
4242

4343
public static final String EXTRA_INTENT = "intent";
4444

@@ -78,13 +78,13 @@ public abstract class ReplicationService extends Service
7878
private List<Message> mCommandQueue = new ArrayList<Message>();
7979

8080
/**
81-
* Stores the set of {@link ReplicationPolicyManager.ReplicationsCompletedListener}s
81+
* Stores the set of {@link PolicyReplicationsCompletedListener}s
8282
* listening for replication complete
8383
* events. Note that all modifications or iterations over mListeners should be protected by
8484
* synchronization on the mListeners object.
8585
*/
86-
private final Set<ReplicationPolicyManager.ReplicationsCompletedListener> mListeners = new
87-
HashSet<ReplicationPolicyManager.ReplicationsCompletedListener>();
86+
private final Set<PolicyReplicationsCompletedListener> mListeners = new
87+
HashSet<PolicyReplicationsCompletedListener>();
8888

8989
// It's safest to assume we could be transferring a large amount of data in a
9090
// replication, so we want a high performance WiFi connection even though it
@@ -303,7 +303,7 @@ protected void stopReplications() {
303303
@Override
304304
public void allReplicationsCompleted() {
305305
synchronized (mListeners) {
306-
for (ReplicationPolicyManager.ReplicationsCompletedListener listener : mListeners) {
306+
for (PolicyReplicationsCompletedListener listener : mListeners) {
307307
listener.allReplicationsCompleted();
308308
}
309309
}
@@ -314,7 +314,7 @@ public void allReplicationsCompleted() {
314314
@Override
315315
public void replicationCompleted(int id) {
316316
synchronized (mListeners) {
317-
for (ReplicationPolicyManager.ReplicationsCompletedListener listener : mListeners) {
317+
for (PolicyReplicationsCompletedListener listener : mListeners) {
318318
listener.replicationCompleted(id);
319319
}
320320
}
@@ -323,33 +323,33 @@ public void replicationCompleted(int id) {
323323
@Override
324324
public void replicationErrored(int id) {
325325
synchronized (mListeners) {
326-
for (ReplicationPolicyManager.ReplicationsCompletedListener listener : mListeners) {
326+
for (PolicyReplicationsCompletedListener listener : mListeners) {
327327
listener.replicationErrored(id);
328328
}
329329
}
330330
}
331331

332332
/**
333333
* Add a listener to the set of
334-
* {@link com.cloudant.sync.replication.ReplicationPolicyManager.ReplicationsCompletedListener}s that are notified when
334+
* {@link PolicyReplicationsCompletedListener}s that are notified when
335335
* replications complete.
336336
*
337337
* @param listener The listener to add.
338338
*/
339-
public void addListener(ReplicationPolicyManager.ReplicationsCompletedListener listener) {
339+
public void addListener(PolicyReplicationsCompletedListener listener) {
340340
synchronized (mListeners) {
341341
mListeners.add(listener);
342342
}
343343
}
344344

345345
/**
346346
* Remove a listener from the set of
347-
* {@link com.cloudant.sync.replication.ReplicationPolicyManager.ReplicationsCompletedListener}s that are notified when
347+
* {@link PolicyReplicationsCompletedListener}s that are notified when
348348
* replications complete.
349349
*
350350
* @param listener The listener to remove.
351351
*/
352-
public void removeListener(ReplicationPolicyManager.ReplicationsCompletedListener listener) {
352+
public void removeListener(PolicyReplicationsCompletedListener listener) {
353353
synchronized (mListeners) {
354354
mListeners.remove(listener);
355355
}

cloudant-sync-datastore-core/src/main/java/com/cloudant/sync/internal/replication/ReplicationListener.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.cloudant.sync.event.Subscribe;
44
import com.cloudant.sync.event.notifications.ReplicationCompleted;
55
import com.cloudant.sync.event.notifications.ReplicationErrored;
6-
import com.cloudant.sync.replication.ReplicationPolicyManager;
6+
import com.cloudant.sync.replication.PolicyReplicationsCompletedListener;
77
import com.cloudant.sync.replication.Replicator;
88

99
import java.util.HashSet;
@@ -12,16 +12,15 @@
1212
/**
1313
* This class is not intended as API, it is public for EventBus access only.
1414
* API consumers should not call these methods. Forwards events from the EventBus to a
15-
* {@link com.cloudant.sync.replication.ReplicationPolicyManager.ReplicationsCompletedListener}.
15+
* {@link PolicyReplicationsCompletedListener}.
1616
*/
1717
public class ReplicationListener {
1818

1919
private final Set<Replicator> replicatorsInProgress = new HashSet<Replicator>();
20-
private ReplicationPolicyManager.ReplicationsCompletedListener replicationsCompletedListener
20+
private PolicyReplicationsCompletedListener replicationsCompletedListener
2121
= null;
2222

23-
public void setReplicationsCompletedListener(ReplicationPolicyManager
24-
.ReplicationsCompletedListener
23+
public void setReplicationsCompletedListener(PolicyReplicationsCompletedListener
2524
replicationsCompletedListener) {
2625
this.replicationsCompletedListener = replicationsCompletedListener;
2726
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright © 2017 IBM Corp. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*/
14+
15+
package com.cloudant.sync.replication;
16+
17+
/**
18+
* Interface with methods to receive callbacks for different termination states
19+
* of the replications configured by a replication policy.
20+
*/
21+
public interface PolicyReplicationsCompletedListener {
22+
/**
23+
* Gets called back when all replicators completed.
24+
*/
25+
void allReplicationsCompleted();
26+
/**
27+
* Gets called back when a replication completed.
28+
* @param id the replication ID
29+
*/
30+
void replicationCompleted(int id);
31+
/**
32+
* Gets called back when a replication has an error.
33+
* @param id the replication ID
34+
*/
35+
void replicationErrored(int id);
36+
37+
/**
38+
* A simple {@link PolicyReplicationsCompletedListener}
39+
* to save clients having to override every method if they are only interested in a subset of
40+
* the events.
41+
*/
42+
class SimpleListener implements PolicyReplicationsCompletedListener {
43+
@Override
44+
public void allReplicationsCompleted() {
45+
}
46+
47+
@Override
48+
public void replicationCompleted(int id) {
49+
}
50+
51+
@Override
52+
public void replicationErrored(int id) {
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -29,27 +29,6 @@ public class ReplicationPolicyManager {
2929
private final List<Replicator> replicators = new ArrayList<Replicator>();
3030
private final ReplicationListener replicationListener = new ReplicationListener();
3131

32-
/**
33-
* Interface with methods to receive callbacks for different termination states
34-
* of the replications configured by a replication policy.
35-
*/
36-
public interface ReplicationsCompletedListener {
37-
/**
38-
* Gets called back when all replicators completed.
39-
*/
40-
void allReplicationsCompleted();
41-
/**
42-
* Gets called back when a replication completed.
43-
* @param id the replication ID
44-
*/
45-
void replicationCompleted(int id);
46-
/**
47-
* Gets called back when a replication has an error.
48-
* @param id the replication ID
49-
*/
50-
void replicationErrored(int id);
51-
}
52-
5332
protected void startReplications() {
5433
synchronized (replicators) {
5534
for (Replicator replicator : replicators) {
@@ -78,32 +57,13 @@ public void addReplicators(Replicator... replicators) {
7857

7958
/**
8059
* Interested parties wishing to receive replication lifecycle events should call
81-
* this method with a class implementing the {@link ReplicationsCompletedListener} interface.
60+
* this method with a class implementing the {@link PolicyReplicationsCompletedListener} interface.
8261
*
8362
* @param listener A class implementing appropriate callback methods for replication lifecycle
8463
* events
8564
*/
86-
public void setReplicationsCompletedListener(ReplicationsCompletedListener listener) {
65+
public void setReplicationsCompletedListener(PolicyReplicationsCompletedListener listener) {
8766
replicationListener.setReplicationsCompletedListener(listener);
8867
}
8968

90-
/**
91-
* A simple {@link ReplicationsCompletedListener}
92-
* to save clients having to override every method if they are only interested in a subset of
93-
* the events.
94-
*/
95-
public static class SimpleReplicationsCompletedListener implements
96-
ReplicationsCompletedListener {
97-
@Override
98-
public void allReplicationsCompleted() {
99-
}
100-
101-
@Override
102-
public void replicationCompleted(int id) {
103-
}
104-
105-
@Override
106-
public void replicationErrored(int id) {
107-
}
108-
}
10969
}

doc/migration.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,17 @@ winning revision.
253253
* The `IntervalTimerReplicationPolicyManager` was moved into the `cloudant-sync-datastore-javase`
254254
module since it was not suitable for running on Android anyway.
255255

256-
* The Android `ReplicationService` now uses the same `ReplicationsCompletedListener` interface as
257-
the `ReplicationPolicyManager`. The `ReplicationService.ReplicationCompleteListener` has been
258-
removed and implementers should implement `ReplicationPolicyManager.ReplicationsCompletedListener`
256+
* The `ReplicationPolicyManager.ReplicationsCompletedListener` interface has been moved to
257+
`com.cloudant.sync.replication.PolicyReplicationsCompletedListener`.
258+
259+
* The Android `ReplicationService` now uses the same `PolicyReplicationsCompletedListener` interface
260+
as the `ReplicationPolicyManager`. The `ReplicationService.ReplicationCompleteListener` has been
261+
removed and implementers should implement `PolicyReplicationsCompletedListener`
259262
instead. Migration also requires some minor method renames.
260263

261264
* The `ReplicationService.SimpleReplicationCompleteListener` has been moved to
262-
`ReplicationPolicyManager.SimpleReplicationsCompletedListener` and now implements
263-
`ReplicationPolicyManager.ReplicationsCompletedListener`.
265+
`PolicyReplicationsCompletedListener.SimpleListener` and now implements the
266+
`PolicyReplicationsCompletedListener` interface.
264267

265268
* It is now possible to have multiple subclasses of `PeriodicReplicationService` in the same Android app without
266269
them interfering with each other. If you have not used replication policies involving a `PeriodicReplicationService`

doc/replication-policies.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ private ServiceConnection mConnection = new ServiceConnection() {
337337
@Override
338338
public void onServiceConnected(ComponentName name, IBinder service) {
339339
mReplicationService = ((ReplicationService.LocalBinder) service).getService();
340-
mReplicationService.addListener(new ReplicationPolicyManager.SimpleReplicationsCompletedListener() {
340+
mReplicationService.addListener(new PolicyReplicationsCompletedListener.SimpleListener() {
341341
@Override
342342
public void replicationCompleted(int id) {
343343
// Check if this is the pull replication

0 commit comments

Comments
 (0)