forked from Netflix/hollow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Producer Listener support refactored for code reuse (Netflix#448)
* Producer Listener support refactored * Address PR feedback
- Loading branch information
Showing
10 changed files
with
128 additions
and
84 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
hollow/src/main/java/com/netflix/hollow/api/common/EventListener.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,7 @@ | ||
package com.netflix.hollow.api.common; | ||
|
||
/** | ||
* The top-level type for all listeners. | ||
*/ | ||
public interface EventListener { | ||
} |
30 changes: 30 additions & 0 deletions
30
hollow/src/main/java/com/netflix/hollow/api/common/ListenerSupport.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,30 @@ | ||
package com.netflix.hollow.api.common; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
public class ListenerSupport { | ||
|
||
protected final CopyOnWriteArrayList<EventListener> eventListeners; | ||
|
||
public ListenerSupport() { | ||
eventListeners = new CopyOnWriteArrayList<>(); | ||
} | ||
|
||
public ListenerSupport(List<? extends EventListener> listeners) { | ||
eventListeners = new CopyOnWriteArrayList<>(listeners); | ||
} | ||
|
||
public ListenerSupport(ListenerSupport that) { | ||
eventListeners = new CopyOnWriteArrayList<>(that.eventListeners); | ||
} | ||
|
||
public void addListener(EventListener listener) { | ||
eventListeners.addIfAbsent(listener); | ||
} | ||
|
||
public void removeListener(EventListener listener) { | ||
eventListeners.remove(listener); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
hollow/src/main/java/com/netflix/hollow/api/common/Listeners.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,44 @@ | ||
package com.netflix.hollow.api.common; | ||
|
||
import com.netflix.hollow.api.producer.listener.VetoableListener; | ||
import java.util.Arrays; | ||
import java.util.function.Consumer; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Stream; | ||
|
||
public abstract class Listeners { | ||
|
||
private static final Logger LOG = Logger.getLogger(Listeners.class.getName()); | ||
|
||
protected final EventListener[] listeners; | ||
|
||
protected Listeners(EventListener[] listeners) { | ||
this.listeners = listeners; | ||
} | ||
|
||
public <T extends EventListener> Stream<T> getListeners(Class<T> c) { | ||
return Arrays.stream(listeners).filter(c::isInstance).map(c::cast); | ||
} | ||
|
||
protected <T extends EventListener> void fire( | ||
Class<T> c, Consumer<? super T> r) { | ||
fireStream(getListeners(c), r); | ||
} | ||
|
||
protected <T extends EventListener> void fireStream( | ||
Stream<T> s, Consumer<? super T> r) { | ||
s.forEach(l -> { | ||
try { | ||
r.accept(l); | ||
} catch (VetoableListener.ListenerVetoException e) { | ||
throw e; | ||
} catch (RuntimeException e) { | ||
if (l instanceof VetoableListener) { | ||
throw e; | ||
} | ||
LOG.log(Level.WARNING, "Error executing listener", e); | ||
} | ||
}); | ||
} | ||
} |
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
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
Oops, something went wrong.