Disconnect SchemaSourceListener from EventListener 18/109218/5
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 7 Dec 2023 22:19:53 +0000 (23:19 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 8 Dec 2023 12:52:45 +0000 (13:52 +0100)
j.u.EventListener should really be tied to j.u.EventObject, which is not
useful for our purposes. Ditch the reference in SchemaSourceListener.

This ends up forcing us to abandon AbstractSchemaListenerRegistration,
which is fine, as we can just pull the functionality into an internal
class.

JIRA: YANGTOOLS-1551
Change-Id: Id439e9bac61a4ba991ed75d04c19ead7d94a72c3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaListenerRegistration.java [deleted file]
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaRepository.java
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaListenerRegistration.java [deleted file]
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceListener.java
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistry.java

index 8e087bf256bc535217edf8c4dc5fe7c7069e7b87..b3929828e354b3ffff810b7b605b865d6f94618c 100644 (file)
@@ -50,7 +50,6 @@ import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.GuavaSchemaSourceCache;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
-import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
@@ -70,9 +69,9 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
     private final AtomicReference<Optional<EffectiveModelContext>> currentSchemaContext =
             new AtomicReference<>(Optional.empty());
     private final GuavaSchemaSourceCache<YangIRSchemaSource> cache;
-    private final SchemaListenerRegistration transReg;
     private final SchemaSourceRegistry registry;
     private final SchemaRepository repository;
+    private final Registration transReg;
 
     private volatile Object version = new Object();
     private volatile Object contextVersion = version;
diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaListenerRegistration.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaListenerRegistration.java
deleted file mode 100644 (file)
index 488acbf..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.model.repo.spi;
-
-import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
-
-public abstract class AbstractSchemaListenerRegistration extends AbstractListenerRegistration<SchemaSourceListener>
-        implements SchemaListenerRegistration {
-    protected AbstractSchemaListenerRegistration(final SchemaSourceListener listener) {
-        super(listener);
-    }
-}
index 45befd23921733a0f0555331c86ee40fabe2282e..7b596362100cbc06f57fa33be1fd7c2b484cca34 100644 (file)
@@ -12,15 +12,12 @@ import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediate
 import com.google.common.annotations.Beta;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Multimap;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.io.Serializable;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -28,6 +25,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
@@ -80,20 +79,19 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
         final ArrayList<AbstractSchemaSourceRegistration<?>> sortedSchemaSourceRegistrations;
 
         synchronized (this) {
-            final ListMultimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> srcs =
-                sources.get(id);
+            final var srcs = sources.get(id);
             if (srcs == null) {
                 return immediateFailedFluentFuture(new MissingSchemaSourceException(
                     "No providers registered for source " + id, id));
             }
 
-            sortedSchemaSourceRegistrations = Lists.newArrayList(srcs.get(representation));
+            sortedSchemaSourceRegistrations = new ArrayList<>(srcs.get(representation));
         }
 
         // TODO, remove and make sources keep sorted multimap (e.g. ArrayListMultimap with SortedLists)
         sortedSchemaSourceRegistrations.sort(SchemaProviderCostComparator.INSTANCE);
 
-        final Iterator<AbstractSchemaSourceRegistration<?>> regs = sortedSchemaSourceRegistrations.iterator();
+        final var regs = sortedSchemaSourceRegistrations.iterator();
         if (!regs.hasNext()) {
             return immediateFailedFluentFuture(new MissingSchemaSourceException(
                         "No providers for source " + id + " representation " + representation + " available", id));
@@ -104,7 +102,7 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
         Futures.addCallback(fetchSourceFuture, new FutureCallback<T>() {
             @Override
             public void onSuccess(final T result) {
-                for (final SchemaListenerRegistration listener : listeners) {
+                for (var listener : listeners) {
                     listener.getInstance().schemaSourceEncountered(result);
                 }
             }
@@ -121,29 +119,22 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
 
     private synchronized <T extends SchemaSourceRepresentation> void addSource(final PotentialSchemaSource<T> source,
             final AbstractSchemaSourceRegistration<T> reg) {
-        ListMultimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> map =
-            sources.get(source.getSourceIdentifier());
-        if (map == null) {
-            map = ArrayListMultimap.create();
-            sources.put(source.getSourceIdentifier(), map);
-        }
-
-        map.put(source.getRepresentation(), reg);
+        sources.computeIfAbsent(source.getSourceIdentifier(), ignored -> ArrayListMultimap.create())
+            .put(source.getRepresentation(), reg);
 
-        final Collection<PotentialSchemaSource<?>> reps = Collections.singleton(source);
-        for (SchemaListenerRegistration l : listeners) {
+        final var reps = Collections.<PotentialSchemaSource<?>>singleton(source);
+        for (var l : listeners) {
             l.getInstance().schemaSourceRegistered(reps);
         }
     }
 
     private synchronized <T extends SchemaSourceRepresentation> void removeSource(final PotentialSchemaSource<?> source,
             final SchemaSourceRegistration<?> reg) {
-        final Multimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> m =
-            sources.get(source.getSourceIdentifier());
+        final var m = sources.get(source.getSourceIdentifier());
         if (m != null) {
             m.remove(source.getRepresentation(), reg);
 
-            for (SchemaListenerRegistration l : listeners) {
+            for (var l : listeners) {
                 l.getInstance().schemaSourceUnregistered(source);
             }
 
@@ -156,9 +147,8 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
     @Override
     public <T extends SchemaSourceRepresentation> SchemaSourceRegistration<T> registerSchemaSource(
             final SchemaSourceProvider<? super T> provider, final PotentialSchemaSource<T> source) {
-        final PotentialSchemaSource<T> src = source.cachedReference();
-
-        final AbstractSchemaSourceRegistration<T> ret = new AbstractSchemaSourceRegistration<>(provider, src) {
+        final var src = source.cachedReference();
+        final var ret = new AbstractSchemaSourceRegistration<>(provider, src) {
             @Override
             protected void removeRegistration() {
                 removeSource(src, this);
@@ -170,19 +160,13 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
     }
 
     @Override
-    public SchemaListenerRegistration registerSchemaSourceListener(final SchemaSourceListener listener) {
-        final SchemaListenerRegistration ret = new AbstractSchemaListenerRegistration(listener) {
-            @Override
-            protected void removeRegistration() {
-                listeners.remove(this);
-            }
-        };
+    public Registration registerSchemaSourceListener(final SchemaSourceListener listener) {
+        final SchemaListenerRegistration ret = new SchemaListenerRegistration(listener);
 
         synchronized (this) {
-            final Collection<PotentialSchemaSource<?>> col = new ArrayList<>();
-            for (Multimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> m
-                    : sources.values()) {
-                for (AbstractSchemaSourceRegistration<?> r : m.values()) {
+            final var col = new ArrayList<PotentialSchemaSource<?>>();
+            for (var m : sources.values()) {
+                for (var r : m.values()) {
                     col.add(r.getInstance());
                 }
             }
@@ -195,6 +179,17 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
         return ret;
     }
 
+    private final class SchemaListenerRegistration extends AbstractObjectRegistration<SchemaSourceListener> {
+        SchemaListenerRegistration(final SchemaSourceListener instance) {
+            super(instance);
+        }
+
+        @Override
+        protected void removeRegistration() {
+            listeners.remove(this);
+        }
+    }
+
     private static final class SchemaProviderCostComparator implements Comparator<AbstractSchemaSourceRegistration<?>>,
             Serializable {
         static final SchemaProviderCostComparator INSTANCE = new SchemaProviderCostComparator();
diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaListenerRegistration.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaListenerRegistration.java
deleted file mode 100644 (file)
index 7e480b9..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.model.repo.spi;
-
-import com.google.common.annotations.Beta;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-
-/**
- * Registration of a SchemaSourceListener.
- */
-@Beta
-public interface SchemaListenerRegistration extends ListenerRegistration<SchemaSourceListener> {
-
-}
index 71ca20307ffe3f0f18a5ecd61ec14045f249d203..5e07793fcd7984dfb3b7ad49a55e54dfd6589e51 100644 (file)
@@ -8,18 +8,18 @@
 package org.opendaylight.yangtools.yang.model.repo.spi;
 
 import com.google.common.annotations.Beta;
-import java.util.EventListener;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 
 /**
- * Listener for schema source lifecycle events.
+ * Listener on {@link SchemaSourceRepresentation} lifecycle.
  */
 @Beta
-public interface SchemaSourceListener extends EventListener {
+@NonNullByDefault
+public interface SchemaSourceListener {
     /**
-     * Invoked when the registry sees a concrete source. This callback is typically
-     * used by cache-type listeners, who intercept the source, store it locally and
-     * announce themselves as a provider of that particular schema source.
+     * Invoked when the registry sees a concrete source. This callback is typically used by cache-type listeners, who
+     * intercept the source, store it locally and announce themselves as a provider of that particular schema source.
      *
      * @param source Schema source
      */
index ad034f5e271093fba56f77c17ea9cc0a66818e75..f7cb93c5f28d436dd283bb7c3ce0866d7d12b3e8 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.model.repo.spi;
 
 import com.google.common.annotations.Beta;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 
@@ -35,7 +36,7 @@ public interface SchemaSourceRegistry {
      * available, subject to the provided filter.
      *
      * @param listener Schema source listener
-     * @return A registration handle. Invoking {@link SchemaListenerRegistration#close()} will cancel the registration.
+     * @return A registration handle. Invoking {@link Registration#close()} will cancel the registration.
      */
-    SchemaListenerRegistration registerSchemaSourceListener(SchemaSourceListener listener);
+    Registration registerSchemaSourceListener(SchemaSourceListener listener);
 }