Package: MapUtil

MapUtil

nameinstructionbranchcomplexitylinemethod
MapUtil()
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%
comp(Object, Object)
M: 0 C: 49
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
differentValues(Object, Object)
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
dom(Object)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
domResBy(Object, Object)
M: 42 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
domResTo(Object, Object)
M: 41 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
get(Object, Object)
M: 32 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
inverse(Object)
M: 66 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
iteration(Object, Object)
M: 16 C: 98
86%
M: 3 C: 15
83%
M: 2 C: 8
80%
M: 1 C: 25
96%
M: 0 C: 1
100%
map()
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%
map(Maplet[])
M: 20 C: 47
70%
M: 5 C: 5
50%
M: 4 C: 2
33%
M: 5 C: 12
71%
M: 0 C: 1
100%
mapAdd(Object, Object)
M: 54 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
merge(Object)
M: 31 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
munion(Object, Object)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
override(Object, Object)
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
putAll(VDMMap, VDMMap)
M: 40 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
rng(Object)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
rngResBy(Object, Object)
M: 42 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
rngResTo(Object, Object)
M: 44 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
toMaplets(Object)
M: 38 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
validateMap(Object, String)
M: 19 C: 4
17%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 2
50%
M: 0 C: 1
100%
validateMaps(Object, Object, String)
M: 23 C: 7
23%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 2
50%
M: 0 C: 1
100%

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.LinkedList;
25: import java.util.Set;
26:
27: public class MapUtil
28: {
29:         public static VDMMap map()
30:         {
31:                 return new VDMMap();
32:         }
33:
34:         public static Maplet[] toMaplets(Object map)
35:         {
36:                 validateMap(map, "toMaplets");
37:
38:                 VDMMap vdmMap = (VDMMap) map;
39:
40:                 Maplet[] maplets = new Maplet[vdmMap.size()];
41:
42:                 int nextIndex = 0;
43:•                for (Object key : vdmMap.keySet())
44:                 {
45:                         Object val = vdmMap.get(key);
46:
47:                         maplets[nextIndex++] = new Maplet(key, val);
48:                 }
49:
50:                 return maplets;
51:         }
52:
53:         public static Object get(Object map, Object key)
54:         {
55:                 validateMap(map, "map read");
56:
57:                 VDMMap vdmMap = (VDMMap) map;
58:
59:                 Object value = vdmMap.get(key);
60:•                if (value != null)
61:                 {
62:                         return value;
63:                 } else
64:                 {
65:                         // The key may map to null
66:•                        if (vdmMap.containsKey(key))
67:                         {
68:                                 // The key is there
69:                                 return null;
70:                         } else
71:                         {
72:                                 throw new IllegalArgumentException("No such key in map: "
73:                                                 + key);
74:                         }
75:                 }
76:         }
77:
78:         @SuppressWarnings("unchecked")
79:         public static VDMSet dom(Object map)
80:         {
81:                 validateMap(map, "map domain");
82:
83:                 VDMMap vdmMap = (VDMMap) map;
84:
85:                 VDMSet set = SetUtil.set();
86:                 set.addAll(vdmMap.keySet());
87:
88:                 return set;
89:         }
90:
91:         @SuppressWarnings("unchecked")
92:         public static VDMSet rng(Object map)
93:         {
94:                 validateMap(map, "map range");
95:
96:                 VDMMap vdmMap = (VDMMap) map;
97:
98:                 VDMSet set = SetUtil.set();
99:                 set.addAll(vdmMap.values());
100:
101:                 return set;
102:         }
103:
104:         @SuppressWarnings("unchecked")
105:         public static VDMMap munion(Object left, Object right)
106:         {
107:                 validateMaps(left, right, "map union");
108:
109:                 VDMMap mapLeft = (VDMMap) left;
110:                 VDMMap mapRight = (VDMMap) right;
111:
112:                 VDMMap result = map();
113:
114:                 result.putAll(mapLeft);
115:
116:                 putAll(result, mapRight);
117:
118:                 return result;
119:         }
120:
121:         @SuppressWarnings("unchecked")
122:         public static void mapAdd(Object map, Object maplet)
123:         {
124:•                if (!(map instanceof VDMMap))
125:                 {
126:                         throw new IllegalArgumentException("Expected " + map + " to be a "
127:                                         + VDMMap.class.getSimpleName());
128:                 }
129:
130:•                if (!(maplet instanceof Maplet))
131:                 {
132:                         throw new IllegalArgumentException("Expected " + maplet
133:                                         + " to be a " + Maplet.class.getSimpleName());
134:                 }
135:
136:                 VDMMap vdmMap = (VDMMap) map;
137:                 Maplet vdmMaplet = (Maplet) maplet;
138:
139:                 vdmMap.put(vdmMaplet.getLeft(), vdmMaplet.getRight());
140:         }
141:
142:         @SuppressWarnings("unchecked")
143:         public static VDMMap override(Object left, Object right)
144:         {
145:                 validateMaps(left, right, "map override");
146:
147:                 VDMMap mapLeft = (VDMMap) left;
148:                 VDMMap mapRight = (VDMMap) right;
149:
150:                 VDMMap result = map();
151:
152:                 result.putAll(mapLeft);
153:                 result.putAll(mapRight);
154:
155:                 return result;
156:         }
157:
158:         public static VDMMap merge(Object setOfMaps)
159:         {
160:                 final String MAP_MERGE = "map merge";
161:
162:                 SetUtil.validateSet(setOfMaps, MAP_MERGE);
163:
164:                 VDMSet vdmSetOfMaps = (VDMSet) setOfMaps;
165:
166:                 VDMMap result = map();
167:
168:•                for (Object map : vdmSetOfMaps)
169:                 {
170:                         validateMap(map, MAP_MERGE);
171:
172:                         VDMMap vdmMap = (VDMMap) map;
173:
174:                         putAll(result, vdmMap);
175:                 }
176:
177:                 return result;
178:         }
179:
180:         @SuppressWarnings("unchecked")
181:         public static VDMMap domResTo(Object dom, Object map)
182:         {
183:                 final String MAP_DOM_RESTRICT_TO = "map domain restrict to";
184:
185:                 SetUtil.validateSet(dom, MAP_DOM_RESTRICT_TO);
186:                 validateMap(map, MAP_DOM_RESTRICT_TO);
187:
188:                 VDMSet vdmDom = (VDMSet) dom;
189:                 VDMMap vdmMap = (VDMMap) map;
190:
191:                 VDMMap result = map();
192:
193:•                for (Object key : vdmDom)
194:                 {
195:•                        if (vdmMap.containsKey(key))
196:                         {
197:                                 Object value = vdmMap.get(key);
198:                                 result.put(key, value);
199:                         }
200:                 }
201:
202:                 return result;
203:         }
204:
205:         @SuppressWarnings("unchecked")
206:         public static VDMMap domResBy(Object dom, Object map)
207:         {
208:                 final String MAP_DOM_RESTRICT_BY = "map domain restrict by";
209:
210:                 SetUtil.validateSet(dom, MAP_DOM_RESTRICT_BY);
211:                 validateMap(map, MAP_DOM_RESTRICT_BY);
212:
213:                 VDMSet vdmDom = (VDMSet) dom;
214:                 VDMMap vdmMap = (VDMMap) map;
215:
216:                 VDMMap result = map();
217:
218:•                for (Object key : vdmMap.keySet())
219:                 {
220:•                        if (!vdmDom.contains(key))
221:                         {
222:                                 Object value = vdmMap.get(key);
223:                                 result.put(key, value);
224:                         }
225:                 }
226:
227:                 return result;
228:         }
229:
230:         @SuppressWarnings("unchecked")
231:         public static VDMMap rngResTo(Object map, Object rng)
232:         {
233:                 final String MAP_RANGE_RESTRICT_TO = "map range restrict to";
234:
235:                 validateMap(map, MAP_RANGE_RESTRICT_TO);
236:                 SetUtil.validateSet(rng, MAP_RANGE_RESTRICT_TO);
237:
238:                 VDMMap vdmMap = (VDMMap) map;
239:                 VDMSet vdmRng = (VDMSet) rng;
240:
241:                 VDMMap result = map();
242:
243:                 @SuppressWarnings("rawtypes")
244:                 Set dom = vdmMap.keySet();
245:
246:•                for (Object key : dom)
247:                 {
248:                         Object value = vdmMap.get(key);
249:
250:•                        if (vdmRng.contains(value))
251:                         {
252:                                 result.put(key, value);
253:                         }
254:                 }
255:
256:                 return result;
257:         }
258:
259:         @SuppressWarnings("unchecked")
260:         public static VDMMap rngResBy(Object map, Object rng)
261:         {
262:                 final String MAP_RANGE_RESTRICT_BY = "map range restrict by";
263:
264:                 validateMap(map, MAP_RANGE_RESTRICT_BY);
265:                 SetUtil.validateSet(rng, MAP_RANGE_RESTRICT_BY);
266:
267:                 VDMMap vdmMap = (VDMMap) map;
268:                 VDMSet vdmRng = (VDMSet) rng;
269:
270:                 VDMMap result = map();
271:
272:•                for (Object key : vdmMap.keySet())
273:                 {
274:                         Object value = vdmMap.get(key);
275:
276:•                        if (!vdmRng.contains(value))
277:                         {
278:                                 result.put(key, value);
279:                         }
280:                 }
281:
282:                 return result;
283:         }
284:
285:         @SuppressWarnings("unchecked")
286:         public static VDMMap inverse(Object map)
287:         {
288:                 validateMap(map, "map inverse");
289:
290:                 VDMMap vdmMap = (VDMMap) map;
291:
292:                 VDMMap result = map();
293:
294:•                if (vdmMap.size() == 0)
295:                 {
296:                         return result;
297:                 }
298:
299:                 @SuppressWarnings("rawtypes")
300:                 Set keysSet = vdmMap.keySet();
301:                 @SuppressWarnings("rawtypes")
302:                 LinkedList keyList = new LinkedList(keysSet);
303:
304:                 Object firstKey = keyList.get(0);
305:                 Object firstValue = vdmMap.get(firstKey);
306:                 result.put(firstValue, firstKey);
307:
308:•                for (int i = 1; i < keyList.size(); i++)
309:                 {
310:                         Object nextKey = keyList.get(i);
311:                         Object nextValue = vdmMap.get(nextKey);
312:
313:•                        if (result.containsKey(nextKey))
314:                         {
315:                                 throw new IllegalArgumentException("Cannot invert non-injective map");
316:                         } else
317:                         {
318:                                 result.put(nextValue, nextKey);
319:                         }
320:                 }
321:
322:                 return result;
323:         }
324:
325:         @SuppressWarnings("unchecked")
326:         public static VDMMap comp(Object left, Object right)
327:         {
328:                 validateMaps(left, right, "map composition");
329:
330:                 VDMMap mapLeft = (VDMMap) left;
331:                 VDMMap mapRight = (VDMMap) right;
332:
333:•                if(!SetUtil.subset(MapUtil.rng(mapRight), MapUtil.dom(mapLeft)))
334:                 {
335:                         throw new IllegalArgumentException("The RHS range is not a subset of the LHS domain");
336:                 }
337:
338:
339:                 VDMMap res = MapUtil.map();
340:
341:•                for(Object key : mapRight.keySet())
342:                 {
343:                         Object nextKey = mapRight.get(key);
344:                         Object value = mapLeft.get(nextKey);
345:
346:                         res.put(key, value);
347:                 }
348:
349:                 return res;
350:         }
351:
352:         @SuppressWarnings("unchecked")
353:         public static VDMMap iteration(Object left, Object iterations)
354:         {
355:                 validateMap(left, "map iteration");
356:
357:•                if(!Utils.is_nat(iterations))
358:                 {
359:                         throw new IllegalArgumentException("Map iterator expects a nat as right hand arg");
360:                 }
361:
362:                 VDMMap map = (VDMMap) left;
363:
364:                 Number n = (Number) iterations;
365:
366:                 int intVal = n.intValue();
367:
368:•                if(intVal == 0)
369:                 {
370:                         VDMMap identityMap = MapUtil.map();
371:
372:•                        for(Object k : map.keySet())
373:                         {
374:                                 identityMap.put(k, k);
375:                         }
376:
377:                         return identityMap;
378:                 }
379:•                else if(intVal == 1)
380:                 {
381:                         return map;
382:                 }
383:                 else
384:                 {
385:                         // iterations > 0
386:                         VDMMap result = new VDMMap();
387:
388:•                        for (Object k : map.keySet())
389:                         {
390:                                 Object r = k;
391:
392:•                                for (int i = 0; i < intVal; i++)
393:                                 {
394:                                         r = map.get(r);
395:                                 }
396:
397:•                                if (r == null)
398:                                 {
399:                                         throw new IllegalArgumentException("Map range is not a subset of its domain: " + k);
400:                                 }
401:
402:                                 Object old = result.put(k, r);
403:
404:•                                if (old != null && !Utils.equals(old, r))
405:                                 {
406:                                         throw new IllegalArgumentException("Duplicate map keys have different values: " + k);
407:                                 }
408:                         }
409:
410:                         return result;
411:                 }
412:         }
413:
414:         @SuppressWarnings("unchecked")
415:         public static VDMMap map(Maplet... elements)
416:         {
417:•                if (elements == null)
418:                 {
419:                         throw new IllegalArgumentException("Cannot instantiate map from null");
420:                 }
421:
422:                 VDMMap map = map();
423:
424:•                if (elements.length == 0)
425:                 {
426:                         return map;
427:                 } else
428:                 {
429:                         Maplet firstElement = elements[0];
430:                         map.put(firstElement.getLeft(), firstElement.getRight());
431:                 }
432:
433:•                for (int i = 1; i < elements.length; i++)
434:                 {
435:                         Maplet maplet = elements[i];
436:
437:                         Object mapletKey = maplet.getLeft();
438:                         Object mapletValue = maplet.getRight();
439:
440:•                        if (map.containsKey(mapletKey))
441:                         {
442:                                 Object mapValue = map.get(mapletKey);
443:
444:•                                if (differentValues(mapletValue, mapValue))
445:                                 {
446:                                         throw new IllegalArgumentException("Duplicate keys that have different values are not allowed");
447:                                 }
448:                         }
449:
450:                         map.put(mapletKey, mapletValue);
451:                 }
452:
453:                 return map;
454:         }
455:
456:         @SuppressWarnings("unchecked")
457:         private static void putAll(VDMMap to, VDMMap from)
458:         {
459:                 @SuppressWarnings("rawtypes")
460:                 Set fromKeys = from.keySet();
461:
462:•                for (Object fromKey : fromKeys)
463:                 {
464:                         Object fromVal = from.get(fromKey);
465:
466:•                        if (to.containsKey(fromKey))
467:                         {
468:                                 Object toVal = to.get(fromKey);
469:•                                if (differentValues(toVal, fromVal))
470:                                 {
471:                                         throw new IllegalAccessError("Duplicate keys that have different values are not allowed");
472:                                 }
473:                         }
474:
475:                         to.put(fromKey, fromVal);
476:                 }
477:         }
478:
479:         static void validateMap(Object arg, String operator)
480:         {
481:•                if (!(arg instanceof VDMMap))
482:                 {
483:                         throw new IllegalArgumentException(operator
484:                                         + " is only supported for " + VDMMap.class.getName()
485:                                         + ". Got " + arg);
486:                 }
487:         }
488:
489:         private static void validateMaps(Object left, Object right, String operator)
490:         {
491:•                if (!(left instanceof VDMMap) || !(right instanceof VDMMap))
492:                 {
493:                         throw new IllegalArgumentException(operator
494:                                         + " is only supported for " + VDMMap.class.getName()
495:                                         + ". Got " + left + " and " + right);
496:                 }
497:         }
498:
499:         private static boolean differentValues(Object leftVal, Object rightVal)
500:         {
501:•                return leftVal == null && rightVal != null
502:•                                || leftVal != null && !leftVal.equals(rightVal);
503:         }
504: }