1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package nl.tudelft.goal.unreal.translators;
21
22
23 import nl.tudelft.goal.unreal.messages.ParameterMap;
24 import eis.eis2java.exception.TranslationException;
25 import eis.eis2java.translation.Parameter2Java;
26 import eis.iilang.Identifier;
27 import eis.iilang.Parameter;
28 import eis.iilang.ParameterList;
29
30 public class ParameterMapTranslator implements Parameter2Java<ParameterMap> {
31
32
33 private static final int ENTRY_SIZE = 2;
34
35 @Override
36 public ParameterMap translate(Parameter parameter) throws TranslationException {
37 if (!(parameter instanceof ParameterList)) {
38 String message = String.format("%s is not a parameter list.", parameter);
39 throw new TranslationException(message);
40 }
41
42 return translateEntries((ParameterList) parameter);
43 }
44
45 private ParameterMap translateEntries(ParameterList parameterList) throws TranslationException {
46 ParameterMap map = new ParameterMap();
47
48 for (Parameter entry : parameterList) {
49
50 if (!(entry instanceof ParameterList)) {
51 String message = String.format("%s is not a parameter list.", entry);
52 throw new TranslationException(message);
53 }
54
55 ParameterList entryList = (ParameterList) entry;
56
57 if (entryList.size() != ENTRY_SIZE) {
58 String message = String.format("Expected a parameter list with " + ENTRY_SIZE + " elements but received %s.", entryList);
59 throw new TranslationException(message);
60 }
61
62 Parameter key = entryList.get(0);
63 Parameter value = entryList.get(1);
64
65 if(!(key instanceof Identifier)){
66 String message = String.format("Expected an identifier but received %s.", key);
67 throw new TranslationException(message);
68 }
69
70 map.put((Identifier) key,value);
71 }
72
73 return map;
74 }
75
76 @Override
77 public Class<ParameterMap> translatesTo() {
78 return ParameterMap.class;
79 }
80
81 }