use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class TestPropertyChange method testWithSingleReferenceModel.
@Test
public void testWithSingleReferenceModel(@Mocked AbstractClientConnector connector) {
final ClientModelStore clientModelStore = createClientModelStore(connector);
final EventDispatcher dispatcher = createEventDispatcher(clientModelStore);
final BeanRepository repository = createBeanRepository(clientModelStore, dispatcher);
final BeanManager manager = createBeanManager(clientModelStore, repository, dispatcher);
final SimpleTestModel ref1 = manager.create(SimpleTestModel.class);
final SimpleTestModel ref2 = manager.create(SimpleTestModel.class);
final SimpleTestModel ref3 = manager.create(SimpleTestModel.class);
final SingleReferenceModel model = manager.create(SingleReferenceModel.class);
final ListerResults<SimpleTestModel> results = new ListerResults<>();
final ValueChangeListener<SimpleTestModel> myListener = new ValueChangeListener<SimpleTestModel>() {
@Override
public void valueChanged(ValueChangeEvent<? extends SimpleTestModel> evt) {
Assert.assertEquals(evt.getSource(), model.getReferenceProperty());
results.newValue = evt.getNewValue();
results.oldValue = evt.getOldValue();
results.listenerCalls++;
}
};
final Subscription subscription = model.getReferenceProperty().onChanged(myListener);
assertThat(results.listenerCalls, is(0));
assertThat(results.newValue, nullValue());
assertThat(results.oldValue, nullValue());
model.getReferenceProperty().set(ref1);
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, is(ref1));
assertThat(results.oldValue, nullValue());
results.listenerCalls = 0;
model.getReferenceProperty().set(ref2);
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, is(ref2));
assertThat(results.oldValue, is(ref1));
results.listenerCalls = 0;
model.getReferenceProperty().set(null);
assertThat(results.listenerCalls, is(1));
assertThat(results.newValue, nullValue());
assertThat(results.oldValue, is(ref2));
results.listenerCalls = 0;
subscription.unsubscribe();
model.getReferenceProperty().set(ref3);
assertThat(results.listenerCalls, is(0));
assertThat(results.newValue, nullValue());
assertThat(results.oldValue, is(ref2));
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class DistributedEventBus method subscribe.
@Override
public <T extends Serializable> Subscription subscribe(final Topic<T> topic, final MessageListener<? super T> handler, final Predicate<MessageEventContext<T>> filter) {
final Subscription basicSubscription = super.subscribe(topic, handler, filter);
final Subscription hazelcastSubscription = createHazelcastSubscription(topic);
return new Subscription() {
@Override
public void unsubscribe() {
hazelcastSubscription.unsubscribe();
basicSubscription.unsubscribe();
}
};
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class DefaultDolphinBinder method bidirectionalTo.
@Override
public <T> Binding bidirectionalTo(final javafx.beans.property.Property<T> javaFxProperty, final BidirectionalConverter<T, S> converter) {
if (javaFxProperty == null) {
throw new IllegalArgumentException("javaFxProperty must not be null");
}
if (converter == null) {
throw new IllegalArgumentException("converter must not be null");
}
final Binding unidirectionalBinding = to(javaFxProperty, converter);
final Subscription subscription = property.onChanged(e -> javaFxProperty.setValue(converter.convertBack(property.get())));
return () -> {
unidirectionalBinding.unbind();
subscription.unsubscribe();
};
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class DefaultJavaFXListBinder method bidirectionalTo.
@Override
public <T> Binding bidirectionalTo(final ObservableList<T> dolphinList, final Function<? super T, ? extends S> converter, final Function<? super S, ? extends T> backConverter) {
Assert.requireNonNull(dolphinList, "dolphinList");
Assert.requireNonNull(converter, "converter");
Assert.requireNonNull(backConverter, "backConverter");
if (boundLists.containsKey(list)) {
throw new IllegalStateException("A JavaFX list can only be bound to one Dolphin Platform list!");
}
final InternalBidirectionalListChangeListener<T> listChangeListener = new InternalBidirectionalListChangeListener<>(dolphinList, converter, backConverter);
final Subscription subscription = dolphinList.onChanged(listChangeListener);
list.setAll(dolphinList.stream().map(converter).collect(Collectors.toList()));
list.addListener(listChangeListener);
return () -> {
subscription.unsubscribe();
list.removeListener(listChangeListener);
};
}
use of com.canoo.platform.core.functional.Subscription in project dolphin-platform by canoo.
the class MBeanRegistry method register.
/**
* Register the given MBean based on the given name
* @param mBean the bean
* @param name the name
* @return the subscription that can be used to unregister the bean
*/
public Subscription register(final Object mBean, final String name) {
try {
if (mbeanSupport.get()) {
final ObjectName objectName = new ObjectName(name);
server.registerMBean(mBean, objectName);
return new Subscription() {
@Override
public void unsubscribe() {
try {
server.unregisterMBean(objectName);
} catch (JMException e) {
throw new RuntimeException("Can not unsubscribe!", e);
}
}
};
} else {
return new Subscription() {
@Override
public void unsubscribe() {
}
};
}
} catch (Exception e) {
throw new RuntimeException("Can not register MBean!", e);
}
}
Aggregations