2 * Copyright (c) 2013 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.controller.yang.parser.util;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
15 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
16 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
17 import org.opendaylight.controller.yang.model.util.BaseConstraints;
20 * Holder object for holding YANG type constraints.
22 public final class TypeConstraints {
23 private final List<List<RangeConstraint>> ranges = new ArrayList<List<RangeConstraint>>();
24 private final List<List<LengthConstraint>> lengths = new ArrayList<List<LengthConstraint>>();
25 private final List<PatternConstraint> patterns = new ArrayList<PatternConstraint>();
26 private Integer fractionDigits;
28 List<List<RangeConstraint>> getAllRanges() {
32 public List<RangeConstraint> getRange() {
33 if(ranges.isEmpty()) {
34 return Collections.emptyList();
37 final List<RangeConstraint> resolved = ranges.get(0);
38 RangeConstraint firstRange = resolved.get(0);
39 RangeConstraint lastRange = resolved.get(resolved.size() - 1);
40 Number min = firstRange.getMin();
41 Number max = lastRange.getMax();
43 if (!(min instanceof UnknownBoundaryNumber)
44 && !(max instanceof UnknownBoundaryNumber)) {
48 if (firstRange.equals(lastRange)) {
49 if (min instanceof UnknownBoundaryNumber) {
50 min = resolveMinRange(min);
52 if (max instanceof UnknownBoundaryNumber) {
53 max = resolveMaxRange(max);
55 firstRange = BaseConstraints.rangeConstraint(min, max,
56 firstRange.getDescription(), firstRange.getReference());
57 resolved.set(0, firstRange);
58 lastRange = BaseConstraints.rangeConstraint(min, max,
59 lastRange.getDescription(), lastRange.getReference());
60 resolved.set(resolved.size() - 1, lastRange);
62 if (min instanceof UnknownBoundaryNumber) {
63 min = resolveMinRange(min);
64 firstRange = BaseConstraints.rangeConstraint(min,
65 firstRange.getMax(), firstRange.getDescription(),
66 firstRange.getReference());
67 resolved.set(0, firstRange);
69 if (max instanceof UnknownBoundaryNumber) {
70 max = resolveMaxRange(max);
71 lastRange = BaseConstraints.rangeConstraint(lastRange.getMin(),
72 max, lastRange.getDescription(),
73 lastRange.getReference());
74 resolved.set(resolved.size() - 1, lastRange);
80 private Number resolveMinRange(Number min) {
82 while (min instanceof UnknownBoundaryNumber) {
83 final List<RangeConstraint> act = ranges.get(i);
84 min = act.get(0).getMin();
90 private Number resolveMaxRange(Number max) {
92 while (max instanceof UnknownBoundaryNumber) {
93 final List<RangeConstraint> act = ranges.get(i);
94 max = act.get(act.size() - 1).getMax();
100 public void addRanges(final List<RangeConstraint> ranges) {
101 if (ranges != null && !(ranges.isEmpty())) {
102 this.ranges.add(ranges);
106 public List<List<LengthConstraint>> getAllLengths() {
110 public List<LengthConstraint> getLength() {
111 if(lengths.isEmpty()) {
112 return Collections.emptyList();
115 final List<LengthConstraint> resolved = lengths.get(0);
116 LengthConstraint firstLength = resolved.get(0);
117 LengthConstraint lastLength = resolved.get(resolved.size() - 1);
118 Number min = firstLength.getMin();
119 Number max = lastLength.getMax();
121 if (!(min instanceof UnknownBoundaryNumber)
122 && !(max instanceof UnknownBoundaryNumber)) {
126 if (firstLength.equals(lastLength)) {
127 if (min instanceof UnknownBoundaryNumber) {
128 min = resolveMinLength(min);
130 if (max instanceof UnknownBoundaryNumber) {
131 max = resolveMaxLength(max);
133 firstLength = BaseConstraints.lengthConstraint(min, max,
134 firstLength.getDescription(), firstLength.getReference());
135 resolved.set(0, firstLength);
136 lastLength = BaseConstraints.lengthConstraint(min, max,
137 lastLength.getDescription(), lastLength.getReference());
138 resolved.set(resolved.size() - 1, lastLength);
140 if (min instanceof UnknownBoundaryNumber) {
141 min = resolveMinLength(min);
142 firstLength = BaseConstraints.lengthConstraint(min,
143 firstLength.getMax(), firstLength.getDescription(),
144 firstLength.getReference());
145 resolved.set(0, firstLength);
147 if (max instanceof UnknownBoundaryNumber) {
148 max = resolveMaxLength(max);
149 lastLength = BaseConstraints.lengthConstraint(
150 lastLength.getMin(), max, lastLength.getDescription(),
151 lastLength.getReference());
152 resolved.set(resolved.size() - 1, lastLength);
158 private Number resolveMinLength(Number min) {
160 while (min instanceof UnknownBoundaryNumber) {
161 final List<LengthConstraint> act = lengths.get(i);
162 min = act.get(0).getMin();
168 private Number resolveMaxLength(Number max) {
170 while (max instanceof UnknownBoundaryNumber) {
171 final List<LengthConstraint> act = lengths.get(i);
172 max = act.get(act.size() - 1).getMax();
178 public void addLengths(final List<LengthConstraint> lengths) {
179 if (lengths != null && !(lengths.isEmpty())) {
180 this.lengths.add(lengths);
184 public List<PatternConstraint> getPatterns() {
188 public void addPatterns(final List<PatternConstraint> patterns) {
189 this.patterns.addAll(patterns);
192 public Integer getFractionDigits() {
193 return fractionDigits;
196 public void setFractionDigits(final Integer fractionDigits) {
197 if (this.fractionDigits == null) {
198 this.fractionDigits = fractionDigits;