Skip to content

Commit

Permalink
event context
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghanzheng committed Jul 22, 2022
1 parent bfeb555 commit 1f19306
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import lombok.Setter;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

Expand Down Expand Up @@ -223,13 +220,7 @@ protected class EventOperatorImpl implements EventOperator {

protected Type type;

protected EventExchange exchange;

protected EventEncoder encoder;

protected EventDecoder decoder;

protected EventErrorHandler errorHandler;
protected Map<Object, Object> context = new LinkedHashMap<>();

protected EventOperatorImpl(Type type) {
this.type = type;
Expand All @@ -241,25 +232,37 @@ protected EventOperatorImpl(Object event) {

@Override
public EventOperator exchange(EventExchange exchange) {
this.exchange = exchange;
context.put(EventExchange.class, exchange);
return this;
}

@Override
public EventOperator encoder(EventEncoder encoder) {
this.encoder = encoder;
context.put(EventEncoder.class, encoder);
return this;
}

@Override
public EventOperator decoder(EventDecoder decoder) {
this.decoder = decoder;
context.put(EventDecoder.class, decoder);
return this;
}

@Override
public EventOperator error(EventErrorHandler errorHandler) {
this.errorHandler = errorHandler;
context.put(EventErrorHandler.class, errorHandler);
return this;
}

@Override
public EventOperator context(Object key, Object value) {
context.put(key, value);
return this;
}

@Override
public <K, V> EventOperator context(Map<K, V> context) {
this.context.putAll(context);
return this;
}

Expand Down Expand Up @@ -289,10 +292,7 @@ public void subscribe(EventSubscriber subscriber) {

protected EventContext buildContext() {
EventContext context = contextFactory.create();
context.put(EventExchange.class, exchange);
context.put(EventEncoder.class, encoder);
context.put(EventDecoder.class, decoder);
context.put(EventErrorHandler.class, errorHandler);
this.context.forEach(context::put);
return context;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ public interface EventOperator {

EventOperator exchange(EventExchange exchange);

EventOperator error(EventErrorHandler errorHandler);

EventOperator encoder(EventEncoder encoder);

EventOperator decoder(EventDecoder decoder);

EventOperator error(EventErrorHandler errorHandler);

EventOperator context(Object key, Object value);

<K, V> EventOperator context(Map<K, V> context);

void publish();

void publish(EventPublisher publisher);
Expand Down

0 comments on commit 1f19306

Please sign in to comment.