small code improvements
This commit is contained in:
parent
7c0c0b6f82
commit
ed901a8501
|
@ -28,18 +28,16 @@ public abstract class AbstractConcurrentSet<T> implements IConcurrentSet<T> {
|
|||
protected abstract Entry<T> createEntry(T value, Entry<T> next);
|
||||
|
||||
@Override
|
||||
public IConcurrentSet<T> add(T element) {
|
||||
if (element == null) return this;
|
||||
public void add(T element) {
|
||||
if (element == null) return;
|
||||
Lock writeLock = lock.writeLock();
|
||||
writeLock.lock();
|
||||
if (element == null || entries.containsKey(element)) {
|
||||
writeLock.unlock();
|
||||
return this;
|
||||
} else {
|
||||
insert(element);
|
||||
writeLock.unlock();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -69,7 +67,7 @@ public abstract class AbstractConcurrentSet<T> implements IConcurrentSet<T> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public IConcurrentSet<T> addAll(Iterable<T> elements) {
|
||||
public void addAll(Iterable<T> elements) {
|
||||
Lock writeLock = lock.writeLock();
|
||||
try {
|
||||
writeLock.lock();
|
||||
|
@ -81,7 +79,6 @@ public abstract class AbstractConcurrentSet<T> implements IConcurrentSet<T> {
|
|||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -8,13 +8,13 @@ package net.engio.mbassy.common;
|
|||
*/
|
||||
public interface IConcurrentSet<T> extends Iterable<T> {
|
||||
|
||||
IConcurrentSet<T> add(T element);
|
||||
void add(T element);
|
||||
|
||||
boolean contains(T element);
|
||||
|
||||
int size();
|
||||
|
||||
IConcurrentSet<T> addAll(Iterable<T> elements);
|
||||
void addAll(Iterable<T> elements);
|
||||
|
||||
boolean remove(T element);
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ public class MessageHandler {
|
|||
Map<String, Object> properties = new HashMap<String, Object>();
|
||||
properties.put(HandlerMethod, handler);
|
||||
// add EL filter if a condition is present
|
||||
if(handlerConfig.condition() != null){
|
||||
if(handlerConfig.condition().length() > 0){
|
||||
if (!ElFilter.isELAvailable()) {
|
||||
throw new IllegalStateException("A handler uses an EL filter but no EL implementation is available.");
|
||||
}
|
||||
|
|
|
@ -22,7 +22,8 @@ import org.junit.runners.Suite;
|
|||
DeadMessageTest.class,
|
||||
SynchronizedHandlerTest.class,
|
||||
SubscriptionManagerTest.class,
|
||||
AsyncFIFOBusTest.class
|
||||
AsyncFIFOBusTest.class,
|
||||
ConditionalHandlers.class
|
||||
})
|
||||
public class AllTests {
|
||||
}
|
||||
|
|
|
@ -1,168 +1,168 @@
|
|||
package net.engio.mbassy;
|
||||
|
||||
import net.engio.mbassy.bus.MBassador;
|
||||
import net.engio.mbassy.bus.config.BusConfiguration;
|
||||
import net.engio.mbassy.common.MessageBusTest;
|
||||
import net.engio.mbassy.listener.Enveloped;
|
||||
import net.engio.mbassy.listener.Handler;
|
||||
import net.engio.mbassy.listener.Listener;
|
||||
import net.engio.mbassy.listener.References;
|
||||
import net.engio.mbassy.subscription.MessageEnvelope;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/*****************************************************************************
|
||||
* Some unit tests for the "condition" filter.
|
||||
****************************************************************************/
|
||||
|
||||
public class ConditionTest extends MessageBusTest {
|
||||
|
||||
public static class TestEvent {
|
||||
|
||||
private Set<String> handledBy = new HashSet<String>();
|
||||
private String type;
|
||||
private int size;
|
||||
|
||||
public TestEvent(String type, int size) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public boolean wasHandledBy(String ...handlers){
|
||||
for(String handler : handlers){
|
||||
if (!handledBy.contains(handler)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void handledBy(String handler){
|
||||
handledBy.add(handler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Listener(references = References.Strong)
|
||||
public static class ConditionalMessageListener {
|
||||
|
||||
@Handler(condition = "msg.type == 'TEST'")
|
||||
public void handleTypeMessage(TestEvent message) {
|
||||
message.handledBy("handleTypeMessage");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.size > 4")
|
||||
public void handleSizeMessage(TestEvent message) {
|
||||
message.handledBy("handleSizeMessage");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.foo > 4")
|
||||
public void handleInvalidEL(TestEvent message) {
|
||||
message.handledBy("handleInvalidEL");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.size > 2 && msg.size < 4")
|
||||
public void handleCombinedEL(TestEvent message) {
|
||||
message.handledBy( "handleCombinedEL");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.getType().equals('XYZ') && msg.getSize() == 1")
|
||||
public void handleMethodAccessEL(TestEvent message) {
|
||||
message.handledBy("handleMethodAccessEL");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.type == 'TEST'")
|
||||
@Enveloped(messages = {TestEvent.class, Object.class})
|
||||
public void handleEnvelopedMessage(MessageEnvelope envelope) {
|
||||
envelope.<TestEvent>getMessage().handledBy("handleEnvelopedMessage");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testSimpleStringCondition() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("TEST", 0);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.wasHandledBy("handleTypeMessage", "handleEnvelopedMessage"));
|
||||
assertFalse(message.wasHandledBy("handleInvalidEL"));
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testSimpleNumberCondition() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("", 5);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.wasHandledBy("handleSizeMessage"));
|
||||
assertFalse(message.wasHandledBy("handleInvalidEL"));
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testHandleCombinedEL() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("", 3);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.wasHandledBy("handleCombinedEL"));
|
||||
assertFalse(message.wasHandledBy("handleInvalidEL"));
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testNotMatchingAnyCondition() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("", 0);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.handledBy.isEmpty());
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testHandleMethodAccessEL() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("XYZ", 1);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.wasHandledBy("handleMethodAccessEL"));
|
||||
assertFalse(message.wasHandledBy("handleInvalidEL"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
package net.engio.mbassy;
|
||||
|
||||
import net.engio.mbassy.bus.MBassador;
|
||||
import net.engio.mbassy.bus.config.BusConfiguration;
|
||||
import net.engio.mbassy.common.MessageBusTest;
|
||||
import net.engio.mbassy.listener.Enveloped;
|
||||
import net.engio.mbassy.listener.Handler;
|
||||
import net.engio.mbassy.listener.Listener;
|
||||
import net.engio.mbassy.listener.References;
|
||||
import net.engio.mbassy.subscription.MessageEnvelope;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/*****************************************************************************
|
||||
* Some unit tests for the "condition" filter.
|
||||
****************************************************************************/
|
||||
|
||||
public class ConditionalHandlers extends MessageBusTest {
|
||||
|
||||
public static class TestEvent {
|
||||
|
||||
private Set<String> handledBy = new HashSet<String>();
|
||||
private String type;
|
||||
private int size;
|
||||
|
||||
public TestEvent(String type, int size) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public boolean wasHandledBy(String ...handlers){
|
||||
for(String handler : handlers){
|
||||
if (!handledBy.contains(handler)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void handledBy(String handler){
|
||||
handledBy.add(handler);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Listener(references = References.Strong)
|
||||
public static class ConditionalMessageListener {
|
||||
|
||||
@Handler(condition = "msg.type == 'TEST'")
|
||||
public void handleTypeMessage(TestEvent message) {
|
||||
message.handledBy("handleTypeMessage");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.size > 4")
|
||||
public void handleSizeMessage(TestEvent message) {
|
||||
message.handledBy("handleSizeMessage");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.foo > 4")
|
||||
public void handleInvalidEL(TestEvent message) {
|
||||
message.handledBy("handleInvalidEL");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.size > 2 && msg.size < 4")
|
||||
public void handleCombinedEL(TestEvent message) {
|
||||
message.handledBy( "handleCombinedEL");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.getType().equals('XYZ') && msg.getSize() == 1")
|
||||
public void handleMethodAccessEL(TestEvent message) {
|
||||
message.handledBy("handleMethodAccessEL");
|
||||
}
|
||||
|
||||
@Handler(condition = "msg.type == 'TEST'")
|
||||
@Enveloped(messages = {TestEvent.class, Object.class})
|
||||
public void handleEnvelopedMessage(MessageEnvelope envelope) {
|
||||
envelope.<TestEvent>getMessage().handledBy("handleEnvelopedMessage");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testSimpleStringCondition() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("TEST", 0);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.wasHandledBy("handleTypeMessage", "handleEnvelopedMessage"));
|
||||
assertFalse(message.wasHandledBy("handleInvalidEL"));
|
||||
}
|
||||
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testSimpleNumberCondition() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("", 5);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.wasHandledBy("handleSizeMessage"));
|
||||
assertFalse(message.wasHandledBy("handleInvalidEL"));
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testHandleCombinedEL() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("", 3);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.wasHandledBy("handleCombinedEL"));
|
||||
assertFalse(message.wasHandledBy("handleInvalidEL"));
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testNotMatchingAnyCondition() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("", 0);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.handledBy.isEmpty());
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* @throws Exception
|
||||
************************************************************************/
|
||||
@Test
|
||||
public void testHandleMethodAccessEL() throws Exception {
|
||||
MBassador bus = getBus(BusConfiguration.Default());
|
||||
bus.subscribe(new ConditionalMessageListener());
|
||||
|
||||
TestEvent message = new TestEvent("XYZ", 1);
|
||||
bus.publish(message);
|
||||
|
||||
assertTrue(message.wasHandledBy("handleMethodAccessEL"));
|
||||
assertFalse(message.wasHandledBy("handleInvalidEL"));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -30,7 +30,7 @@ public class ListenerFactory {
|
|||
return this;
|
||||
}
|
||||
|
||||
public ListenerFactory create(int numberOfInstances, Class[] classes){
|
||||
public ListenerFactory create(int numberOfInstances, Class ...classes){
|
||||
for(Class clazz : classes)
|
||||
create(numberOfInstances,clazz);
|
||||
return this;
|
||||
|
|
Loading…
Reference in New Issue
Block a user