Merge "Extract typedefs to local variable to avoid error in xtend generated class."
authorTony Tkacik <ttkacik@cisco.com>
Mon, 23 Jun 2014 16:16:52 +0000 (16:16 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 23 Jun 2014 16:16:52 +0000 (16:16 +0000)
code-generator/binding-type-provider/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java
yang/yang-model-api/pom.xml
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaPath.java

index 1dec469b954480df49ed442d788fa38f9d53b46b..47b8ce1ca8d59787107914408b31749692640d89 100644 (file)
@@ -93,6 +93,10 @@ import com.google.common.collect.Sets;
 import com.google.common.io.BaseEncoding;
 
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
+import com.google.common.io.BaseEncoding;
+
 public final class TypeProviderImpl implements TypeProvider {
     private static final Pattern NUMBERS_PATTERN = Pattern.compile("[0-9]+\\z");
 
@@ -165,6 +169,7 @@ public final class TypeProviderImpl implements TypeProvider {
      * @see TypeProvider#javaTypeForYangType(String)
      */
     @Override
+    @Deprecated
     public Type javaTypeForYangType(final String type) {
         return BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(type);
     }
@@ -372,7 +377,7 @@ public final class TypeProviderImpl implements TypeProvider {
      *             if <code>extendTypeDef</code> equal null
      */
     private TypeDefinition<?> baseTypeDefForExtendedType(final TypeDefinition<?> extendTypeDef) {
-        Preconditions.checkArgument(extendTypeDef != null, "Type Definiition reference cannot be NULL!");
+        Preconditions.checkArgument(extendTypeDef != null, "Type Definition reference cannot be NULL!");
         final TypeDefinition<?> baseTypeDef = extendTypeDef.getBaseType();
         if (baseTypeDef == null) {
             return extendTypeDef;
@@ -412,9 +417,7 @@ public final class TypeProviderImpl implements TypeProvider {
         final String strXPath = xpath.toString();
 
         if (strXPath != null) {
-            if (strXPath.contains("[")) {
-                returnType = Types.typeForClass(Object.class);
-            } else {
+            if (strXPath.indexOf('[') == -1) {
                 final Module module = findParentModule(schemaContext, parentNode);
                 if (module != null) {
                     final SchemaNode dataNode;
@@ -432,6 +435,8 @@ public final class TypeProviderImpl implements TypeProvider {
                         returnType = resolveTypeFromDataSchemaNode(dataNode);
                     }
                 }
+            } else {
+                returnType = Types.typeForClass(Object.class);
             }
         }
         if (returnType == null) {
@@ -1394,7 +1399,7 @@ public final class TypeProviderImpl implements TypeProvider {
             String packageName = packageNameForGeneratedType(basePackageName, type.getPath());
             String className = packageName + "." + BindingMapping.getClassName(typeQName);
             sb.insert(0, "new " + className + "(");
-            sb.insert(sb.length(), ")");
+            sb.insert(sb.length(), ')');
         }
 
         return sb.toString();
@@ -1415,7 +1420,7 @@ public final class TypeProviderImpl implements TypeProvider {
                 sb.append(", ");
             }
         }
-        sb.append("}");
+        sb.append('}');
         return sb.toString();
     }
 
@@ -1428,7 +1433,11 @@ public final class TypeProviderImpl implements TypeProvider {
             }
         });
         StringBuilder sb = new StringBuilder();
-        sb.append(isExt ? "" : "new " + className + "(");
+        if (!isExt) {
+            sb.append("new ");
+            sb.append(className);
+            sb.append('(');
+        }
         for (int i = 0; i < bits.size(); i++) {
             if (bits.get(i).getName().equals(defaultValue)) {
                 sb.append(true);
@@ -1439,7 +1448,9 @@ public final class TypeProviderImpl implements TypeProvider {
                 sb.append(", ");
             }
         }
-        sb.append(isExt ? "" : ")");
+        if (!isExt) {
+            sb.append(')');
+        }
         return sb.toString();
     }
 
@@ -1459,9 +1470,7 @@ public final class TypeProviderImpl implements TypeProvider {
         final String strXPath = xpath.toString();
 
         if (strXPath != null) {
-            if (strXPath.contains("[")) {
-                return "new java.lang.Object()";
-            } else {
+            if (strXPath.indexOf('[') == -1) {
                 final Module module = findParentModule(schemaContext, parentNode);
                 if (module != null) {
                     final SchemaNode dataNode;
@@ -1473,6 +1482,8 @@ public final class TypeProviderImpl implements TypeProvider {
                     String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, parentNode.getDefault());
                     return result;
                 }
+            } else {
+                return "new java.lang.Object()";
             }
         }
 
@@ -1535,12 +1546,11 @@ public final class TypeProviderImpl implements TypeProvider {
 
     private String union(final String className, final String defaultValue, final LeafSchemaNode node) {
         StringBuilder sb = new StringBuilder();
-        sb.append("new " + className + "(");
-        sb.append("\"");
+        sb.append("new ");
+        sb.append(className);
+        sb.append("(\"");
         sb.append(defaultValue);
-        sb.append("\"");
-        sb.append(".toCharArray()");
-        sb.append(")");
+        sb.append("\".toCharArray())");
         return sb.toString();
     }
 
index 10d7a13fdafcc2c7ce1bbd9c4d8ab7a0f0a2ea99..ff3c31bbae76bc8bcac5cc9962f18dc50320d6de 100644 (file)
     <description>${project.artifactId}</description>
 
     <dependencies>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>util</artifactId>
+        </dependency>
         <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>yang-common</artifactId>
index 6d13ee5f2f45ba2d60ae591050033a5ef25a3cd8..9c369f63d6388f13c6e0824d11d7475b9f878e0a 100644 (file)
@@ -11,7 +11,10 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.util.HashCodeBuilder;
 import org.opendaylight.yangtools.yang.common.QName;
 
 /**
@@ -19,27 +22,38 @@ import org.opendaylight.yangtools.yang.common.QName;
  * Represents unique path to the every node inside the module.
  *
  */
-public class SchemaPath {
+public class SchemaPath implements Immutable {
     /**
      * Shared instance of the conceptual root schema node.
      */
-    public static final SchemaPath ROOT = new SchemaPath(ImmutableList.<QName> of(), true, null);
+    public static final SchemaPath ROOT = new SchemaPath(Collections.<QName>emptyList(), true, Boolean.TRUE.hashCode());
 
     /**
      * Shared instance of the "same" relative schema node.
      */
-    public static final SchemaPath SAME = new SchemaPath(ImmutableList.<QName> of(), false, null);
+    public static final SchemaPath SAME = new SchemaPath(Collections.<QName>emptyList(), false, Boolean.FALSE.hashCode());
 
     /**
      * List of QName instances which represents complete path to the node.
      */
-    private final ImmutableList<QName> path;
+    private final Iterable<QName> path;
 
     /**
      * Boolean value which represents type of schema path (relative or
      * absolute).
      */
-    private final Boolean absolute;
+    private final boolean absolute;
+
+    /**
+     * Cached hash code. We can use this since we are immutable.
+     */
+    private final int hash;
+
+    /**
+     * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
+     * is invoked.
+     */
+    private ImmutableList<QName> legacyPath;
 
     /**
      * Constructs new instance of this class with the concrete path.
@@ -55,7 +69,15 @@ public class SchemaPath {
      */
     @Deprecated
     public SchemaPath(final List<QName> path, final boolean absolute) {
-        this(ImmutableList.copyOf(path), absolute, null);
+        this(ImmutableList.copyOf(path), absolute, Boolean.valueOf(absolute).hashCode());
+    }
+
+    private ImmutableList<QName> getLegacyPath() {
+        if (legacyPath == null) {
+            legacyPath = ImmutableList.copyOf(path);
+        }
+
+        return legacyPath;
     }
 
     /**
@@ -68,12 +90,13 @@ public class SchemaPath {
      */
     @Deprecated
     public List<QName> getPath() {
-        return path;
+        return getLegacyPath();
     }
 
-    private SchemaPath(final ImmutableList<QName> path, final boolean absolute, final Void dummy) {
+    private SchemaPath(final Iterable<QName> path, final boolean absolute, final int hash) {
         this.path = Preconditions.checkNotNull(path);
         this.absolute = absolute;
+        this.hash = hash;
     }
 
     /**
@@ -89,11 +112,8 @@ public class SchemaPath {
      * @return A SchemaPath instance.
      */
     public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
-        if (Iterables.isEmpty(path)) {
-            return absolute ? ROOT : SAME;
-        } else {
-            return new SchemaPath(ImmutableList.copyOf(path), absolute, null);
-        }
+        final SchemaPath parent = absolute ? ROOT : SAME;
+        return parent.createChild(path);
     }
 
     /**
@@ -109,7 +129,20 @@ public class SchemaPath {
      * @return A SchemaPath instance.
      */
     public static SchemaPath create(final boolean absolute, final QName... path) {
-       return create(Arrays.asList(path), absolute);
+        return create(Arrays.asList(path), absolute);
+    }
+
+    private SchemaPath trustedCreateChild(final Iterable<QName> relative) {
+        if (Iterables.isEmpty(relative)) {
+            return this;
+        }
+
+        final HashCodeBuilder<QName> b = new HashCodeBuilder<>(hash);
+        for (QName p : relative) {
+            b.addArgument(p);
+        }
+
+        return new SchemaPath(Iterables.concat(path, relative), absolute, b.toInstance());
     }
 
     /**
@@ -122,7 +155,8 @@ public class SchemaPath {
         if (Iterables.isEmpty(relative)) {
             return this;
         }
-        return create(Iterables.concat(path, relative), absolute);
+
+        return trustedCreateChild(ImmutableList.copyOf(relative));
     }
 
     /**
@@ -133,7 +167,7 @@ public class SchemaPath {
      */
     public SchemaPath createChild(final SchemaPath relative) {
         Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
-        return createChild(relative.path);
+        return trustedCreateChild(relative.path);
     }
 
     /**
@@ -167,7 +201,7 @@ public class SchemaPath {
      *         path from the schema node towards the root.
      */
     public Iterable<QName> getPathTowardsRoot() {
-        return path.reverse();
+        return getLegacyPath().reverse();
     }
 
     /**
@@ -182,18 +216,7 @@ public class SchemaPath {
 
     @Override
     public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + absolute.hashCode();
-
-        // TODO: Temporary fix for Bug 1076 - hash computation
-        // Which adds same behaviour as using List.hashCode().
-        int pathHash = 1;
-        for (Object o : path) {
-            pathHash = prime * pathHash + o.hashCode();
-        }
-        result = prime * result + pathHash;
-        return result;
+        return hash;
     }
 
     @Override