Mass-migrate to Objects.requireNonNull()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / YangTextSchemaContextResolver.java
index 5520c5e3e9dff9e78be5de9f36f516c44dead501..1955136d169564484f44575d8c78fde1f804994f 100644 (file)
@@ -8,15 +8,17 @@
 package org.opendaylight.yangtools.yang.parser.repo;
 
 import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.base.Preconditions;
 import com.google.common.base.Verify;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Multimap;
-import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.io.IOException;
 import java.io.InputStream;
@@ -28,7 +30,7 @@ import java.util.concurrent.ConcurrentLinkedDeque;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
-import javax.annotation.Nonnull;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
@@ -49,8 +51,8 @@ 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;
 import org.opendaylight.yangtools.yang.model.repo.util.InMemorySchemaSourceCache;
-import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
-import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.ASTSchemaSource;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToASTTransformer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -70,8 +72,8 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
     private volatile Object contextVersion = version;
 
     private YangTextSchemaContextResolver(final SchemaRepository repository, final SchemaSourceRegistry registry) {
-        this.repository = Preconditions.checkNotNull(repository);
-        this.registry = Preconditions.checkNotNull(registry);
+        this.repository = requireNonNull(repository);
+        this.registry = requireNonNull(registry);
 
         final TextToASTTransformer t = TextToASTTransformer.create(repository, registry);
         transReg = registry.registerSchemaSourceListener(t);
@@ -94,7 +96,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
      * @throws IOException when the URL is not readable
      * @throws SchemaSourceException When parsing encounters general error
      */
-    public YangTextSchemaSourceRegistration registerSource(@Nonnull final YangTextSchemaSource source)
+    public @NonNull YangTextSchemaSourceRegistration registerSource(final @NonNull YangTextSchemaSource source)
             throws SchemaSourceException, IOException, YangSyntaxErrorException {
         checkArgument(source != null);
 
@@ -163,8 +165,8 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
      * @throws IOException when the URL is not readable
      * @throws SchemaSourceException When parsing encounters general error
      */
-    public YangTextSchemaSourceRegistration registerSource(@Nonnull final URL url) throws SchemaSourceException,
-            IOException, YangSyntaxErrorException {
+    public @NonNull YangTextSchemaSourceRegistration registerSource(final @NonNull URL url)
+            throws SchemaSourceException, IOException, YangSyntaxErrorException {
         checkArgument(url != null, "Supplied URL must not be null");
 
         final String path = url.getPath();
@@ -183,10 +185,10 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
         });
     }
 
-    private static SourceIdentifier guessSourceIdentifier(final String fileName) {
+    private static SourceIdentifier guessSourceIdentifier(final @NonNull String fileName) {
         try {
             return YangTextSchemaSource.identifierFromFilename(fileName);
-        } catch (IllegalArgumentException e) {
+        } catch (final IllegalArgumentException e) {
             LOG.warn("Invalid file name format in '{}'", fileName, e);
             return RevisionSourceIdentifier.create(fileName);
         }
@@ -236,9 +238,9 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
                 try {
                     sc = Optional.of(f.get());
                     break;
-                } catch (InterruptedException e) {
-                    throw new RuntimeException("Interrupted while assembling schema context", e);
-                } catch (ExecutionException e) {
+                } catch (final InterruptedException e) {
+                    throw new IllegalStateException("Interrupted while assembling schema context", e);
+                } catch (final ExecutionException e) {
                     LOG.info("Failed to fully assemble schema context for {}", sources, e);
                     final Throwable cause = e.getCause();
                     Verify.verify(cause instanceof SchemaResolutionException);
@@ -260,17 +262,17 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
     }
 
     @Override
-    public synchronized ListenableFuture<YangTextSchemaSource> getSource(
+    public synchronized FluentFuture<YangTextSchemaSource> getSource(
             final SourceIdentifier sourceIdentifier) {
         final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
 
         LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
         if (ret.isEmpty()) {
-            return Futures.immediateFailedFuture(new MissingSchemaSourceException(
-                "URL for " + sourceIdentifier + " not registered", sourceIdentifier));
+            return immediateFailedFluentFuture(new MissingSchemaSourceException("URL for " + sourceIdentifier
+                + " not registered", sourceIdentifier));
         }
 
-        return Futures.immediateFuture(ret.iterator().next());
+        return immediateFluentFuture(ret.iterator().next());
     }
 
     /**
@@ -293,6 +295,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
     }
 
     @Beta
+    @SuppressWarnings("checkstyle:avoidHidingCauseException")
     public SchemaContext trySchemaContext(final StatementParserMode statementParserMode)
             throws SchemaResolutionException {
         final ListenableFuture<SchemaContext> future = repository
@@ -301,9 +304,9 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem
 
         try {
             return future.get();
-        } catch (InterruptedException e) {
-            throw new RuntimeException("Interrupted while waiting for SchemaContext assembly", e);
-        } catch (ExecutionException e) {
+        } catch (final InterruptedException e) {
+            throw new IllegalStateException("Interrupted while waiting for SchemaContext assembly", e);
+        } catch (final ExecutionException e) {
             final Throwable cause = e.getCause();
             if (cause instanceof SchemaResolutionException) {
                 throw (SchemaResolutionException) cause;