*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
-import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.YangVersion;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
+import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.UnresolvedNumber;
import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LengthConstraintEffectiveImpl;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.RangeConstraintEffectiveImpl;
/**
-* util class for manipulating YANG base and extended types implementation
+* Utility class for manipulating YANG base and extended types implementation.
*/
public final class TypeUtils {
public static final String UINT64 = "uint64";
public static final String UNION = "union";
- private static final Set<String> BUILT_IN_TYPES =
- ImmutableSet.of(BINARY, BITS, BOOLEAN, DECIMAL64, EMPTY, ENUMERATION, IDENTITY_REF, INSTANCE_IDENTIFIER,
- INT8, INT16, INT32, INT64, LEAF_REF, STRING, UINT8, UINT16, UINT32, UINT64, UNION);
+ private static final Map<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
+ .put(BINARY, BINARY)
+ .put(BITS, BITS)
+ .put(BOOLEAN, BOOLEAN)
+ .put(DECIMAL64, DECIMAL64)
+ .put(EMPTY, EMPTY)
+ .put(ENUMERATION, ENUMERATION)
+ .put(IDENTITY_REF,IDENTITY_REF)
+ .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
+ .put(INT8, INT8)
+ .put(INT16, INT16)
+ .put(INT32, INT32)
+ .put(INT64, INT64)
+ .put(LEAF_REF, LEAF_REF)
+ .put(STRING, STRING)
+ .put(UINT8, UINT8)
+ .put(UINT16, UINT16)
+ .put(UINT32, UINT32)
+ .put(UINT64, UINT64)
+ .put(UNION, UNION)
+ .build();
private static final Set<String> TYPE_BODY_STMTS = ImmutableSet.of(
DECIMAL64, ENUMERATION, LEAF_REF, IDENTITY_REF, BITS, UNION);
try {
return new BigInteger(value);
} catch (final NumberFormatException e) {
- throw new SourceException(String.format("Value %s is not a valid integer", value),
- ctx.getStatementSourceReference(), e);
+ throw new SourceException(ctx.getStatementSourceReference(), e, "Value %s is not a valid integer", value);
}
}
public static List<RangeConstraint> parseRangeListFromString(final StmtContext<?, ?, ?> ctx,
final String rangeArgument) {
- final Optional<String> description = Optional.absent();
- final Optional<String> reference = Optional.absent();
+ final Optional<String> description = Optional.empty();
+ final Optional<String> reference = Optional.empty();
final List<RangeConstraint> rangeConstraints = new ArrayList<>();
return rangeConstraints;
}
- public static List<LengthConstraint> parseLengthListFromString(final StmtContext<?, ?, ?> ctx,
+ public static List<ValueRange> parseLengthListFromString(final StmtContext<?, ?, ?> ctx,
final String lengthArgument) {
- final Optional<String> description = Optional.absent();
- final Optional<String> reference = Optional.absent();
-
- final List<LengthConstraint> lengthConstraints = new ArrayList<>();
+ final List<ValueRange> ranges = new ArrayList<>();
for (final String singleRange : PIPE_SPLITTER.split(lengthArgument)) {
- final Iterator<String> boundaries = TWO_DOTS_SPLITTER.splitToList(singleRange).iterator();
+ final Iterator<String> boundaries = TWO_DOTS_SPLITTER.split(singleRange).iterator();
final Number min = parseIntegerConstraintValue(ctx, boundaries.next());
final Number max;
max = parseIntegerConstraintValue(ctx, boundaries.next());
// if min larger than max then error
- Preconditions.checkArgument(compareNumbers(min, max) != 1,
- "Length constraint %s has descending order of boundaries; should be ascending. Statement source at %s",
- singleRange, ctx.getStatementSourceReference());
- Preconditions.checkArgument(!boundaries.hasNext(),
- "Wrong number of boundaries in length constraint %s. Statement source at %s", singleRange,
- ctx.getStatementSourceReference());
+ SourceException.throwIf(compareNumbers(min, max) == 1, ctx.getStatementSourceReference(),
+ "Length constraint %s has descending order of boundaries; should be ascending.",
+ singleRange);
+ SourceException.throwIf(boundaries.hasNext(), ctx.getStatementSourceReference(),
+ "Wrong number of boundaries in length constraint %s.", singleRange);
} else {
max = min;
}
// some of intervals overlapping
- if (lengthConstraints.size() > 1 && compareNumbers(min, Iterables.getLast(lengthConstraints).getMax()) != 1) {
- throw new InferenceException(ctx.getStatementSourceReference(),
- "Some of the length ranges in %s are not disjoint", lengthArgument);
- }
+ InferenceException.throwIf(ranges.size() > 1
+ && compareNumbers(min, Iterables.getLast(ranges).upperBound()) != 1,
+ ctx.getStatementSourceReference(), "Some of the length ranges in %s are not disjoint",
+ lengthArgument);
- lengthConstraints.add(new LengthConstraintEffectiveImpl(min, max, description, reference));
+ ranges.add(ValueRange.of(min, max));
}
- return lengthConstraints;
+ return ranges;
}
public static boolean isYangTypeBodyStmtString(final String typeName) {
}
public static boolean isYangBuiltInTypeString(final String typeName) {
- return BUILT_IN_TYPES.contains(typeName);
+ return BUILT_IN_TYPES.containsKey(typeName);
}
*
* @return true if any of specified default values is marked with an
* if-feature, otherwise false
- *
- * @throws IllegalStateException
- * if any of specified default values has not been found
*/
public static boolean hasDefaultValueMarkedWithIfFeature(final YangVersion yangVersion,
final TypeEffectiveStatement<?> typeStmt, final Set<String> defaultValues) {
*
* @return true if specified default value is marked with an if-feature,
* otherwise false
- *
- * @throws IllegalStateException
- * if specified default value has not been found
*/
public static boolean hasDefaultValueMarkedWithIfFeature(final YangVersion yangVersion,
final TypeEffectiveStatement<?> typeStmt, final String defaultValue) {
&& isAnyDefaultValueMarkedWithIfFeature(typeStmt, defaultValues);
}
+ static String findBuiltinString(final String rawArgument) {
+ return BUILT_IN_TYPES.get(rawArgument);
+ }
+
private static boolean isRelevantForIfFeatureCheck(final TypeEffectiveStatement<?> typeStmt) {
final TypeDefinition<?> typeDefinition = typeStmt.getTypeDefinition();
return typeDefinition instanceof EnumTypeDefinition || typeDefinition instanceof BitsTypeDefinition
private static boolean isAnyDefaultValueMarkedWithIfFeature(final TypeEffectiveStatement<?> typeStmt,
final Set<String> defaultValues) {
- final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = typeStmt.effectiveSubstatements();
- for (final EffectiveStatement<?, ?> effectiveSubstatement : effectiveSubstatements) {
+ final Iterator<? extends EffectiveStatement<?, ?>> iter = typeStmt.effectiveSubstatements().iterator();
+ while (iter.hasNext() && !defaultValues.isEmpty()) {
+ final EffectiveStatement<?, ?> effectiveSubstatement = iter.next();
if (YangStmtMapping.BIT.equals(effectiveSubstatement.statementDefinition())) {
final QName bitQName = (QName) effectiveSubstatement.argument();
if (defaultValues.remove(bitQName.getLocalName()) && containsIfFeature(effectiveSubstatement)) {
&& defaultValues.remove(effectiveSubstatement.argument())
&& containsIfFeature(effectiveSubstatement)) {
return true;
- } else if (effectiveSubstatement instanceof TypeEffectiveStatement) {
- return isAnyDefaultValueMarkedWithIfFeature((TypeEffectiveStatement<?>) effectiveSubstatement,
- defaultValues);
+ } else if (effectiveSubstatement instanceof TypeEffectiveStatement && isAnyDefaultValueMarkedWithIfFeature(
+ (TypeEffectiveStatement<?>) effectiveSubstatement, defaultValues)) {
+ return true;
}
}
- Preconditions.checkState(defaultValues.isEmpty(), "Unable to find following default values %s", defaultValues);
return false;
}