2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.model.ri.type;
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableRangeSet;
14 import com.google.common.collect.Range;
15 import com.google.common.collect.RangeSet;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
21 * Abstract base class for {@link ResolvedLengthConstraint} and {@link ResolvedRangeConstraint}.
23 * @param <T> type of constraint
25 abstract class AbstractConstraint<T extends Number & Comparable<T>> implements ConstraintMetaDefinition {
26 private final ConstraintMetaDefinition meta;
27 private final Object ranges;
29 AbstractConstraint(final ConstraintMetaDefinition meta, final RangeSet<T> ranges) {
30 this.meta = requireNonNull(meta);
32 final var tmp = ranges.asRanges();
33 if (tmp.size() == 1) {
34 this.ranges = tmp.iterator().next();
36 this.ranges = ImmutableRangeSet.copyOf(ranges);
41 public final Optional<String> getDescription() {
42 return meta.getDescription();
46 public final Optional<String> getErrorAppTag() {
47 return meta.getErrorAppTag();
51 public final Optional<String> getErrorMessage() {
52 return meta.getErrorMessage();
56 public final Optional<String> getReference() {
57 return meta.getReference();
60 @SuppressWarnings("unchecked")
61 final @NonNull ImmutableRangeSet<T> ranges() {
62 if (ranges instanceof ImmutableRangeSet) {
63 return (ImmutableRangeSet<T>) ranges;
65 verify(ranges instanceof Range, "Unexpected range object %s", ranges);
66 return ImmutableRangeSet.of((Range<T>) ranges);