*/
package org.opendaylight.yangtools.sal.binding.yang.types;
-import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import java.math.BigDecimal;
import java.math.BigInteger;
-import java.util.Collections;
-import java.util.List;
import java.util.Map;
import org.opendaylight.yangtools.binding.generator.util.Types;
import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
-import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
-import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
-import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
public final class BaseYangTypes {
/**
};
private static <T extends Number> Restrictions singleRangeRestrictions(final T min, final T max) {
- return new Restrictions() {
- @Override
- public boolean isEmpty() {
- return false;
- }
-
- @Override
- public List<RangeConstraint> getRangeConstraints() {
- return Collections.singletonList(BaseConstraints.newRangeConstraint(min, max,
- Optional.<String> absent(), Optional.<String> absent()));
- }
-
- @Override
- public List<PatternConstraint> getPatternConstraints() {
- return Collections.emptyList();
- }
-
- @Override
- public List<LengthConstraint> getLengthConstraints() {
- return Collections.emptyList();
- }
- };
+ return Types.getDefaultRestrictions(min, max);
}
public static final class UnionType implements Type {
return "Union";
}
}
-
}
import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
+import org.opendaylight.yangtools.sal.binding.model.api.BaseTypeWithRestrictions;
import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
return result;
}
- /**
- * FIXME: Remove @Ignore annotation once the bug https://bugs.opendaylight.org/show_bug.cgi?id=1862 is fixed
- */
- @Ignore
@Test
public void bug1862RestrictedTypedefTransformationTest() {
final TypeProvider provider = new TypeProviderImpl(schemaContext);
final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(leafType);
final Type result = provider.javaTypeForSchemaDefinitionType(leafType, leaf, restrictions);
assertNotNull(result);
- assertTrue(result instanceof GeneratedTransferObject);
- //TODO: complete test after bug 1862 is fixed
+ assertTrue(result instanceof BaseTypeWithRestrictions);
}
@Test
package org.opendaylight.yangtools.binding.generator.util;
import com.google.common.base.CharMatcher;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Iterables;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import javax.annotation.Nullable;
+import org.opendaylight.yangtools.sal.binding.model.api.BaseTypeWithRestrictions;
import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
import org.opendaylight.yangtools.sal.binding.model.api.WildcardType;
import org.opendaylight.yangtools.yang.binding.Augmentable;
import org.opendaylight.yangtools.yang.binding.Augmentation;
+import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
+import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
public final class Types {
private static final CacheLoader<Class<?>, ConcreteType> TYPE_LOADER =
public static ConcreteType typeForClass(final Class<?> cls, final Restrictions restrictions) {
if (restrictions != null) {
- return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+ if (restrictions instanceof DefaultRestrictions) {
+ return new ConcreteTypeImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+ } else {
+ return new BaseTypeWithRestrictionsImpl(cls.getPackage().getName(), cls.getSimpleName(), restrictions);
+ }
} else {
return typeForClass(cls);
}
}
}
+ /**
+ *
+ * Represents concrete JAVA type with changed restriction values.
+ *
+ */
+ private static final class BaseTypeWithRestrictionsImpl extends AbstractBaseType implements BaseTypeWithRestrictions {
+ private final Restrictions restrictions;
+
+ /**
+ * Creates instance of this class with package <code>pkName</code> and
+ * with the type name <code>name</code>.
+ *
+ * @param pkName
+ * string with package name
+ * @param name
+ * string with the name of the type
+ */
+ private BaseTypeWithRestrictionsImpl(final String pkName, final String name, final Restrictions restrictions) {
+ super(pkName, name);
+ this.restrictions = Preconditions.checkNotNull(restrictions);
+ }
+
+ @Override
+ public Restrictions getRestrictions() {
+ return restrictions;
+ }
+ }
+
/**
*
* Represents parametrized JAVA type.
}
}
-}
+ public static <T extends Number> DefaultRestrictions getDefaultRestrictions(final T min, final T max) {
+ return new DefaultRestrictions(min, max);
+ }
+
+ private static final class DefaultRestrictions<T extends Number> implements Restrictions {
+ private final T min;
+ private final T max;
+ private final List<RangeConstraint> rangeConstraints;
+
+ private DefaultRestrictions(final T min, final T max) {
+ this.min = Preconditions.checkNotNull(min);
+ this.max = Preconditions.checkNotNull(max);
+ this.rangeConstraints = Collections.singletonList(BaseConstraints.newRangeConstraint(min, max, Optional
+ .<String>absent(), Optional.<String>absent()));
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return false;
+ }
+
+ @Override
+ public List<RangeConstraint> getRangeConstraints() {
+ return rangeConstraints;
+ }
+
+ @Override
+ public List<PatternConstraint> getPatternConstraints() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<LengthConstraint> getLengthConstraints() {
+ return Collections.emptyList();
+ }
+ }
+}
\ No newline at end of file