/* * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.model.util.type; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; import java.util.Collection; import java.util.List; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.util.BaseConstraints; import org.opendaylight.yangtools.yang.model.util.UnresolvedNumber; public abstract class LengthRestrictedTypeBuilder> extends AbstractRestrictedTypeBuilder { private List lengthAlternatives; LengthRestrictedTypeBuilder(final T baseType, final SchemaPath path) { super(Preconditions.checkNotNull(baseType), path); } public final void setLengthAlternatives(@Nonnull final Collection lengthAlternatives) { Preconditions.checkState(this.lengthAlternatives == null, "Range alternatives already defined as %s", lengthAlternatives); this.lengthAlternatives = ImmutableList.copyOf(lengthAlternatives); touch(); } private static List ensureResolvedLengths(final List unresolved, final List baseRangeConstraints) { // First check if we need to resolve anything at all for (LengthConstraint c : unresolved) { if (c.getMax() instanceof UnresolvedNumber || c.getMin() instanceof UnresolvedNumber) { return resolveLengths(unresolved, baseRangeConstraints); } } // No need, just return the same list return unresolved; } private static List resolveLengths(final List unresolved, final List baseLengthConstraints) { final Builder builder = ImmutableList.builder(); for (LengthConstraint c : unresolved) { final Number max = c.getMax(); final Number min = c.getMin(); if (max instanceof UnresolvedNumber || min instanceof UnresolvedNumber) { final Number rMax = max instanceof UnresolvedNumber ? ((UnresolvedNumber)max).resolveLength(baseLengthConstraints) : max; final Number rMin = min instanceof UnresolvedNumber ? ((UnresolvedNumber)min).resolveLength(baseLengthConstraints) : min; builder.add(BaseConstraints.newLengthConstraint(rMin, rMax, Optional.fromNullable(c.getDescription()), Optional.fromNullable(c.getReference()))); } else { builder.add(c); } } return builder.build(); } private static List ensureTypedLengths(final List lengths, final Class clazz) { for (LengthConstraint c : lengths) { if (!clazz.isInstance(c.getMin()) || !clazz.isInstance(c.getMax())) { return typedLengths(lengths, clazz); } } return lengths; } private static List typedLengths(final List lengths, final Class clazz) { final Function function = NumberUtil.converterTo(clazz); Preconditions.checkArgument(function != null, "Unsupported range class %s", clazz); final Builder builder = ImmutableList.builder(); for (LengthConstraint c : lengths) { if (!clazz.isInstance(c.getMin()) || !clazz.isInstance(c.getMax())) { final Number min, max; try { min = function.apply(c.getMin()); max = function.apply(c.getMax()); } catch (NumberFormatException e) { throw new IllegalArgumentException(String.format("Constraint %s does not fit into range of %s", c, clazz.getSimpleName()), e); } builder.add(BaseConstraints.newLengthConstraint(min, max, Optional.fromNullable(c.getDescription()), Optional.fromNullable(c.getReference()))); } else { builder.add(c); } } return builder.build(); } private static boolean lengthCovered(final List where, final LengthConstraint what) { for (LengthConstraint c : where) { if (NumberUtil.isRangeCovered(what.getMin(), what.getMax(), c.getMin(), c.getMax())) { return true; } } return false; } abstract T buildType(List lengthConstraints); abstract List getLengthConstraints(T type); abstract List typeLengthConstraints(); private List findLenghts() { List ret = ImmutableList.of(); T wlk = getBaseType(); while (wlk != null && ret.isEmpty()) { ret = getLengthConstraints(wlk); wlk = wlk.getBaseType(); } return ret.isEmpty() ? typeLengthConstraints() : ret; } @Override final T buildType() { final List baseLengths = findLenghts(); if (lengthAlternatives == null || lengthAlternatives.isEmpty()) { return buildType(baseLengths); } // Run through alternatives and resolve them against the base type final List resolvedLengths = ensureResolvedLengths(lengthAlternatives, baseLengths); // Next up, ensure the of boundaries match base constraints final Class clazz = baseLengths.get(0).getMin().getClass(); final List typedLengths = ensureTypedLengths(resolvedLengths, clazz); // Now verify if new ranges are strict subset of base ranges for (LengthConstraint c : typedLengths) { if (!lengthCovered(baseLengths, c)) { throw new InvalidLengthConstraintException(c, "Length constraint %s is not a subset of parent constraints %s", c, baseLengths); } } return buildType(typedLengths); } }