Use lambdas instead of anonymous classes
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / ASTSchemaSource.java
index 5ff245962e11d94856e82c40834863959186fc2a..5e3ab5eaabf926f71a22c701e1a682c394f15d54 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ * 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
@@ -8,14 +9,14 @@ package org.opendaylight.yangtools.yang.parser.util;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Function;
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
-
 import javax.annotation.Nonnull;
-
 import org.antlr.v4.runtime.ParserRuleContext;
+import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
+import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
 
@@ -31,35 +32,25 @@ import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
  */
 @Beta
 public final class ASTSchemaSource implements SchemaSourceRepresentation {
-    public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = new Function<ASTSchemaSource, SourceIdentifier>() {
-        @Override
-        public SourceIdentifier apply(final ASTSchemaSource input) {
-            return input.getIdentifier();
-        }
-    };
-    public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = new Function<ASTSchemaSource, YangModelDependencyInfo>() {
-        @Override
-        public YangModelDependencyInfo apply(final ASTSchemaSource input) {
-            return input.getDependencyInformation();
-        }
-    };
-    public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = new Function<ASTSchemaSource, ParserRuleContext>() {
-        @Override
-        public ParserRuleContext apply(final ASTSchemaSource input) {
-            return input.getAST();
-        }
-    };
+    @Deprecated
+    public static final Function<ASTSchemaSource, SourceIdentifier> GET_IDENTIFIER = ASTSchemaSource::getIdentifier;
+    @Deprecated
+    public static final Function<ASTSchemaSource, SourceIdentifier> GET_SEMVER_IDENTIFIER = ASTSchemaSource::getSemVerIdentifier;
+    @Deprecated
+    public static final Function<ASTSchemaSource, YangModelDependencyInfo> GET_DEPINFO = ASTSchemaSource::getDependencyInformation;
+    @Deprecated
+    public static final Function<ASTSchemaSource, ParserRuleContext> GET_AST = ASTSchemaSource::getAST;
 
     private final YangModelDependencyInfo depInfo;
     private final ParserRuleContext tree;
     private final SourceIdentifier id;
-    private final String text;
+    private final SemVerSourceIdentifier semVerId;
 
-    private ASTSchemaSource(final @Nonnull SourceIdentifier id, @Nonnull final ParserRuleContext tree, final @Nonnull YangModelDependencyInfo depInfo, final @Nonnull String text) {
+    private ASTSchemaSource(@Nonnull final SourceIdentifier id, @Nonnull final SemVerSourceIdentifier semVerId, @Nonnull final ParserRuleContext tree, @Nonnull final YangModelDependencyInfo depInfo) {
         this.depInfo = Preconditions.checkNotNull(depInfo);
         this.tree = Preconditions.checkNotNull(tree);
         this.id = Preconditions.checkNotNull(id);
-        this.text = Preconditions.checkNotNull(text);
+        this.semVerId = Preconditions.checkNotNull(semVerId);
     }
 
     /**
@@ -71,36 +62,65 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
      * @return A new representation instance.
      * @throws YangSyntaxErrorException if we fail to extract dependency information.
      */
-    public static final ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree) throws YangSyntaxErrorException {
+    public static ASTSchemaSource create(@Nonnull final String name, @Nonnull final ParserRuleContext tree) throws YangSyntaxErrorException {
         final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
-        final SourceIdentifier id = new SourceIdentifier(depInfo.getName(), Optional.of(depInfo.getFormattedRevision()));
-        return new ASTSchemaSource(id, tree, depInfo, null);
+        final SourceIdentifier id = getSourceId(depInfo);
+        final SemVerSourceIdentifier semVerId = getSemVerSourceId(depInfo);
+        return new ASTSchemaSource(id, semVerId, tree, depInfo);
+    }
+
+    private static SourceIdentifier getSourceId(final YangModelDependencyInfo depInfo) {
+        final String name = depInfo.getName();
+        return depInfo.getFormattedRevision() == null
+                ? RevisionSourceIdentifier.create(name)
+                : RevisionSourceIdentifier.create(name, depInfo.getFormattedRevision());
+    }
+
+    private static SemVerSourceIdentifier getSemVerSourceId(final YangModelDependencyInfo depInfo) {
+        return depInfo.getFormattedRevision() == null ? SemVerSourceIdentifier.create(depInfo.getName(), depInfo
+                .getSemanticVersion().or(Module.DEFAULT_SEMANTIC_VERSION)) : SemVerSourceIdentifier.create(
+                depInfo.getName(), depInfo.getFormattedRevision(),
+                depInfo.getSemanticVersion().or(Module.DEFAULT_SEMANTIC_VERSION));
     }
 
     /**
      * Create a new instance of AST representation for a abstract syntax tree,
      * performing minimal semantic analysis to acquire dependency information.
      *
-     * @param name YANG source name. Used only for error reporting.
-     * @param tree ANTLR abstract syntax tree
+     * @param identifier
+     *            SourceIdentifier of yang schema source.
+     * @param tree
+     *            ANTLR abstract syntax tree
+     * @param text
+     *            YANG text source
      * @return A new representation instance.
-     * @throws YangSyntaxErrorException if we fail to extract dependency information.
-     *
-     * @deprecated Migration only, will be removed as soon as the migration is completed.
+     * @throws YangSyntaxErrorException
+     *             if we fail to extract dependency information.
      */
-    @Deprecated
-    public static final ASTSchemaSource create(final @Nonnull String name, final @Nonnull ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
-        final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(name, tree);
-        final SourceIdentifier id = new SourceIdentifier(depInfo.getName(), Optional.of(depInfo.getFormattedRevision()));
-        return new ASTSchemaSource(id, tree, depInfo, text);
-    }
+    public static ASTSchemaSource create(@Nonnull final SourceIdentifier identifier,
+            @Nonnull final ParserRuleContext tree, final String text) throws YangSyntaxErrorException {
+        final YangModelDependencyInfo depInfo = YangModelDependencyInfo.fromAST(identifier.getName(), tree);
+        final SourceIdentifier id = getSourceId(depInfo);
+
+        final SemVerSourceIdentifier semVerId;
+        if (identifier instanceof SemVerSourceIdentifier && !depInfo.getSemanticVersion().isPresent()) {
+            semVerId = (SemVerSourceIdentifier) identifier;
+        } else {
+            semVerId = getSemVerSourceId(depInfo);
+        }
 
+        return new ASTSchemaSource(id, semVerId, tree, depInfo);
+    }
 
     @Override
     public SourceIdentifier getIdentifier() {
         return id;
     }
 
+    public SemVerSourceIdentifier getSemVerIdentifier() {
+        return semVerId;
+    }
+
     @Override
     public Class<? extends SchemaSourceRepresentation> getType() {
         return ASTSchemaSource.class;
@@ -111,7 +131,7 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
      *
      * @return Underlying AST.
      */
-    public @Nonnull ParserRuleContext getAST() {
+    @Nonnull public ParserRuleContext getAST() {
         return tree;
     }
 
@@ -123,20 +143,7 @@ public final class ASTSchemaSource implements SchemaSourceRepresentation {
      *
      * @return Dependency information.
      */
-    public @Nonnull YangModelDependencyInfo getDependencyInformation() {
+    @Nonnull public YangModelDependencyInfo getDependencyInformation() {
         return depInfo;
     }
-
-    /**
-     * Return the semantically-equivalent text YANG text source.
-     *
-     * @return YANG text source
-     * @deprecated Used for migration purposes. Users are advised to use the
-     *             schema repository to acquire the representation of their
-     *             choice. Will be removed as soon as the migration is completed.
-     */
-    @Deprecated
-    public @Nonnull String getYangText() {
-        return text;
-    }
 }