Remove SchemaSourceRegistration 25/109225/3
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 8 Dec 2023 13:34:15 +0000 (14:34 +0100)
committerRobert Varga <nite@hq.sk>
Fri, 8 Dec 2023 16:26:16 +0000 (16:26 +0000)
Previous patch has touched yang.model.repo.spi. Since we are in the
area, also remove SchemaSourceRegistration to keep the API consistent.

JIRA: YANGTOOLS-1551
Change-Id: Iba1ea385c8f72433410c283a523207d2d3d77dfb
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
12 files changed:
yang/yang-repo-fs/src/test/java/org/opendaylight/yangtools/yang/model/repo/fs/FilesystemSchemaSourceCacheTest.java
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/AbstractSchemaSourceCache.java
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceRegistration.java [deleted file]
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCache.java
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistration.java
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java [deleted file]
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistry.java
yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformer.java
yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCacheTest.java
yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistrationTest.java
yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SoftSchemaSourceCacheTest.java

index 08bbc03d34510c3f9f324281434d91ec54dec8ff..e6dd9515dae420ea4884118e5d57dc9831c872cf 100644 (file)
@@ -41,11 +41,11 @@ import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.mockito.junit.jupiter.MockitoSettings;
 import org.mockito.quality.Strictness;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
-import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
 
 @ExtendWith(MockitoExtension.class)
@@ -54,7 +54,7 @@ public class FilesystemSchemaSourceCacheTest {
     @Mock
     public SchemaSourceRegistry registry;
     @Mock
-    public SchemaSourceRegistration<?> registration;
+    public Registration registration;
 
     public File storageDir;
 
index 7b596362100cbc06f57fa33be1fd7c2b484cca34..9d95628b50ab17f758f987e90c41cf66acf41291 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.model.repo.spi;
 
+import static java.util.Objects.requireNonNull;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
 
 import com.google.common.annotations.Beta;
@@ -25,6 +26,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
 import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
@@ -48,7 +50,7 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
      */
     @GuardedBy("this")
     private final Map<SourceIdentifier, ListMultimap<Class<? extends SchemaSourceRepresentation>,
-            AbstractSchemaSourceRegistration<?>>> sources = new HashMap<>();
+            SchemaSourceRegistration>> sources = new HashMap<>();
 
     /*
      * Schema source listeners.
@@ -56,27 +58,25 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
     @GuardedBy("this")
     private final List<SchemaListenerRegistration> listeners = new ArrayList<>();
 
-    @SuppressWarnings("unchecked")
-    private static <T extends SchemaSourceRepresentation> ListenableFuture<T> fetchSource(
-            final SourceIdentifier id, final Iterator<AbstractSchemaSourceRegistration<?>> it) {
-        final AbstractSchemaSourceRegistration<?> reg = it.next();
+    private static <T extends SchemaSourceRepresentation> ListenableFuture<T> fetchSource(final SourceIdentifier id,
+            final Iterator<SchemaSourceRegistration> it) {
+        final var reg = it.next();
+        @SuppressWarnings("unchecked")
+        final var provider = (SchemaSourceProvider<T>) reg.provider();
 
-        return Futures.catchingAsync(((SchemaSourceProvider<T>)reg.getProvider()).getSource(id), Throwable.class,
-            input -> {
-                LOG.debug("Failed to acquire source from {}", reg, input);
-
-                if (it.hasNext()) {
-                    return fetchSource(id, it);
-                }
-
-                throw new MissingSchemaSourceException("All available providers exhausted", id, input);
-            }, MoreExecutors.directExecutor());
+        return Futures.catchingAsync(provider.getSource(id), Throwable.class, input -> {
+            LOG.debug("Failed to acquire source from {}", reg, input);
+            if (it.hasNext()) {
+                return fetchSource(id, it);
+            }
+            throw new MissingSchemaSourceException("All available providers exhausted", id, input);
+        }, MoreExecutors.directExecutor());
     }
 
     @Override
     public <T extends SchemaSourceRepresentation> ListenableFuture<T> getSchemaSource(final SourceIdentifier id,
             final Class<T> representation) {
-        final ArrayList<AbstractSchemaSourceRegistration<?>> sortedSchemaSourceRegistrations;
+        final ArrayList<SchemaSourceRegistration> sortedSchemaSourceRegistrations;
 
         synchronized (this) {
             final var srcs = sources.get(id);
@@ -117,8 +117,8 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
         return fetchSourceFuture;
     }
 
-    private synchronized <T extends SchemaSourceRepresentation> void addSource(final PotentialSchemaSource<T> source,
-            final AbstractSchemaSourceRegistration<T> reg) {
+    private synchronized void addSource(final SchemaSourceRegistration reg) {
+        final var source = reg.getInstance();
         sources.computeIfAbsent(source.getSourceIdentifier(), ignored -> ArrayListMultimap.create())
             .put(source.getRepresentation(), reg);
 
@@ -128,8 +128,8 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
         }
     }
 
-    private synchronized <T extends SchemaSourceRepresentation> void removeSource(final PotentialSchemaSource<?> source,
-            final SchemaSourceRegistration<?> reg) {
+    private synchronized void removeSource(final SchemaSourceRegistration reg) {
+        final var source = reg.getInstance();
         final var m = sources.get(source.getSourceIdentifier());
         if (m != null) {
             m.remove(source.getRepresentation(), reg);
@@ -145,23 +145,16 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
     }
 
     @Override
-    public <T extends SchemaSourceRepresentation> SchemaSourceRegistration<T> registerSchemaSource(
+    public <T extends SchemaSourceRepresentation> Registration registerSchemaSource(
             final SchemaSourceProvider<? super T> provider, final PotentialSchemaSource<T> source) {
-        final var src = source.cachedReference();
-        final var ret = new AbstractSchemaSourceRegistration<>(provider, src) {
-            @Override
-            protected void removeRegistration() {
-                removeSource(src, this);
-            }
-        };
-
-        addSource(src, ret);
+        final var ret = new SchemaSourceRegistration(source.cachedReference(), provider);
+        addSource(ret);
         return ret;
     }
 
     @Override
     public Registration registerSchemaSourceListener(final SchemaSourceListener listener) {
-        final SchemaListenerRegistration ret = new SchemaListenerRegistration(listener);
+        final var ret = new SchemaListenerRegistration(listener);
 
         synchronized (this) {
             final var col = new ArrayList<PotentialSchemaSource<?>>();
@@ -180,8 +173,8 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
     }
 
     private final class SchemaListenerRegistration extends AbstractObjectRegistration<SchemaSourceListener> {
-        SchemaListenerRegistration(final SchemaSourceListener instance) {
-            super(instance);
+        SchemaListenerRegistration(final SchemaSourceListener listener) {
+            super(listener);
         }
 
         @Override
@@ -190,15 +183,33 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche
         }
     }
 
-    private static final class SchemaProviderCostComparator implements Comparator<AbstractSchemaSourceRegistration<?>>,
-            Serializable {
+    private final class SchemaSourceRegistration extends AbstractObjectRegistration<PotentialSchemaSource<?>> {
+        private final @NonNull SchemaSourceProvider<?> provider;
+
+        SchemaSourceRegistration(final PotentialSchemaSource<?> source, final SchemaSourceProvider<?> provider) {
+            super(source);
+            this.provider = requireNonNull(provider);
+        }
+
+        @NonNull SchemaSourceProvider<?> provider() {
+            return provider;
+        }
+
+        @Override
+        protected void removeRegistration() {
+            removeSource(this);
+        }
+    }
+
+    private static final class SchemaProviderCostComparator
+            implements Comparator<SchemaSourceRegistration>, Serializable {
         static final SchemaProviderCostComparator INSTANCE = new SchemaProviderCostComparator();
 
         @java.io.Serial
         private static final long serialVersionUID = 1L;
 
         @Override
-        public int compare(final AbstractSchemaSourceRegistration<?> o1, final AbstractSchemaSourceRegistration<?> o2) {
+        public int compare(final SchemaSourceRegistration o1, final SchemaSourceRegistration o2) {
             return o1.getInstance().getCost() - o2.getInstance().getCost();
         }
 
index 1390f8b05f7f355bd0471a2ea9d88d1a7889eb26..0d6e1776b66b6d3bb91cf49219876cabf600cd0e 100644 (file)
@@ -9,15 +9,15 @@ package org.opendaylight.yangtools.yang.model.repo.spi;
 
 import static java.util.Objects.requireNonNull;
 
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
 
 /**
- * Abstract base class for cache-type SchemaSourceListeners. It needs to be
- * registered with a {@link SchemaSourceRegistry}, where it gets notifications
- * from. It performs filtering and {@link #offer(SchemaSourceRepresentation)}s
- * conforming sources to the subclass.
+ * Abstract base class for cache-type SchemaSourceListeners. It needs to be registered with a
+ * {@link SchemaSourceRegistry}, where it gets notifications from. It performs filtering and
+ * {@link #offer(SchemaSourceRepresentation)}s conforming sources to the subclass.
  *
  * @param <T> Cached schema source type.
  */
@@ -44,20 +44,17 @@ public abstract class AbstractSchemaSourceCache<T extends SchemaSourceRepresenta
     protected abstract void offer(T source);
 
     /**
-     * Register the presence of a cached schema source with the consumer. Subclasses
-     * need to call this method once they have cached a schema source representation,
-     * or when they have determined they have a schema source is available -- like
-     * when a persistent cache reads its cache index.
+     * Register the presence of a cached schema source with the consumer. Subclasses need to call this method once they
+     * have cached a schema source representation, or when they have determined they have a schema source is available
+     * -- like when a persistent cache reads its cache index.
      *
      * @param sourceIdentifier Source identifier
-     * @return schema source registration, which the subclass needs to
-     *         {@link SchemaSourceRegistration#close()} once it expunges the source
-     *         from the cache.
+     * @return schema source registration, which the subclass needs to {@link Registration#close()} once it expunges the
+     *         source from the cache.
      */
-    protected final SchemaSourceRegistration<T> register(final SourceIdentifier sourceIdentifier) {
-        final PotentialSchemaSource<T> src = PotentialSchemaSource.create(sourceIdentifier, representation,
-                cost.getValue());
-        return consumer.registerSchemaSource(this, src);
+    protected final Registration register(final SourceIdentifier sourceIdentifier) {
+        return consumer.registerSchemaSource(this, PotentialSchemaSource.create(sourceIdentifier, representation,
+            cost.getValue()));
     }
 
     @Override
diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceRegistration.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceRegistration.java
deleted file mode 100644 (file)
index 865abf9..0000000
+++ /dev/null
@@ -1,34 +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 static java.util.Objects.requireNonNull;
-
-import com.google.common.base.MoreObjects.ToStringHelper;
-import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
-
-public abstract class AbstractSchemaSourceRegistration<T extends SchemaSourceRepresentation>
-        extends AbstractObjectRegistration<PotentialSchemaSource<T>> implements SchemaSourceRegistration<T> {
-    private final SchemaSourceProvider<?> provider;
-
-    protected AbstractSchemaSourceRegistration(final SchemaSourceProvider<?> provider,
-            final PotentialSchemaSource<T> source) {
-        super(source);
-        this.provider = requireNonNull(provider);
-    }
-
-    public final SchemaSourceProvider<?> getProvider() {
-        return provider;
-    }
-
-    @Override
-    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-        return super.addToStringAttributes(toStringHelper).add("provider", provider);
-    }
-}
index 4720e37519dc2dafa2833b550c15e2278b25cd76..65f038e090deedc53ef6cb11990eb0bb86b2c81d 100644 (file)
@@ -83,7 +83,7 @@ public final class GuavaSchemaSourceCache<T extends SchemaSourceRepresentation>
         cache.put(srcId, source);
         final var reg = register(srcId);
 
-        final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<>(source, queue) {
+        final var ref = new FinalizablePhantomReference<>(source, queue) {
             @Override
             public void finalizeReferent() {
                 reg.close();
index b409df0376109f6b5b931246dbecd87bbbb2cff7..45c541c06cf69742870c9af21870bc5458ac882a 100644 (file)
@@ -10,11 +10,13 @@ package org.opendaylight.yangtools.yang.model.repo.spi;
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.Objects.requireNonNull;
 
+import org.opendaylight.yangtools.concepts.Registration;
+
 final class RefcountedRegistration {
-    private final SchemaSourceRegistration<?> reg;
+    private final Registration reg;
     private int refcount = 1;
 
-    RefcountedRegistration(final SchemaSourceRegistration<?> reg) {
+    RefcountedRegistration(final Registration reg) {
         this.reg = requireNonNull(reg);
     }
 
diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java
deleted file mode 100644 (file)
index 781bfd5..0000000
+++ /dev/null
@@ -1,22 +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.ObjectRegistration;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
-
-/**
- * Registration of a schema source.
- */
-@Beta
-public interface SchemaSourceRegistration<T extends SchemaSourceRepresentation>
-        extends ObjectRegistration<PotentialSchemaSource<T>> {
-    @Override
-    void close();
-}
index f7cb93c5f28d436dd283bb7c3ce0866d7d12b3e8..246353351b846f3a7d18a87a9733fec2621a76dc 100644 (file)
@@ -26,10 +26,10 @@ public interface SchemaSourceRegistry {
      * @param <T> schema source representation type
      * @param provider Resolver which can potentially resolve the identifier
      * @param source Schema source details
-     * @return A registration handle. Invoking {@link SchemaSourceRegistration#close()} will cancel the registration.
+     * @return A registration handle. Invoking {@link Registration#close()} will cancel the registration.
      */
-    <T extends SchemaSourceRepresentation> SchemaSourceRegistration<T> registerSchemaSource(
-            SchemaSourceProvider<? super T> provider, PotentialSchemaSource<T> source);
+    <T extends SchemaSourceRepresentation> Registration registerSchemaSource(SchemaSourceProvider<? super T> provider,
+        PotentialSchemaSource<T> source);
 
     /**
      * Register a schema source listener. The listener will be notified as new sources and their representations become
index 30764f5741157822046237670bdfe79c8031c9aa..21872ae53030b78841347194614a5f06c2469b91 100644 (file)
@@ -21,7 +21,6 @@ import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 
 public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
         implements SchemaSourceListener, SchemaSourceProvider<D> {
-
     @FunctionalInterface
     public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
             extends AsyncFunction<S, D> {
@@ -58,8 +57,8 @@ public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D ext
 
     @Override
     public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
-        for (PotentialSchemaSource<?> src : sources) {
-            final Class<?> rep = src.getRepresentation();
+        for (var src : sources) {
+            final var rep = src.getRepresentation();
             if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
                 registerSource(src);
             }
@@ -68,24 +67,22 @@ public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D ext
 
     @Override
     public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
-        final Class<?> rep = source.getRepresentation();
+        final var rep = source.getRepresentation();
         if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
             unregisterSource(source);
         }
     }
 
     private void registerSource(final PotentialSchemaSource<?> src) {
-        RefcountedRegistration reg = availableSources.get(src);
+        final var reg = availableSources.get(src);
         if (reg != null) {
             reg.incRef();
             return;
         }
 
-        final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
-                src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
-
-        final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
-        availableSources.put(src, new RefcountedRegistration(r));
+        final var newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
+            src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
+        availableSources.put(src, new RefcountedRegistration(consumer.registerSchemaSource(this, newSrc)));
     }
 
     private void unregisterSource(final PotentialSchemaSource<?> src) {
index 0b5f885bbae5cab232aa73feea793ec1b5edf4e9..9e84d86e1c8003c5385c29d75a463a92008af10f 100644 (file)
@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@@ -37,7 +38,7 @@ class GuavaSchemaSourceCacheTest {
     @Mock
     public SchemaSourceRegistry registry;
     @Mock
-    public SchemaSourceRegistration<?> registration;
+    public Registration registration;
 
     @Test
     void inMemorySchemaSourceCacheTest1() {
index a3124d2f55d0a09e0dd0b38265b27aa79e5ff5ca..17c0c26023a45bb1c3b9bd8269da308325d01ccd 100644 (file)
@@ -15,11 +15,12 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
+import org.opendaylight.yangtools.concepts.Registration;
 
 @ExtendWith(MockitoExtension.class)
 class RefcountedRegistrationTest {
     @Mock
-    private SchemaSourceRegistration<?> reg;
+    private Registration reg;
 
     @Test
     void refcountDecTrue() {
index 2dae5fb1a71fd19216b063c1919f71a65f12ba54..cf9c3ad2088a891d055523361f4179cea7fc75c9 100644 (file)
@@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@@ -33,7 +34,7 @@ class SoftSchemaSourceCacheTest {
     @Mock
     private SchemaSourceRegistry registry;
     @Mock
-    private SchemaSourceRegistration<?> registration;
+    private Registration registration;
 
     @Test
     void inMemorySchemaSourceCacheTest() {