|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.repositories; |
|
6 |
|
7 import java.util.HashSet; |
|
8 import java.util.Iterator; |
|
9 |
|
10 import org.mozilla.gecko.sync.repositories.domain.Record; |
|
11 |
|
12 public class HashSetStoreTracker implements StoreTracker { |
|
13 |
|
14 // Guarded by `this`. |
|
15 // Used to store GUIDs that were not locally modified but |
|
16 // have been modified by a call to `store`, and thus |
|
17 // should not be returned by a subsequent fetch. |
|
18 private HashSet<String> guids; |
|
19 |
|
20 public HashSetStoreTracker() { |
|
21 guids = new HashSet<String>(); |
|
22 } |
|
23 |
|
24 @Override |
|
25 public String toString() { |
|
26 return "#<Tracker: " + guids.size() + " guids tracked.>"; |
|
27 } |
|
28 |
|
29 @Override |
|
30 public synchronized boolean trackRecordForExclusion(String guid) { |
|
31 return (guid != null) && guids.add(guid); |
|
32 } |
|
33 |
|
34 @Override |
|
35 public synchronized boolean isTrackedForExclusion(String guid) { |
|
36 return (guid != null) && guids.contains(guid); |
|
37 } |
|
38 |
|
39 @Override |
|
40 public synchronized boolean untrackStoredForExclusion(String guid) { |
|
41 return (guid != null) && guids.remove(guid); |
|
42 } |
|
43 |
|
44 @Override |
|
45 public RecordFilter getFilter() { |
|
46 if (guids.size() == 0) { |
|
47 return null; |
|
48 } |
|
49 return new RecordFilter() { |
|
50 @Override |
|
51 public boolean excludeRecord(Record r) { |
|
52 return isTrackedForExclusion(r.guid); |
|
53 } |
|
54 }; |
|
55 } |
|
56 |
|
57 @Override |
|
58 public Iterator<String> recordsTrackedForExclusion() { |
|
59 return this.guids.iterator(); |
|
60 } |
|
61 } |