18e4c31ab93856826251748abde40e73e30bf33e
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / util / TypeConstraints.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.yang.parser.util;
9
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
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;
18
19 /**
20  * Holder object for holding YANG type constraints.
21  */
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;
27
28     List<List<RangeConstraint>> getAllRanges() {
29         return ranges;
30     }
31
32     public List<RangeConstraint> getRange() {
33         if(ranges.isEmpty()) {
34             return Collections.emptyList();
35         }
36
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();
42
43         if (!(min instanceof UnknownBoundaryNumber)
44                 && !(max instanceof UnknownBoundaryNumber)) {
45             return resolved;
46         }
47
48         if (firstRange.equals(lastRange)) {
49             if (min instanceof UnknownBoundaryNumber) {
50                 min = resolveMinRange(min);
51             }
52             if (max instanceof UnknownBoundaryNumber) {
53                 max = resolveMaxRange(max);
54             }
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);
61         } else {
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);
68             }
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);
75             }
76         }
77         return resolved;
78     }
79
80     private Number resolveMinRange(Number min) {
81         int i = 1;
82         while (min instanceof UnknownBoundaryNumber) {
83             final List<RangeConstraint> act = ranges.get(i);
84             min = act.get(0).getMin();
85             i++;
86         }
87         return min;
88     }
89
90     private Number resolveMaxRange(Number max) {
91         int i = 1;
92         while (max instanceof UnknownBoundaryNumber) {
93             final List<RangeConstraint> act = ranges.get(i);
94             max = act.get(act.size() - 1).getMax();
95             i++;
96         }
97         return max;
98     }
99
100     public void addRanges(final List<RangeConstraint> ranges) {
101         if (ranges != null && !(ranges.isEmpty())) {
102             this.ranges.add(ranges);
103         }
104     }
105
106     public List<List<LengthConstraint>> getAllLengths() {
107         return lengths;
108     }
109
110     public List<LengthConstraint> getLength() {
111         if(lengths.isEmpty()) {
112             return Collections.emptyList();
113         }
114
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();
120
121         if (!(min instanceof UnknownBoundaryNumber)
122                 && !(max instanceof UnknownBoundaryNumber)) {
123             return resolved;
124         }
125
126         if (firstLength.equals(lastLength)) {
127             if (min instanceof UnknownBoundaryNumber) {
128                 min = resolveMinLength(min);
129             }
130             if (max instanceof UnknownBoundaryNumber) {
131                 max = resolveMaxLength(max);
132             }
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);
139         } else {
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);
146             }
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);
153             }
154         }
155         return resolved;
156     }
157
158     private Number resolveMinLength(Number min) {
159         int i = 1;
160         while (min instanceof UnknownBoundaryNumber) {
161             final List<LengthConstraint> act = lengths.get(i);
162             min = act.get(0).getMin();
163             i++;
164         }
165         return min;
166     }
167
168     private Number resolveMaxLength(Number max) {
169         int i = 1;
170         while (max instanceof UnknownBoundaryNumber) {
171             final List<LengthConstraint> act = lengths.get(i);
172             max = act.get(act.size() - 1).getMax();
173             i++;
174         }
175         return max;
176     }
177
178     public void addLengths(final List<LengthConstraint> lengths) {
179         if (lengths != null && !(lengths.isEmpty())) {
180             this.lengths.add(lengths);
181         }
182     }
183
184     public List<PatternConstraint> getPatterns() {
185         return patterns;
186     }
187
188     public void addPatterns(final List<PatternConstraint> patterns) {
189         this.patterns.addAll(patterns);
190     }
191
192     public Integer getFractionDigits() {
193         return fractionDigits;
194     }
195
196     public void setFractionDigits(final Integer fractionDigits) {
197         if (this.fractionDigits == null) {
198             this.fractionDigits = fractionDigits;
199         }
200     }
201
202 }