Ditch use of Optional in EventFormatter 30/108830/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Nov 2023 23:16:14 +0000 (00:16 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 2 Nov 2023 23:37:13 +0000 (00:37 +0100)
These are internal implementation classes, switch to using @Nullable
instead.

Change-Id: Id8d0a5e096e324e7e9a03bc84196c39e340f7147
JIRA: NETCONF-1102
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/AbstractNotificationListenerAdaptor.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/EventFormatter.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/streams/ListenerAdapter.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/streams/JsonNotificationListenerTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/streams/XmlNotificationListenerTest.java

index acca7d2fc1cba46a2b9d701cee04ffa4be962858..8cb0ce2bafc600e7817f9f3d084a3ce54d54d60e 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.restconf.nb.rfc8040.streams;
 
 import java.time.Instant;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.dom.api.DOMEvent;
 import org.opendaylight.mdsal.dom.api.DOMNotification;
@@ -45,14 +44,16 @@ abstract class AbstractNotificationListenerAdaptor extends AbstractCommonSubscri
     @SuppressWarnings("checkstyle:IllegalCatch")
     public final void onNotification(final DOMNotification notification) {
         final var eventInstant = notification instanceof DOMEvent domEvent ? domEvent.getEventInstant() : Instant.now();
-        final Optional<String> maybeOutput;
+        final String data;
         try {
-            maybeOutput = formatter().eventData(effectiveModel(), notification, eventInstant);
+            data = formatter().eventData(effectiveModel(), notification, eventInstant);
         } catch (Exception e) {
             LOG.error("Failed to process notification {}", notification, e);
             return;
         }
-        maybeOutput.ifPresent(this::post);
+        if (data != null) {
+            post(data);
+        }
     }
 
     abstract @NonNull EffectiveModelContext effectiveModel();
index f5e03ec9c822dcee97dfc6de78d32f03d9de538b..a5b4280230dd03a6aec47d374ec107b1c615a914 100644 (file)
@@ -14,7 +14,6 @@ import java.time.Instant;
 import java.time.OffsetDateTime;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
-import java.util.Optional;
 import javax.xml.XMLConstants;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
@@ -24,6 +23,7 @@ import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.w3c.dom.Document;
@@ -73,12 +73,9 @@ abstract class EventFormatter<T> implements Immutable {
         filter = xpath.compile(xpathFilter);
     }
 
-    final Optional<String> eventData(final EffectiveModelContext schemaContext, final T input, final Instant now)
+    final @Nullable String eventData(final EffectiveModelContext schemaContext, final T input, final Instant now)
             throws Exception {
-        if (!filterMatches(schemaContext, input, now)) {
-            return Optional.empty();
-        }
-        return Optional.ofNullable(createText(textParams, schemaContext, input, now));
+        return filterMatches(schemaContext, input, now) ? createText(textParams, schemaContext, input, now) : null;
     }
 
     /**
index 29c14f546bfa702f2fb58b2819447d0e8505b7e9..d995fe5eea0bbf44b808984e240b97a5383c922b 100644 (file)
@@ -13,7 +13,6 @@ import com.google.common.base.MoreObjects.ToStringHelper;
 import java.time.Instant;
 import java.util.Collection;
 import java.util.List;
-import java.util.Optional;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
@@ -71,17 +70,17 @@ public class ListenerAdapter extends AbstractCommonSubscriber<Collection<DataTre
     @SuppressWarnings("checkstyle:IllegalCatch")
     public void onDataTreeChanged(final List<DataTreeCandidate> dataTreeCandidates) {
         final var now = Instant.now();
-        final Optional<String> maybeData;
+        final String data;
         try {
-            maybeData = formatter().eventData(databindProvider.currentContext().modelContext(), dataTreeCandidates,
-                now);
+            data = formatter().eventData(databindProvider.currentContext().modelContext(), dataTreeCandidates, now);
         } catch (final Exception e) {
             LOG.error("Failed to process notification {}",
                     dataTreeCandidates.stream().map(Object::toString).collect(Collectors.joining(",")), e);
             return;
         }
-
-        maybeData.ifPresent(this::post);
+        if (data != null) {
+            post(data);
+        }
     }
 
     /**
index b4920de9086fdc2caaa981fe1940de2a032f0267..2584bd57cebe906c08f17b40e72a0fcfad46eed3 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.streams;
 
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -164,8 +165,9 @@ public class JsonNotificationListenerTest extends AbstractNotificationListenerTe
 
     private String prepareJson(final DOMNotification notificationData, final QName schemaPathNotifi)
             throws Exception {
-        final var notifiAdapter = listenersBroker.registerNotificationListener(MODEL_CONTEXT,
-            ImmutableSet.of(schemaPathNotifi), NotificationOutputType.JSON);
-        return notifiAdapter.formatter().eventData(MODEL_CONTEXT, notificationData, Instant.now()).orElseThrow();
+        final var ret = listenersBroker.registerNotificationListener(MODEL_CONTEXT, ImmutableSet.of(schemaPathNotifi),
+            NotificationOutputType.JSON).formatter().eventData(MODEL_CONTEXT, notificationData, Instant.now());
+        assertNotNull(ret);
+        return ret;
     }
 }
index 1c4a5716ce44d20bd8ab62bf32afa76d83fe8975..ac35836f5e1a846974cecfa5867c19e8b7a92f76 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.streams;
 
+import static org.junit.Assert.assertNotNull;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -156,8 +157,9 @@ public class XmlNotificationListenerTest extends AbstractNotificationListenerTes
 
     private String prepareXmlResult(final DOMNotification notificationData, final QName schemaPathNotifi)
             throws Exception {
-        final var notifiAdapter = listenersBroker.registerNotificationListener(MODEL_CONTEXT,
-            ImmutableSet.of(schemaPathNotifi), NotificationOutputType.XML);
-        return notifiAdapter.formatter().eventData(MODEL_CONTEXT, notificationData, Instant.now()).orElseThrow();
+        final var ret = listenersBroker.registerNotificationListener(MODEL_CONTEXT, ImmutableSet.of(schemaPathNotifi),
+            NotificationOutputType.XML).formatter().eventData(MODEL_CONTEXT, notificationData, Instant.now());
+        assertNotNull(ret);
+        return ret;
     }
 }