-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add DcEventCenter and start using it, tackles #11
- Loading branch information
Showing
5 changed files
with
141 additions
and
18 deletions.
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
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,81 @@ | ||
/******************************************************************************* | ||
* | ||
* Delta Chat Java Adapter | ||
* (C) 2017 Björn Petersen | ||
* Contact: r10s@b44t.com, http://b44t.com | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation, either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see http://www.gnu.org/licenses/ . | ||
* | ||
******************************************************************************/ | ||
|
||
|
||
package com.b44t.messenger; | ||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.Enumeration; | ||
import java.util.Hashtable; | ||
|
||
public class DcEventCenter { | ||
private Hashtable<Integer, ArrayList<Object>> allObservers = new Hashtable<>(); | ||
private final Object LOCK = new Object(); | ||
|
||
public interface DcEventDelegate { | ||
void handleEvent(int eventId, Object data1, Object data2); | ||
} | ||
|
||
public void addObserver(Object observer, int eventId) { | ||
synchronized (LOCK) { | ||
ArrayList<Object> idObservers = allObservers.get(eventId); | ||
if (idObservers == null) { | ||
allObservers.put(eventId, (idObservers = new ArrayList<>())); | ||
} | ||
idObservers.add(observer); | ||
} | ||
} | ||
|
||
public void removeObserver(Object observer, int eventId) { | ||
synchronized (LOCK) { | ||
ArrayList<Object> idObservers = allObservers.get(eventId); | ||
if (idObservers != null) { | ||
idObservers.remove(observer); | ||
} | ||
} | ||
} | ||
|
||
public void removeObservers(Object observer) { | ||
synchronized (LOCK) { | ||
Enumeration<Integer> enumKey = allObservers.keys(); | ||
while(enumKey.hasMoreElements()) { | ||
Integer eventId = enumKey.nextElement(); | ||
ArrayList<Object> idObservers = allObservers.get(eventId); | ||
if (idObservers != null) { | ||
idObservers.remove(observer); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void sendToObservers(int eventId, Object data1, Object data2) { | ||
synchronized (LOCK) { | ||
ArrayList<Object> idObservers = allObservers.get(eventId); | ||
if (idObservers != null) { | ||
for (int i = 0; i < idObservers.size(); i++) { | ||
Object observer = idObservers.get(i); | ||
((DcEventDelegate) observer).handleEvent(eventId, data1, data2); | ||
} | ||
} | ||
} | ||
} | ||
} |
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