From 5bd4e3c091c2d309243b258eb014014db284f025 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 1 Jun 2014 08:09:06 +0200 Subject: [PATCH] BUG-614: Remove xtendisms Generated xtend code is quite verbose and sometimes the logic gets quite obfuscated. This patch brings the code more in-line to what a programmer might write. Change-Id: I9a04012a2ba106082e9b1eef25bd9cb6958736cd Signed-off-by: Robert Varga --- .../impl/GeneratedListenerRegistration.java | 26 ++-- .../impl/GenericNotificationRegistration.java | 21 ++-- .../binding/impl/NotificationBrokerImpl.java | 100 +++++----------- .../sal/binding/impl/NotifyTask.java | 111 +++++++----------- 4 files changed, 98 insertions(+), 160 deletions(-) diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/GeneratedListenerRegistration.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/GeneratedListenerRegistration.java index cc8471f7b4..5325ed3c3a 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/GeneratedListenerRegistration.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/GeneratedListenerRegistration.java @@ -12,26 +12,28 @@ import org.opendaylight.yangtools.concepts.AbstractObjectRegistration; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.NotificationListener; -public class GeneratedListenerRegistration extends AbstractObjectRegistration implements ListenerRegistration { - private final NotificationInvoker _invoker; - - public NotificationInvoker getInvoker() { - return this._invoker; - } +import com.google.common.base.Preconditions; +class GeneratedListenerRegistration extends AbstractObjectRegistration implements ListenerRegistration { private NotificationBrokerImpl notificationBroker; + private final NotificationInvoker invoker; public GeneratedListenerRegistration(final NotificationListener instance, final NotificationInvoker invoker, final NotificationBrokerImpl broker) { super(instance); - this._invoker = invoker; - this.notificationBroker = broker; + this.invoker = Preconditions.checkNotNull(invoker); + this.notificationBroker = Preconditions.checkNotNull(broker); + } + + public NotificationInvoker getInvoker() { + // There is a race with NotificationBrokerImpl: + // the invoker can be closed here + return invoker; } @Override protected void removeRegistration() { - this.notificationBroker.unregisterListener(this); - this.notificationBroker = null; - NotificationInvoker _invoker = this.getInvoker(); - _invoker.close(); + notificationBroker.unregisterListener(this); + notificationBroker = null; + invoker.close(); } } diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/GenericNotificationRegistration.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/GenericNotificationRegistration.java index 956783130c..448adfa02e 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/GenericNotificationRegistration.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/GenericNotificationRegistration.java @@ -12,24 +12,25 @@ import org.opendaylight.yangtools.concepts.AbstractObjectRegistration; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.binding.Notification; -public class GenericNotificationRegistration extends AbstractObjectRegistration> implements ListenerRegistration> { - private final Class _type; - - public Class getType() { - return this._type; - } +import com.google.common.base.Preconditions; +class GenericNotificationRegistration extends AbstractObjectRegistration> implements ListenerRegistration> { + private final Class type; private NotificationBrokerImpl notificationBroker; public GenericNotificationRegistration(final Class type, final NotificationListener instance, final NotificationBrokerImpl broker) { super(instance); - this._type = type; - this.notificationBroker = broker; + this.type = Preconditions.checkNotNull(type); + this.notificationBroker = Preconditions.checkNotNull(broker); + } + + public Class getType() { + return type; } @Override protected void removeRegistration() { - this.notificationBroker.unregisterListener(this); - this.notificationBroker = null; + notificationBroker.unregisterListener(this); + notificationBroker = null; } } diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationBrokerImpl.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationBrokerImpl.java index 9efd48eabd..5c7d924d34 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationBrokerImpl.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/NotificationBrokerImpl.java @@ -7,7 +7,6 @@ */ package org.opendaylight.controller.sal.binding.impl; -import java.util.Collection; import java.util.Collections; import java.util.Set; import java.util.concurrent.ExecutorService; @@ -27,7 +26,6 @@ import org.opendaylight.yangtools.yang.binding.Notification; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.collect.HashMultimap; import com.google.common.collect.ImmutableSet; @@ -35,7 +33,6 @@ import com.google.common.collect.ImmutableSet.Builder; import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; -import com.google.common.collect.SetMultimap; public class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(NotificationBrokerImpl.class); @@ -43,20 +40,12 @@ public class NotificationBrokerImpl implements NotificationProviderService, Auto private final ListenerRegistry interestListeners = ListenerRegistry.create(); - private final Multimap,NotificationListener> listeners; + private final Multimap, NotificationListener> listeners = + Multimaps.synchronizedSetMultimap(HashMultimap., NotificationListener>create()); private ExecutorService executor; - public NotificationBrokerImpl() { - HashMultimap,NotificationListener> _create = HashMultimap., NotificationListener>create(); - SetMultimap,NotificationListener> _synchronizedSetMultimap = Multimaps., NotificationListener>synchronizedSetMultimap(_create); - this.listeners = _synchronizedSetMultimap; - } - @Deprecated public NotificationBrokerImpl(final ExecutorService executor) { - HashMultimap,NotificationListener> _create = HashMultimap., NotificationListener>create(); - SetMultimap,NotificationListener> _synchronizedSetMultimap = Multimaps., NotificationListener>synchronizedSetMultimap(_create); - this.listeners = _synchronizedSetMultimap; this.setExecutor(executor); } @@ -64,25 +53,18 @@ public class NotificationBrokerImpl implements NotificationProviderService, Auto this.executor = Preconditions.checkNotNull(executor); } - public Iterable> getNotificationTypes(final Notification notification) { - Class _class = notification.getClass(); - Class[] _interfaces = _class.getInterfaces(); - final Function1,Boolean> _function = new Function1,Boolean>() { + public Iterable> getNotificationTypes(final Notification notification) { + Class[] _interfaces = notification.getClass().getInterfaces(); + final Function1, Boolean> _function = new Function1, Boolean>() { @Override - public Boolean apply(final Class it) { - boolean _and = false; - boolean _notEquals = (!Objects.equal(it, Notification.class)); - if (!_notEquals) { - _and = false; - } else { - boolean _isAssignableFrom = Notification.class.isAssignableFrom(it); - _and = (_notEquals && _isAssignableFrom); + public Boolean apply(final Class it) { + if (Notification.class.equals(it)) { + return false; } - return Boolean.valueOf(_and); + return Notification.class.isAssignableFrom(it); } }; - Iterable> _filter = IterableExtensions.>filter(((Iterable>)Conversions.doWrapArray(_interfaces)), _function); - return _filter; + return IterableExtensions.filter(((Iterable>)Conversions.doWrapArray(_interfaces)), _function); } @Override @@ -92,38 +74,32 @@ public class NotificationBrokerImpl implements NotificationProviderService, Auto @Override public void publish(final Notification notification, final ExecutorService service) { - final Iterable> allTypes = this.getNotificationTypes(notification); - Iterable> listenerToNotify = Collections.>emptySet(); - for (final Class type : allTypes) { - Collection> _get = this.listeners.get(((Class) type)); - Iterable> _plus = Iterables.>concat(listenerToNotify, _get); - listenerToNotify = _plus; + Iterable> listenerToNotify = Collections.emptySet(); + for (final Class type : getNotificationTypes(notification)) { + listenerToNotify = Iterables.concat(listenerToNotify, listeners.get(((Class) type))); } - final Function1,NotifyTask> _function = new Function1,NotifyTask>() { + final Function1,NotifyTask> _function = new Function1, NotifyTask>() { @Override - public NotifyTask apply(final NotificationListener it) { - NotifyTask _notifyTask = new NotifyTask(it, notification); - return _notifyTask; + public NotifyTask apply(final NotificationListener it) { + return new NotifyTask(it, notification); } }; - Iterable _map = IterableExtensions., NotifyTask>map(listenerToNotify, _function); - final Set tasks = IterableExtensions.toSet(_map); + final Set tasks = IterableExtensions.toSet( + IterableExtensions., NotifyTask>map(listenerToNotify, _function)); this.submitAll(executor, tasks); } - public ImmutableSet> submitAll(final ExecutorService service, final Set tasks) { + private ImmutableSet> submitAll(final ExecutorService service, final Set tasks) { final Builder> ret = ImmutableSet.>builder(); for (final NotifyTask task : tasks) { - Future _submit = service.submit(task); - ret.add(_submit); + ret.add(service.submit(task)); } return ret.build(); } @Override public Registration> registerNotificationListener(final Class notificationType, final NotificationListener listener) { - GenericNotificationRegistration _genericNotificationRegistration = new GenericNotificationRegistration(notificationType, listener, this); - final GenericNotificationRegistration reg = _genericNotificationRegistration; + final GenericNotificationRegistration reg = new GenericNotificationRegistration(notificationType, listener, this); this.listeners.put(notificationType, listener); this.announceNotificationSubscription(notificationType); return reg; @@ -143,33 +119,22 @@ public class NotificationBrokerImpl implements NotificationProviderService, Auto @Override public Registration registerNotificationListener(final org.opendaylight.yangtools.yang.binding.NotificationListener listener) { final NotificationInvoker invoker = SingletonHolder.INVOKER_FACTORY.invokerFor(listener); - Set> _supportedNotifications = invoker.getSupportedNotifications(); - for (final Class notifyType : _supportedNotifications) { - { - NotificationListener _invocationProxy = invoker.getInvocationProxy(); - this.listeners.put(notifyType, _invocationProxy); - this.announceNotificationSubscription(notifyType); - } + for (final Class notifyType : invoker.getSupportedNotifications()) { + listeners.put(notifyType, invoker.getInvocationProxy()); + announceNotificationSubscription(notifyType); } - GeneratedListenerRegistration _generatedListenerRegistration = new GeneratedListenerRegistration(listener, invoker, this); - final GeneratedListenerRegistration registration = _generatedListenerRegistration; - return (registration); + + return new GeneratedListenerRegistration(listener, invoker, this); } - protected boolean unregisterListener(final GenericNotificationRegistration reg) { - Class _type = reg.getType(); - NotificationListener _instance = reg.getInstance(); - boolean _remove = this.listeners.remove(_type, _instance); - return _remove; + protected boolean unregisterListener(final GenericNotificationRegistration reg) { + return listeners.remove(reg.getType(), reg.getInstance()); } protected void unregisterListener(final GeneratedListenerRegistration reg) { - NotificationInvoker _invoker = reg.getInvoker(); - Set> _supportedNotifications = _invoker.getSupportedNotifications(); - for (final Class notifyType : _supportedNotifications) { - NotificationInvoker _invoker_1 = reg.getInvoker(); - NotificationListener _invocationProxy = _invoker_1.getInvocationProxy(); - this.listeners.remove(notifyType, _invocationProxy); + final NotificationInvoker invoker = reg.getInvoker(); + for (final Class notifyType : invoker.getSupportedNotifications()) { + this.listeners.remove(notifyType, invoker.getInvocationProxy()); } } @@ -180,8 +145,7 @@ public class NotificationBrokerImpl implements NotificationProviderService, Auto @Override public ListenerRegistration registerInterestListener(final NotificationInterestListener interestListener) { final ListenerRegistration registration = this.interestListeners.register(interestListener); - Set> _keySet = this.listeners.keySet(); - for (final Class notification : _keySet) { + for (final Class notification : listeners.keySet()) { interestListener.onNotificationSubscribtion(notification); } return registration; diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/NotifyTask.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/NotifyTask.java index 9471616f01..b0dd2a90f7 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/NotifyTask.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/NotifyTask.java @@ -9,90 +9,59 @@ package org.opendaylight.controller.sal.binding.impl; import java.util.concurrent.Callable; -import org.eclipse.xtext.xbase.lib.Exceptions; -import org.eclipse.xtext.xbase.lib.Functions.Function0; -import org.eclipse.xtext.xbase.lib.util.ToStringHelper; import org.opendaylight.controller.sal.binding.api.NotificationListener; import org.opendaylight.yangtools.yang.binding.Notification; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class NotifyTask implements Callable { - private static Logger log = new Function0() { - @Override - public Logger apply() { - Logger _logger = LoggerFactory.getLogger(NotifyTask.class); - return _logger; - } - }.apply(); +import com.google.common.base.Objects; +import com.google.common.base.Preconditions; - @SuppressWarnings("rawtypes") - private final NotificationListener _listener; +class NotifyTask implements Callable { + private static final Logger LOG = LoggerFactory.getLogger(NotifyTask.class); - public NotificationListener getListener() { - return this._listener; - } + private final NotificationListener listener; + private final Notification notification; - private final Notification _notification; + public NotifyTask(final NotificationListener listener, final Notification notification) { + this.listener = Preconditions.checkNotNull(listener); + this.notification = Preconditions.checkNotNull(notification); + } - public Notification getNotification() { - return this._notification; + @SuppressWarnings("unchecked") + private NotificationListener getListener() { + return (NotificationListener)listener; } @Override public Object call() { + if (LOG.isDebugEnabled()) { + LOG.debug("Delivering notification {} to {}", notification, listener); + } else { + LOG.trace("Delivering notification {} to {}", notification.getClass().getName(), listener); + } + try { - boolean _isDebugEnabled = NotifyTask.log.isDebugEnabled(); - if (_isDebugEnabled) { - Notification _notification = this.getNotification(); - NotificationListener _listener = this.getListener(); - NotifyTask.log.debug("Delivering notification {} to {}", _notification, _listener); - } else { - Notification _notification_1 = this.getNotification(); - Class _class = _notification_1.getClass(); - String _name = _class.getName(); - NotificationListener _listener_1 = this.getListener(); - NotifyTask.log.trace("Delivering notification {} to {}", _name, _listener_1); - } - NotificationListener _listener_2 = this.getListener(); - Notification _notification_2 = this.getNotification(); - _listener_2.onNotification(_notification_2); - boolean _isDebugEnabled_1 = NotifyTask.log.isDebugEnabled(); - if (_isDebugEnabled_1) { - Notification _notification_3 = this.getNotification(); - NotificationListener _listener_3 = this.getListener(); - NotifyTask.log.debug("Notification delivered {} to {}", _notification_3, _listener_3); - } else { - Notification _notification_4 = this.getNotification(); - Class _class_1 = _notification_4.getClass(); - String _name_1 = _class_1.getName(); - NotificationListener _listener_4 = this.getListener(); - NotifyTask.log.trace("Notification delivered {} to {}", _name_1, _listener_4); - } - } catch (final Throwable _t) { - if (_t instanceof Exception) { - final Exception e = (Exception)_t; - NotificationListener _listener_5 = this.getListener(); - NotifyTask.log.error("Unhandled exception thrown by listener: {}", _listener_5, e); - } else { - throw Exceptions.sneakyThrow(_t); - } + getListener().onNotification(notification); + } catch (final Exception e) { + LOG.error("Unhandled exception thrown by listener: {}", listener, e); + } + + if (LOG.isDebugEnabled()) { + LOG.debug("Notification delivered {} to {}", notification, listener); + } else { + LOG.trace("Notification delivered {} to {}", notification.getClass().getName(), listener); } - return null; - } - public NotifyTask(final NotificationListener listener, final Notification notification) { - super(); - this._listener = listener; - this._notification = notification; + return null; } @Override public int hashCode() { final int prime = 31; int result = 1; - result = prime * result + ((_listener== null) ? 0 : _listener.hashCode()); - result = prime * result + ((_notification== null) ? 0 : _notification.hashCode()); + result = prime * result + ((listener== null) ? 0 : listener.hashCode()); + result = prime * result + ((notification== null) ? 0 : notification.hashCode()); return result; } @@ -105,22 +74,24 @@ public class NotifyTask implements Callable { if (getClass() != obj.getClass()) return false; NotifyTask other = (NotifyTask) obj; - if (_listener == null) { - if (other._listener != null) + if (listener == null) { + if (other.listener != null) return false; - } else if (!_listener.equals(other._listener)) + } else if (!listener.equals(other.listener)) return false; - if (_notification == null) { - if (other._notification != null) + if (notification == null) { + if (other.notification != null) return false; - } else if (!_notification.equals(other._notification)) + } else if (!notification.equals(other.notification)) return false; return true; } @Override public String toString() { - String result = new ToStringHelper().toString(this); - return result; + return Objects.toStringHelper(this) + .add("listener", listener) + .add("notification", notification.getClass()) + .toString(); } } -- 2.36.6