Remove Optional from NamespaceBehaviour 75/105275/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 6 Apr 2023 11:52:05 +0000 (13:52 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 6 Apr 2023 12:45:21 +0000 (14:45 +0200)
The criterion-based getFrom() method is unlike all others in that it
returns an Optional. Switch to returning a @Nullable.

JIRA: YANGTOOLS-1204
Change-Id: I5100f4ddac239fb8bbe8c92fa11bae1eb754db83
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java
parser/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/NamespaceBehaviour.java

index 3bfc5cc1d197936dd398d572dd31c5656f57c7ef..42b5842de0614fc98d48b3b98d7d3f64c2324ce2 100644 (file)
@@ -613,9 +613,8 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     final <K, V> void onNamespaceItemAddedAction(final ParserNamespace<K, V> type,
             final ModelProcessingPhase phase, final NamespaceKeyCriterion<K> criterion,
             final OnNamespaceItemAdded listener) {
-        final Optional<Entry<K, V>> existing = getFromNamespace(type, criterion);
-        if (existing.isPresent()) {
-            final Entry<K, V> entry = existing.get();
+        final var entry = getFromNamespace(type, criterion);
+        if (entry != null) {
             LOG.debug("Listener on {} criterion {} found a pre-existing match: {}", type, criterion, entry);
             waitForPhase(entry.getValue(), type, phase, criterion, listener);
             return;
@@ -638,14 +637,15 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
 
     final <K, V> void selectMatch(final ParserNamespace<K, V> type, final NamespaceKeyCriterion<K> criterion,
             final OnNamespaceItemAdded listener) {
-        final Optional<Entry<K, V>> optMatch = getFromNamespace(type, criterion);
-        checkState(optMatch.isPresent(), "Failed to find a match for criterion %s in namespace %s node %s", criterion,
-            type, this);
-        final Entry<K, V> match = optMatch.get();
+        final var match = getFromNamespace(type, criterion);
+        if (match == null) {
+            throw new IllegalStateException(
+                "Failed to find a match for criterion %s in namespace %s node %s".formatted(criterion, type, this));
+        }
         listener.namespaceItemAdded(StatementContextBase.this, type, match.getKey(), match.getValue());
     }
 
-    private <K, V> Optional<Entry<K, V>> getFromNamespace(final ParserNamespace<K, V> type,
+    private <K, V> @Nullable Entry<K, V> getFromNamespace(final ParserNamespace<K, V> type,
             final NamespaceKeyCriterion<K> criterion) {
         return getNamespaceBehaviour(type).getFrom(this, criterion);
     }
index bbc334c3358404c97d8fba24ebeb507ff4c2a9bb..6d03452f39c23d393e1757392fe405e7fea86ae2 100644 (file)
@@ -7,15 +7,15 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.meta;
 
+import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects;
 import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.base.Verify;
 import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceStorage.StorageType;
 
 /**
@@ -114,11 +114,11 @@ public abstract class NamespaceBehaviour<K, V> {
      * @param criterion selection criterion
      * @return Selected mapping, if available.
      */
-    public final Optional<Entry<K, V>> getFrom(final NamespaceStorage storage,
+    public final @Nullable Entry<K, V> getFrom(final NamespaceStorage storage,
             final NamespaceKeyCriterion<K> criterion) {
         final var mappings = getAllFrom(storage);
         if (mappings == null) {
-            return Optional.empty();
+            return null;
         }
 
         Entry<K, V> match = null;
@@ -131,15 +131,15 @@ public abstract class NamespaceBehaviour<K, V> {
                         continue;
                     }
 
-                    Verify.verify(selected == key, "Criterion %s selected invalid key %s from candidates [%s %s]",
-                            selected, match.getKey(), key);
+                    verify(selected == key, "Criterion %s selected invalid key %s from candidates [%s %s]", selected,
+                        match.getKey(), key);
                 }
 
                 match = entry;
             }
         }
 
-        return Optional.ofNullable(match);
+        return match;
     }
 
     /**