Package: SeqUtil

SeqUtil

nameinstructionbranchcomplexitylinemethod
SeqUtil()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
conc(VDMSeq, VDMSeq)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
distConc(VDMSeq)
M: 66 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
distConcStrings(VDMSeq)
M: 38 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
elems(String)
M: 26 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
elems(VDMSeq)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
inds(String)
M: 29 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
inds(VDMSeq)
M: 29 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
mod(String, VDMMap)
M: 82 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
mod(VDMSeq, VDMMap)
M: 65 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
reverse(VDMSeq)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
seq()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
seq(Object[])
M: 5 C: 26
84%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
subSeq(String, Number, Number)
M: 36 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
subSeq(VDMSeq, Number, Number)
M: 42 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
tail(String)
M: 21 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
tail(VDMSeq)
M: 37 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
toSeq(Object)
M: 48 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
toStr(Object)
M: 29 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * VDM Code Generator Runtime
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.codegen.runtime;
23:
24: import java.util.Collections;
25:
26: public class SeqUtil
27: {
28:         public static VDMSeq seq()
29:         {
30:                 return new VDMSeq();
31:         }
32:
33:         @SuppressWarnings("unchecked")
34:         public static VDMSeq seq(Object... elements)
35:         {
36:•                if (elements == null)
37:                 {
38:                         throw new IllegalArgumentException("Cannot instantiate sequence from null");
39:                 }
40:
41:                 VDMSeq seq = seq();
42:•                for (Object element : elements)
43:                 {
44:                         seq.add(element);
45:                 }
46:
47:                 return seq;
48:         }
49:
50:         @SuppressWarnings("unchecked")
51:         public static VDMSeq mod(VDMSeq seq, VDMMap map)
52:         {
53:•                if (map == null)
54:                 {
55:                         throw new IllegalArgumentException("Cannot modify sequence from null");
56:                 }
57:
58:                 Maplet[] maplets = MapUtil.toMaplets(map);
59:
60:•                if (maplets == null)
61:                 {
62:                         throw new IllegalArgumentException("Cannot modify sequence from null");
63:                 }
64:
65:•                if (seq == null)
66:                 {
67:                         throw new IllegalArgumentException("Cannot modify null");
68:                 }
69:
70:•                for (Maplet maplet : maplets)
71:                 {
72:                         Object left = maplet.getLeft();
73:                         Object right = maplet.getRight();
74:
75:•                        if (!(left instanceof Long))
76:                         {
77:                                 throw new IllegalArgumentException("Domain values of maplets in a sequence modification must be of type nat1");
78:                         }
79:
80:                         Long key = (Long) left;
81:                         seq.set(Utils.index(key), right);
82:                 }
83:
84:                 return seq;
85:         }
86:
87:         public static String mod(String string, VDMMap map)
88:         {
89:•                if (map == null)
90:                 {
91:                         throw new IllegalArgumentException("Cannot modify sequence from null");
92:                 }
93:
94:                 Maplet[] maplets = MapUtil.toMaplets(map);
95:
96:•                if (maplets == null)
97:                 {
98:                         throw new IllegalArgumentException("Cannot modify sequence from null");
99:                 }
100:
101:•                if (string == null)
102:                 {
103:                         throw new IllegalArgumentException("Cannot modify null");
104:                 }
105:
106:                 StringBuilder builder = new StringBuilder(string);
107:
108:•                for (Maplet maplet : maplets)
109:                 {
110:                         Object left = maplet.getLeft();
111:                         Object right = maplet.getRight();
112:
113:•                        if (!(left instanceof Long))
114:                         {
115:                                 throw new IllegalArgumentException("Domain values of maplets in a sequence modification must be of type nat1");
116:                         }
117:
118:•                        if (!(right instanceof Character))
119:                         {
120:                                 throw new IllegalArgumentException("Range values must be characters when modifying a character sequence");
121:                         }
122:
123:                         char rightChar = (Character) right;
124:
125:                         Long key = (Long) left;
126:                         builder.setCharAt(Utils.index(key), rightChar);
127:                 }
128:
129:                 return builder.toString();
130:         }
131:
132:         @SuppressWarnings("unchecked")
133:         public static VDMSeq subSeq(VDMSeq seq, Number fromIndex, Number toIndex)
134:         {
135:•                if (fromIndex.longValue() > toIndex.longValue()
136:•                                || toIndex.longValue() < 1)
137:                 {
138:                         return seq();
139:                 }
140:
141:                 fromIndex = Utils.index(Math.max(1, fromIndex.longValue()));
142:                 toIndex = Math.min(seq.size(), toIndex.longValue());
143:
144:                 VDMSeq subSeq = seq();
145:                 subSeq.addAll(seq.subList(Utils.toInt(fromIndex), Utils.toInt(toIndex)));
146:
147:                 return subSeq;
148:         }
149:
150:         public static String subSeq(String seq, Number fromIndex, Number toIndex)
151:         {
152:•                if (fromIndex.longValue() > toIndex.longValue()
153:•                                || toIndex.longValue() < 1)
154:                 {
155:                         return "";
156:                 }
157:
158:                 fromIndex = Utils.index(Math.max(1, fromIndex.longValue()));
159:                 toIndex = Math.min(seq.length(), toIndex.longValue());
160:
161:                 return seq.substring(Utils.toInt(fromIndex), Utils.toInt(toIndex));
162:         }
163:
164:         @SuppressWarnings("unchecked")
165:         public static VDMSet elems(VDMSeq seq)
166:         {
167:•                if (seq == null)
168:                 {
169:                         throw new IllegalArgumentException("Cannot get elems of null");
170:                 }
171:
172:                 VDMSet elems = SetUtil.set();
173:                 elems.addAll(seq);
174:
175:                 return elems;
176:         }
177:
178:         @SuppressWarnings("unchecked")
179:         public static VDMSet elems(String string)
180:         {
181:•                if (string == null)
182:                 {
183:                         throw new IllegalArgumentException("Cannot get elems of null");
184:                 }
185:
186:                 VDMSet elems = SetUtil.set();
187:
188:•                for (int i = 0; i < string.length(); i++)
189:                 {
190:                         elems.add(string.charAt(i));
191:                 }
192:
193:                 return elems;
194:         }
195:
196:         @SuppressWarnings("unchecked")
197:         public static VDMSeq reverse(VDMSeq seq)
198:         {
199:•                if (seq == null)
200:                 {
201:                         throw new IllegalArgumentException("Cannot reverse null");
202:                 }
203:
204:                 VDMSeq result = seq();
205:
206:                 result.addAll(seq);
207:                 Collections.reverse(result);
208:
209:                 return result;
210:         }
211:
212:         @SuppressWarnings("unchecked")
213:         public static VDMSeq tail(VDMSeq seq)
214:         {
215:•                if (seq == null)
216:                 {
217:                         throw new IllegalArgumentException("Cannot take tail of null");
218:                 }
219:
220:•                if (seq.isEmpty())
221:                 {
222:                         throw new IllegalArgumentException("Cannot take tail of empty sequence");
223:                 }
224:
225:                 VDMSeq tail = new VDMSeq();
226:
227:•                for (int i = 1; i < seq.size(); i++)
228:                 {
229:                         Object element = seq.get(i);
230:                         tail.add(element);
231:                 }
232:
233:                 return tail;
234:         }
235:
236:         public static String tail(String seq)
237:         {
238:•                if (seq == null)
239:                 {
240:                         throw new IllegalArgumentException("Cannot take tail of null");
241:                 }
242:
243:•                if (seq.isEmpty())
244:                 {
245:                         throw new IllegalArgumentException("Cannot take tail of empty string");
246:                 }
247:
248:                 return seq.substring(1, seq.length());
249:         }
250:
251:         @SuppressWarnings("unchecked")
252:         public static VDMSet inds(VDMSeq seq)
253:         {
254:•                if (seq == null)
255:                 {
256:                         throw new IllegalArgumentException("Cannot get indices of null");
257:                 }
258:
259:                 VDMSet indices = SetUtil.set();
260:
261:•                for (long i = 1; i <= seq.size(); i++)
262:                 {
263:                         indices.add(i);
264:                 }
265:
266:                 return indices;
267:         }
268:
269:         @SuppressWarnings("unchecked")
270:         public static VDMSet inds(String seq)
271:         {
272:•                if (seq == null)
273:                 {
274:                         throw new IllegalArgumentException("Cannot get indices of null");
275:                 }
276:
277:                 VDMSet indices = SetUtil.set();
278:
279:•                for (long i = 1; i <= seq.length(); i++)
280:                 {
281:                         indices.add(i);
282:                 }
283:
284:                 return indices;
285:         }
286:
287:         @SuppressWarnings("unchecked")
288:         public static VDMSeq conc(VDMSeq left, VDMSeq right)
289:         {
290:•                if (left == null || right == null)
291:                 {
292:                         throw new IllegalArgumentException("Cannot concatenate null");
293:                 }
294:
295:                 VDMSeq result = seq();
296:
297:                 result.addAll(left);
298:                 result.addAll(right);
299:
300:                 return result;
301:         }
302:
303:         @SuppressWarnings("unchecked")
304:         public static VDMSeq distConc(VDMSeq sequences)
305:         {
306:•                if (sequences == null)
307:                 {
308:                         throw new IllegalArgumentException("Distributed concatenation of null is undefined");
309:                 }
310:
311:                 VDMSeq result = seq();
312:
313:•                for (Object seq : sequences)
314:                 {
315:•                        if (seq instanceof String)
316:                         {
317:                                 char[] charArray = ((String) seq).toCharArray();
318:
319:•                                for (Character c : charArray)
320:                                 {
321:                                         result.add(c);
322:                                 }
323:
324:•                        } else if (seq instanceof VDMSeq)
325:                         {
326:                                 VDMSeq vdmSeq = (VDMSeq) seq;
327:                                 result.addAll(vdmSeq);
328:                         } else
329:                         {
330:                                 throw new IllegalArgumentException("Distributed concatenation only supports sequences");
331:
332:                         }
333:                 }
334:
335:                 return result;
336:         }
337:
338:         public static String distConcStrings(VDMSeq stringSeq)
339:         {
340:•                if (stringSeq == null)
341:                 {
342:                         throw new IllegalArgumentException("Distributed string concatenation of null is undefined");
343:                 }
344:
345:                 String result = "";
346:
347:•                for (Object str : stringSeq)
348:                 {
349:•                        if (!(str instanceof String))
350:                         {
351:                                 throw new IllegalArgumentException("Distributed string concatenation only supports strings");
352:                         }
353:
354:                         result += str;
355:                 }
356:
357:                 return result;
358:         }
359:
360:         public static String toStr(Object seq)
361:         {
362:•                if(seq == null)
363:                 {
364:                         /* In case the object subject to conversion is of an optional type */
365:                         return null;
366:                 }
367:
368:•                if (seq instanceof VDMSeq)
369:                 {
370:                         VDMSeq vdmSeq = (VDMSeq) seq;
371:•                        if (vdmSeq.isEmpty())
372:                         {
373:                                 return "";
374:                         } else
375:                         {
376:                                 return seq.toString();
377:                         }
378:•                } else if (seq instanceof String)
379:                 {
380:                         return (String) seq;
381:                 } else
382:                 {
383:                         throw new RuntimeException("String conversion is only supported for VDMSeq");
384:                 }
385:         }
386:
387:         @SuppressWarnings("unchecked")
388:         public static VDMSeq toSeq(Object str)
389:         {
390:•                if(str == null)
391:                 {
392:                         /* In case the object subject to conversion is of an optional type */
393:                         return null;
394:                 }
395:
396:                 VDMSeq result = seq();
397:
398:•                if (str instanceof String)
399:                 {
400:                         String string = (String) str;
401:•                        for (Character c : string.toCharArray())
402:                         {
403:                                 result.add(c);
404:                         }
405:
406:                         return result;
407:•                } else if (str instanceof VDMSeq)
408:                 {
409:                         return ((VDMSeq) str).copy();
410:                 } else
411:                 {
412:                         throw new RuntimeException("VDMSeq conversion is only supported for strings");
413:                 }
414:         }
415: }