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");
* @see TypeProvider#javaTypeForYangType(String)
*/
@Override
+ @Deprecated
public Type javaTypeForYangType(final String type) {
return BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(type);
}
* 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;
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;
returnType = resolveTypeFromDataSchemaNode(dataNode);
}
}
+ } else {
+ returnType = Types.typeForClass(Object.class);
}
}
if (returnType == null) {
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();
sb.append(", ");
}
}
- sb.append("}");
+ sb.append('}');
return sb.toString();
}
}
});
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);
sb.append(", ");
}
}
- sb.append(isExt ? "" : ")");
+ if (!isExt) {
+ sb.append(')');
+ }
return sb.toString();
}
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;
String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, parentNode.getDefault());
return result;
}
+ } else {
+ return "new java.lang.Object()";
}
}
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();
}
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;
/**
* 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.
*/
@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;
}
/**
*/
@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;
}
/**
* @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);
}
/**
* @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());
}
/**
if (Iterables.isEmpty(relative)) {
return this;
}
- return create(Iterables.concat(path, relative), absolute);
+
+ return trustedCreateChild(ImmutableList.copyOf(relative));
}
/**
*/
public SchemaPath createChild(final SchemaPath relative) {
Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
- return createChild(relative.path);
+ return trustedCreateChild(relative.path);
}
/**
* path from the schema node towards the root.
*/
public Iterable<QName> getPathTowardsRoot() {
- return path.reverse();
+ return getLegacyPath().reverse();
}
/**
@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