-
Notifications
You must be signed in to change notification settings - Fork 835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Multiple Notifiers Support (Range and Monitor) #392
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
07e90d5
#Notifiers: manage several RangeNotifiers and several MonitorNotifiers
ost-ct fa2796b
#MultipleNotifier: switch to CopyOnWriteArrayList
ost-ct 6a9842a
#RegionNotifier: split region job into RegionFilter
ost-ct 9f1d67b
#Notifiers: switch from CopyOnWriteArrayList to a CopyOnWriteArraySet
ost-ct File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/org/altbeacon/beacon/notifier/RegionFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.altbeacon.beacon.notifier; | ||
|
||
import org.altbeacon.beacon.Region; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by Connecthings on 21/06/16. | ||
*/ | ||
public class RegionFilter { | ||
|
||
private final List<Region> regions = new ArrayList<>(); | ||
|
||
/** | ||
* @param region | ||
*/ | ||
public void addRegion(Region region){ | ||
synchronized (regions){ | ||
regions.add(region); | ||
} | ||
} | ||
|
||
/** | ||
* @param region | ||
* @return | ||
*/ | ||
public boolean removeRegion(Region region){ | ||
synchronized (regions) { | ||
return regions.remove(region); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public void removeAllRegions(){ | ||
synchronized (regions){ | ||
regions.clear(); | ||
} | ||
} | ||
|
||
public boolean containRegion(Region region){ | ||
synchronized (regions){ | ||
return regions.contains(region); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/org/altbeacon/beacon/notifier/RegionMonitorNotifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.altbeacon.beacon.notifier; | ||
|
||
import org.altbeacon.beacon.Beacon; | ||
import org.altbeacon.beacon.MonitorNotifier; | ||
import org.altbeacon.beacon.RangeNotifier; | ||
import org.altbeacon.beacon.Region; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
/** | ||
* | ||
* Created by Connecthings on 21/06/16. | ||
*/ | ||
public abstract class RegionMonitorNotifier implements MonitorNotifier{ | ||
|
||
private final RegionFilter regionFilter = new RegionFilter(); | ||
|
||
public void addRegion(Region region) { | ||
regionFilter.addRegion(region); | ||
} | ||
|
||
public boolean removeRegion(Region region) { | ||
return regionFilter.removeRegion(region); | ||
} | ||
|
||
public void removeAllRegions() { | ||
regionFilter.removeAllRegions(); | ||
} | ||
|
||
@Override | ||
public void didDetermineStateForRegion(int state, Region region) { | ||
if(regionFilter.containRegion(region)){ | ||
didDetermineStateForInReferencedRegion(state, region); | ||
} | ||
} | ||
|
||
@Override | ||
public void didEnterRegion(Region region) { | ||
if(regionFilter.containRegion(region)) { | ||
didEnterInReferencedRegion(region); | ||
} | ||
} | ||
|
||
@Override | ||
public void didExitRegion(Region region) { | ||
if(regionFilter.containRegion(region)){ | ||
didExitInReferencedRegion(region); | ||
} | ||
} | ||
|
||
public abstract void didEnterInReferencedRegion (Region region ); | ||
|
||
public abstract void didExitInReferencedRegion (Region region ); | ||
|
||
public abstract void didDetermineStateForInReferencedRegion (int state, Region region ); | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/org/altbeacon/beacon/notifier/RegionRangeNotifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.altbeacon.beacon.notifier; | ||
|
||
import org.altbeacon.beacon.Beacon; | ||
import org.altbeacon.beacon.RangeNotifier; | ||
import org.altbeacon.beacon.Region; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by Connecthings on 21/06/16. | ||
*/ | ||
public abstract class RegionRangeNotifier implements RangeNotifier{ | ||
|
||
private final RegionFilter regionFilter = new RegionFilter(); | ||
|
||
|
||
public void addRegion(Region region) { | ||
regionFilter.addRegion(region); | ||
} | ||
|
||
|
||
public boolean removeRegion(Region region) { | ||
return regionFilter.removeRegion(region); | ||
} | ||
|
||
public void removeAllRegions() { | ||
regionFilter.removeAllRegions(); | ||
} | ||
|
||
@Override | ||
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { | ||
if(regionFilter.containRegion(region)){ | ||
didRangeBeaconsInReferencedRegion(beacons, region); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param beacons | ||
* @param region | ||
*/ | ||
public abstract void didRangeBeaconsInReferencedRegion ( Collection<Beacon> beacons, Region region ); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good if these collections were based on
Set
notList
. Because if folks repeatedly calladdXxxNotifier()
with the same object, it could cause trouble. Making this change could protect against this. Perhaps useConcurrentHashMap.newKeySet()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to use an CopyOnWriteArraySet. What do you think?
As consequences, the getMonitorNotifier() and the getRangeNotifier() method are irrelevants. Do you want to keep them or can I remove them ?
(I think these methods are only useful internally to the library. They are not in usage any more inside the library)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do need to keep
getMonitorNotifier()
andgetRangeNotifier()
so that this is not a breaking API change. I think aCopyOnWriteArraySet
is probably fine.