2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.tree.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
17 import org.opendaylight.yangtools.yang.data.tree.api.RequiredElementCountException;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware;
21 final class MinMaxElementsValidation<T extends DataSchemaNode & ElementCountConstraintAware>
22 extends AbstractValidation {
23 private final int minElements;
24 private final int maxElements;
26 private MinMaxElementsValidation(final SchemaAwareApplyOperation<T> delegate, final Integer minElements,
27 final Integer maxElements) {
29 this.minElements = minElements != null ? minElements : 0;
30 this.maxElements = maxElements != null ? maxElements : Integer.MAX_VALUE;
33 static <T extends DataSchemaNode & ElementCountConstraintAware> ModificationApplyOperation from(
34 final SchemaAwareApplyOperation<T> delegate) {
35 final var optConstraint = delegate.getSchema().getElementCountConstraint();
36 if (optConstraint.isEmpty()) {
40 final var constraint = optConstraint.orElseThrow();
41 return new MinMaxElementsValidation<>(delegate, constraint.getMinElements(), constraint.getMaxElements());
45 void enforceOnData(final NormalizedNode data) {
46 enforceOnData(data, (actual, message) -> new MinMaxElementsValidationFailedException(message, minElements,
47 maxElements, actual));
51 void enforceOnData(final ModificationPath path, final NormalizedNode data) throws RequiredElementCountException {
52 enforceOnData(data, (actual, message) -> new RequiredElementCountException(path.toInstanceIdentifier(),
53 minElements, maxElements, actual, message));
58 interface ExceptionSupplier<T extends Exception> {
59 T get(int actual, String message);
62 private <X extends @NonNull Exception> void enforceOnData(final NormalizedNode value,
63 final ExceptionSupplier<X> exceptionSupplier) throws X {
64 checkArgument(value instanceof NormalizedNodeContainer, "Value %s is not a NormalizedNodeContainer", value);
65 final int children = ((NormalizedNodeContainer<?>) value).size();
66 if (minElements > children) {
67 throw exceptionSupplier.get(children, value.name()
68 + " does not have enough elements (" + children + "), needs at least " + minElements);
70 if (maxElements < children) {
71 throw exceptionSupplier.get(children, value.name()
72 + " has too many elements (" + children + "), can have at most " + maxElements);
77 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
78 return super.addToStringAttributes(helper.add("min", minElements).add("max", maxElements));