.thenRun(() -> prepareNextActivation());
}
- public CompletableFuture<Void> getActivated() {
+ CompletableFuture<Void> getActivated() {
return activated;
}
- public CompletableFuture<Void> getDeactivated() {
+ CompletableFuture<Void> getDeactivated() {
return deactivated;
}
dependants.add(dependant);
}
- I getInstance() {
+ public I getInstance() {
return instance;
}
@SuppressWarnings("unchecked")
- <T> PublishedType<T> getType(Class<T> clss) {
+ public <T> PublishedType<T> getType(Class<T> clss) {
if (!types.containsKey(clss))
throw new IllegalArgumentException(clss.getName() + " is not a type published by this component");
return (PublishedType<T>) types.get(clss);
}
- <T> boolean isPublishedType(Class<T> clss) {
+ public <T> boolean isPublishedType(Class<T> clss) {
return types.containsKey(clss);
}
+ /** A type which has been explicitly exposed by a component. */
public static class PublishedType<T> {
private Component<? extends T> component;
private Class<T> clss;
-// private CompletableFuture<Component<? extends T>> publisherAvailable;
private CompletableFuture<T> value;
public PublishedType(Component<? extends T> component, Class<T> clss) {
this.clss = clss;
this.component = component;
value = CompletableFuture.completedFuture((T) component.instance);
-// value = publisherAvailable.thenApply((c) -> c.getInstance());
}
- Component<?> getPublisher() {
+ public Component<?> getPublisher() {
return component;
}
-// CompletableFuture<Component<? extends T>> publisherAvailable() {
-// return publisherAvailable;
-// }
-
- Class<T> getType() {
+ public Class<T> getType() {
return clss;
}
}
+ /** Builds a {@link Component}. */
public static class Builder<I> {
private final I instance;
return this;
}
- public Builder<I> addInit(Runnable init) {
+ public Builder<I> addActivation(Runnable init) {
if (this.init != null)
throw new IllegalArgumentException("init method is already set");
this.init = init;
return this;
}
- public Builder<I> addClose(Runnable close) {
+ public Builder<I> addDeactivation(Runnable close) {
if (this.close != null)
throw new IllegalArgumentException("close method is already set");
this.close = close;
public Dependency(PublishedType<D> types, Consumer<D> set, Consumer<D> unset) {
super();
this.type = types;
- this.set = set;
- this.unset = unset != null ? unset : (v) -> set.accept(null);
+ this.set = set != null ? set : t -> {
+ };
+ this.unset = unset != null ? unset : t -> {
+ };
}
// live
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
-import java.util.function.Consumer;
import java.util.function.Predicate;
/** A minimal component register. */
-public class StaticRegister {
- private final static AtomicBoolean started = new AtomicBoolean(false);
- private final static IdentityHashMap<Object, Component<?>> components = new IdentityHashMap<>();
+public class StaticRegister implements ComponentRegister {
+ private final static StaticRegister instance = new StaticRegister();
- public static Consumer<Component<?>> asConsumer() {
- return (c) -> registerComponent(c);
+ public static ComponentRegister getInstance() {
+ return instance;
}
-// public static BiFunction<Class<?>, Predicate<Map<String, Object>>, Component<?>> asProvider() {
-//
-// }
+ private final AtomicBoolean started = new AtomicBoolean(false);
+ private final IdentityHashMap<Object, Component<?>> components = new IdentityHashMap<>();
- static synchronized <T> Component<? extends T> find(Class<T> clss, Predicate<Map<String, Object>> filter) {
+ @Override
+ public void accept(Component<?> component) {
+ registerComponent(component);
+ }
+
+ @SuppressWarnings({ "unchecked" })
+ @Override
+ public synchronized <T> Component<? extends T> find(Class<T> clss, Predicate<Map<String, Object>> filter) {
Set<Component<? extends T>> result = new HashSet<>();
instances: for (Object instance : components.keySet()) {
if (!clss.isAssignableFrom(instance.getClass()))
}
- static synchronized void registerComponent(Component<?> component) {
+ synchronized void registerComponent(Component<?> component) {
if (started.get()) // TODO make it really dynamic
throw new IllegalStateException("Already activated");
if (components.containsKey(component.getInstance()))
components.put(component.getInstance(), component);
}
- static synchronized Component<?> get(Object instance) {
+ @Override
+ public synchronized Component<?> get(Object instance) {
if (!components.containsKey(instance))
throw new IllegalArgumentException("Not registered as component");
return components.get(instance);
}
- synchronized static void activate() {
+ @Override
+ public synchronized void activate() {
if (started.get())
throw new IllegalStateException("Already activated");
Set<CompletableFuture<?>> constraints = new HashSet<>();
try {
CompletableFuture.allOf(constraints.toArray(new CompletableFuture[0])).thenRun(() -> started.set(true))
.get();
- } catch (ExecutionException | InterruptedException e) {
- throw new RuntimeException(e);
+ } catch (InterruptedException e) {
+ throw new RuntimeException("Register activation has been interrupted", e);
+ } catch (ExecutionException e) {
+ if (RuntimeException.class.isAssignableFrom(e.getCause().getClass())) {
+ throw (RuntimeException) e.getCause();
+ } else {
+ throw new IllegalStateException("Cannot activate register", e.getCause());
+ }
}
}
- synchronized static void deactivate() {
+ @Override
+ public synchronized void deactivate() {
if (!started.get())
throw new IllegalStateException("Not activated");
Set<CompletableFuture<?>> constraints = new HashSet<>();
try {
CompletableFuture.allOf(constraints.toArray(new CompletableFuture[0])).thenRun(() -> started.set(false))
.get();
- } catch (ExecutionException | InterruptedException e) {
- throw new RuntimeException(e);
+ } catch (InterruptedException e) {
+ throw new RuntimeException("Register deactivation has been interrupted", e);
+ } catch (ExecutionException e) {
+ if (RuntimeException.class.isAssignableFrom(e.getCause().getClass())) {
+ throw (RuntimeException) e.getCause();
+ } else {
+ throw new IllegalStateException("Cannot deactivate register", e.getCause());
+ }
}
}
- synchronized static void clear() {
+ @Override
+ public synchronized boolean isActive() {
+ return started.get();
+ }
+
+ @Override
+ public synchronized void clear() {
components.clear();
}