Skip to content

Commit

Permalink
0.4.0-pre1 build
Browse files Browse the repository at this point in the history
this is the first 0.4.0 build that should be functional
  • Loading branch information
ShadowLordAlpha committed Aug 20, 2017
1 parent 40bbdc7 commit ef04ba8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
43 changes: 36 additions & 7 deletions src/main/java/com/shadowcs/themis/EventManager.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,62 @@
package com.shadowcs.themis;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;

public class EventManager {

private ExecutorService eService;
private LoadingCache<Class<?>, Collection<Consumer<?>>> lCache;

// TODO: eventually allow a custom cache or something if I replace caffeine
EventManager(ExecutorService eService) {
this.eService = eService;
lCache = Caffeine.newBuilder().build(key -> ConcurrentHashMap.newKeySet());
}

public <V> EventManager addListener(Consumer<V> listener, Class<V> klass) {

lCache.get(klass).add(listener);
return this;
}

public <V> EventManager removeListener(Consumer<V> listener, Class<V> klass) {

lCache.getIfPresent(klass).remove(listener);
return this;
}

// TODO: Submit, returns a future

public <T> Future<T> submit(T event) {

return eService.submit(() -> event(event), event);
}

// TODO: Execute, returns nothing

public <T> void execute(T event) {

eService.execute(() -> event(event));
}

// TODO: Invoke, blocking run on current thread

private class bogus<T> {
public <T> void invoke(T event) {
event(event);
}

private <T> void event(T event) {
// this should be working
lCache.asMap().forEach((key, col) -> {
if(key.isInstance(event)) {
col.forEach((listener) -> ((Consumer<T>) listener).accept(event)); // unsafe but should be valid and I
// really don't have a better way
}
});
}

public static void main(String[] args) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Objects;

// basically a runnable with different names
/**
* Represents an operation that accepts no input arguments and returns no result. Unlike most other functional
* interfaces, {@code Procedure} is expected to operate via side-effects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Feature: Thread Factory
The tasks run are thread safe so as long as the threads are created and run the value that comes out should be correct. If a
task is not thread safe then it is not the thread factory being tested.

# Basic tests using a default executor service
# Basic tests using a default executor service
Scenario: Single Thread Executor
Given Jeff creates a new "SINGLE" thread executor.
And Jeff executes 100 basic test tasks.
Expand Down

0 comments on commit ef04ba8

Please sign in to comment.