forked from ayld/Facade
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- the API now has a single register() method - listeners registering for events will also be notified for event subtypes - added tests for the new API - added new events during minimisation to improve status updates in the plugins - removed some jar-extraction related leftovers
- Loading branch information
Showing
19 changed files
with
270 additions
and
145 deletions.
There are no files selected for viewing
47 changes: 36 additions & 11 deletions
47
src/main/java/org/codarama/diet/api/ListenerRegistrar.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 |
---|---|---|
@@ -1,26 +1,51 @@ | ||
package org.codarama.diet.api; | ||
|
||
import com.google.common.annotations.Beta; | ||
import com.google.common.eventbus.EventBus; | ||
import com.google.common.eventbus.Subscribe; | ||
import org.codarama.diet.api.reporting.listener.EventListener; | ||
import org.codarama.diet.event.model.ComponentEvent; | ||
import org.codarama.diet.util.Components; | ||
|
||
import com.google.common.eventbus.EventBus; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
|
||
@Beta | ||
public final class ListenerRegistrar { | ||
|
||
private final Object[] listeners; | ||
|
||
private ListenerRegistrar(Object[] listeners) { | ||
this.listeners = listeners; | ||
private ListenerRegistrar() { | ||
} | ||
|
||
public static ListenerRegistrar listeners(Object... listeners) { | ||
return new ListenerRegistrar(listeners); | ||
public static <E extends ComponentEvent> void register(EventListener<E> listener) { | ||
Components.EVENT_BUS.<EventBus>getInstance().register(new GuavaListener<E>(listener)); | ||
} | ||
|
||
public void register() { | ||
for (Object l : listeners) { | ||
Components.EVENT_BUS.<EventBus>getInstance().register(l); | ||
|
||
private static class GuavaListener<E extends ComponentEvent> { | ||
private final EventListener<E> listener; | ||
|
||
private GuavaListener(EventListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
@Subscribe | ||
private void on(E event) { | ||
final Class listenerGenericType = getListenerGenericType(); | ||
final Class eventGenericType = event.getClass(); | ||
|
||
final boolean listenerTypeIsSuperclassOfEventType = listenerGenericType.isAssignableFrom(eventGenericType); | ||
if (listenerTypeIsSuperclassOfEventType) { | ||
listener.on(event); | ||
} | ||
} | ||
|
||
private Class getListenerGenericType() { | ||
final ParameterizedType listenerGenericType = (ParameterizedType) listener.getClass().getGenericInterfaces()[0]; | ||
final String listenerGenericTypeName = listenerGenericType.getActualTypeArguments()[0].getTypeName(); | ||
try { | ||
return Class.forName(listenerGenericTypeName); | ||
} catch (ClassNotFoundException e) { | ||
throw new IllegalStateException("Unknown generic type for listener: " + listener.getClass()); | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/codarama/diet/api/reporting/listener/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,16 @@ | ||
package org.codarama.diet.api.reporting.listener; | ||
|
||
import org.codarama.diet.event.model.ComponentEvent; | ||
|
||
/** | ||
* Implementers of the interface can subscribe to be notified for events during minimization. | ||
* | ||
* @see org.codarama.diet.api.ListenerRegistrar | ||
* @see org.codarama.diet.event.model.ComponentEvent | ||
* | ||
* Created by Ayld on 7/10/16. | ||
*/ | ||
public interface EventListener<E extends ComponentEvent> { | ||
|
||
void on(E event); | ||
} |
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
10 changes: 0 additions & 10 deletions
10
src/main/java/org/codarama/diet/event/model/JarExtractionStartEvent.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/main/java/org/codarama/diet/event/model/MinimizationEndEvent.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,12 @@ | ||
package org.codarama.diet.event.model; | ||
|
||
import org.codarama.diet.component.ListenableComponent; | ||
|
||
/** | ||
* Created by Ayld on 7/11/16. | ||
*/ | ||
public class MinimizationEndEvent extends MinimizationEvent{ | ||
public MinimizationEndEvent(String message, Class<? extends ListenableComponent> by) { | ||
super(message, by); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/codarama/diet/event/model/MinimizationEvent.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,13 @@ | ||
package org.codarama.diet.event.model; | ||
|
||
import org.codarama.diet.component.ListenableComponent; | ||
|
||
/** | ||
* Created by Ayld on 7/11/16. | ||
*/ | ||
public class MinimizationEvent extends ComponentEvent { | ||
|
||
public MinimizationEvent(String message, Class<? extends ListenableComponent> by) { | ||
super(message, by); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/codarama/diet/event/model/MinimizationStartEvent.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,12 @@ | ||
package org.codarama.diet.event.model; | ||
|
||
import org.codarama.diet.component.ListenableComponent; | ||
|
||
/** | ||
* Created by Ayld on 7/11/16. | ||
*/ | ||
public class MinimizationStartEvent extends MinimizationEvent { | ||
public MinimizationStartEvent(String message, Class<? extends ListenableComponent> by) { | ||
super(message, by); | ||
} | ||
} |
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.