*/
package org.opendaylight.yangtools.yang.model.util.type;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
import java.util.List;
-import javax.annotation.Nonnull;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.LengthRestrictedTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
/**
* Compatibility utilities for dealing with differences between the old parser's ExtendedType-driven type
@Deprecated
public final class CompatUtils {
private CompatUtils() {
- throw new UnsupportedOperationException();
+ // Hidden on purpose
}
/**
* @param leaf Leaf for which we are acquiring the type
* @return Potentially base type of the leaf type.
*/
- @Nonnull public static TypeDefinition<?> compatLeafType(@Nonnull final LeafSchemaNode leaf) {
- final TypeDefinition<?> leafType = leaf.getType();
- Preconditions.checkNotNull(leafType);
+ public static @NonNull TypeDefinition<?> compatType(final @NonNull TypedDataSchemaNode leaf) {
+ final TypeDefinition<?> leafType = requireNonNull(leaf.getType());
if (!leaf.getPath().equals(leafType.getPath())) {
// Old parser semantics, or no new default/units defined for this leaf
// We are dealing with a type generated for the leaf itself
final TypeDefinition<?> baseType = leafType.getBaseType();
- Preconditions.checkArgument(baseType != null, "Leaf %s has type for leaf, but no base type", leaf);
+ checkArgument(baseType != null, "Leaf %s has type for leaf, but no base type", leaf);
if (leaf.getPath().equals(baseType.getPath().getParent())) {
// Internal instantiation of a base YANG type (decimal64 and similar)
return baseTypeIfNotConstrained((DecimalTypeDefinition) leafType);
} else if (leafType instanceof InstanceIdentifierTypeDefinition) {
return baseTypeIfNotConstrained((InstanceIdentifierTypeDefinition) leafType);
- } else if (leafType instanceof IntegerTypeDefinition) {
- return baseTypeIfNotConstrained((IntegerTypeDefinition) leafType);
+ } else if (leafType instanceof Int8TypeDefinition) {
+ return baseTypeIfNotConstrained((Int8TypeDefinition) leafType);
+ } else if (leafType instanceof Int16TypeDefinition) {
+ return baseTypeIfNotConstrained((Int16TypeDefinition) leafType);
+ } else if (leafType instanceof Int32TypeDefinition) {
+ return baseTypeIfNotConstrained((Int32TypeDefinition) leafType);
+ } else if (leafType instanceof Int64TypeDefinition) {
+ return baseTypeIfNotConstrained((Int64TypeDefinition) leafType);
} else if (leafType instanceof StringTypeDefinition) {
return baseTypeIfNotConstrained((StringTypeDefinition) leafType);
- } else if (leafType instanceof UnsignedIntegerTypeDefinition) {
- return baseTypeIfNotConstrained((UnsignedIntegerTypeDefinition) leafType);
+ } else if (leafType instanceof Uint8TypeDefinition) {
+ return baseTypeIfNotConstrained((Uint8TypeDefinition) leafType);
+ } else if (leafType instanceof Uint16TypeDefinition) {
+ return baseTypeIfNotConstrained((Uint16TypeDefinition) leafType);
+ } else if (leafType instanceof Uint32TypeDefinition) {
+ return baseTypeIfNotConstrained((Uint32TypeDefinition) leafType);
+ } else if (leafType instanceof Uint64TypeDefinition) {
+ return baseTypeIfNotConstrained((Uint64TypeDefinition) leafType);
} else {
// Other types cannot be constrained, return the base type
return baseType;
}
}
- private static TypeDefinition<?> baseTypeIfNotConstrained(final BinaryTypeDefinition type) {
- final BinaryTypeDefinition base = type.getBaseType();
- return baseTypeIfNotConstrained(type, type.getLengthConstraints(), base, base.getLengthConstraints());
+ private static BinaryTypeDefinition baseTypeIfNotConstrained(final @NonNull BinaryTypeDefinition type) {
+ return baseTypeIfNotConstrained(type, type.getBaseType());
}
- private static TypeDefinition<?> baseTypeIfNotConstrained(final DecimalTypeDefinition type) {
- final DecimalTypeDefinition base = type.getBaseType();
- return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
+ private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull DecimalTypeDefinition type) {
+ return baseTypeIfNotConstrained(type, type.getBaseType());
}
- private static TypeDefinition<?> baseTypeIfNotConstrained(final InstanceIdentifierTypeDefinition type) {
+ private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull InstanceIdentifierTypeDefinition type) {
final InstanceIdentifierTypeDefinition base = type.getBaseType();
return type.requireInstance() == base.requireInstance() ? base : type;
}
- private static TypeDefinition<?> baseTypeIfNotConstrained(final IntegerTypeDefinition type) {
- final IntegerTypeDefinition base = type.getBaseType();
- return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
- }
-
- private static TypeDefinition<?> baseTypeIfNotConstrained(final StringTypeDefinition type) {
+ private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull StringTypeDefinition type) {
final StringTypeDefinition base = type.getBaseType();
final List<PatternConstraint> patterns = type.getPatternConstraints();
- final List<LengthConstraint> lengths = type.getLengthConstraints();
+ final Optional<LengthConstraint> optLengths = type.getLengthConstraint();
if ((patterns.isEmpty() || patterns.equals(base.getPatternConstraints()))
- && (lengths.isEmpty() || lengths.equals(base.getLengthConstraints()))) {
+ && (optLengths.isEmpty() || optLengths.equals(base.getLengthConstraint()))) {
return base;
}
return type;
}
- private static TypeDefinition<?> baseTypeIfNotConstrained(final UnsignedIntegerTypeDefinition type) {
- final UnsignedIntegerTypeDefinition base = type.getBaseType();
- return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
+ private static <T extends RangeRestrictedTypeDefinition<T, ?>> T baseTypeIfNotConstrained(final @NonNull T type) {
+ return baseTypeIfNotConstrained(type, type.getBaseType());
}
- private static TypeDefinition<?> baseTypeIfNotConstrained(final TypeDefinition<?> type,
- final List<?> typeConstraints, final TypeDefinition<?> base, final List<?> baseConstraints) {
- if (typeConstraints.isEmpty() || typeConstraints.equals(baseConstraints)) {
+ private static <T extends RangeRestrictedTypeDefinition<T, ?>> T baseTypeIfNotConstrained(final @NonNull T type,
+ final T base) {
+ final Optional<?> optConstraint = type.getRangeConstraint();
+ if (optConstraint.isEmpty()) {
return base;
}
- return type;
+ return optConstraint.equals(base.getRangeConstraint()) ? base : type;
+ }
+
+ private static <T extends LengthRestrictedTypeDefinition<T>> T baseTypeIfNotConstrained(final @NonNull T type,
+ final T base) {
+ final Optional<LengthConstraint> optConstraint = type.getLengthConstraint();
+ if (optConstraint.isEmpty()) {
+ return base;
+ }
+ return optConstraint.equals(base.getLengthConstraint()) ? base : type;
}
}