Package: VDMUtil

VDMUtil

nameinstructionbranchcomplexitylinemethod
VDMUtil()
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%
classname(Object)
M: 12 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
get_file_pos()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
seq_of_char2val(Object, Object)
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_of_char2val_(Object, Object)
M: 51 C: 237
82%
M: 12 C: 44
79%
M: 9 C: 20
69%
M: 14 C: 46
77%
M: 0 C: 1
100%
set2seq(Object)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
val2seq_of_char(Object)
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%

Coverage

1: package org.overture.codegen.runtime;
2:
3: public class VDMUtil
4: {
5:         private static final String NOT_SUPPORTED_MSG = "Function is currently not supported";
6:
7:         @SuppressWarnings("unchecked")
8:         public static VDMSeq set2seq(Object obj)
9:         {
10:•                if (!(obj instanceof VDMSet))
11:                 {
12:                         throw new IllegalArgumentException("Expected a set but got: " + Utils.toString(obj));
13:                 }
14:                 
15:                 VDMSet set = (VDMSet) obj;
16:                 
17:                 VDMSeq seq = SeqUtil.seq();
18:                 seq.addAll(set);
19:                 return seq;
20:         }
21:
22:         public static Object get_file_pos()
23:         {
24:                 throw new UnsupportedOperationException(NOT_SUPPORTED_MSG);
25:         }
26:
27:         public static String val2seq_of_char(Object value)
28:         {
29:                 return Utils.toString(value);
30:         }
31:
32:         // This method has been updated to also receive the type argument
33:         public static Tuple seq_of_char2val(Object s, Object type)
34:         {
35:                 return seq_of_char2val_(s, type);
36:         }
37:
38:         private static Tuple seq_of_char2val_(Object s, Object type)
39:         {
40:•                if(type == null)
41:                 {
42:                         throw new IllegalArgumentException("null is not a valid type");
43:                 }
44:                 
45:                 Tuple NO_CONVERSION = Tuple.mk_(false, null);
46:                 
47:                 String str = null;
48:                 
49:•                if(s instanceof String)
50:                 {
51:                         str = (String) s;
52:                 }
53:•                else if(s instanceof VDMSeq)
54:                 {
55:                         VDMSeq seq = (VDMSeq) s;
56:
57:                         // Check if the sequence is indeed a sequence of characters
58:•                        for(Object o : seq)
59:                         {
60:•                                if(!(o instanceof Character))
61:                                 {
62:                                         return NO_CONVERSION;
63:                                 }
64:                         }
65:                         
66:                         str = VDMSeq.toStr(seq.iterator());
67:                 }
68:                 else
69:                 {
70:                         return NO_CONVERSION;
71:                 }
72:                 
73:                 str = str.trim();
74:                 
75:•                if(type == Utils.NAT || type == Utils.NAT1 || type == Utils.INT)
76:                 {
77:                         try
78:                         {
79:                                 Integer intRes = Integer.valueOf(str);
80:                                 
81:•                                if((type == Utils.NAT && Utils.is_nat(intRes)) ||
82:•                                        (type == Utils.NAT1 && Utils.is_nat1(intRes)) ||
83:•                                        (type == Utils.INT && Utils.is_int(intRes)))
84:                                 {
85:                                         return Tuple.mk_(true, intRes);
86:                                 }
87:                                 else
88:                                 {
89:                                         return NO_CONVERSION;
90:                                 }
91:                                 
92:                         } catch (NumberFormatException e)
93:                         {
94:                                 return NO_CONVERSION;
95:                         }
96:                 }
97:•                else if(type == Utils.RAT || type == Utils.REAL)
98:                 {
99:                         try
100:                         {
101:                                 return Tuple.mk_(true, Double.valueOf(str));
102:                         } catch(NumberFormatException e)
103:                         {
104:                                 return NO_CONVERSION;
105:                         }
106:                 }
107:•                else if(type == Utils.BOOL)
108:                 {
109:•                        if (str.equals("true"))
110:                         {
111:                                 return Tuple.mk_(true, true);
112:•                        } else if (str.equals("false"))
113:                         {
114:                                 return Tuple.mk_(true, false);
115:                         } else
116:                         {
117:                                 return NO_CONVERSION;
118:                         }
119:                 }
120:•                else if(type == Utils.CHAR)
121:                 {
122:•                        if(str.length() == 3)
123:                         {
124:                                 return Tuple.mk_(true, new Character(str.charAt(1)));
125:                         }
126:                         else
127:                         {
128:                                 return NO_CONVERSION;
129:                         }
130:                 }
131:•                else if(type instanceof Quote)
132:                 {
133:                         // <A> -> A
134:                         String quoteStr = str.replaceAll("<|>", "");
135:                         
136:                         // A -> AQuote
137:                         quoteStr += "Quote";
138:                         
139:•                        if(quoteStr.equals(type.getClass().getSimpleName()))
140:                         {
141:                                 return Tuple.mk_(true, type);
142:                         }
143:                         else
144:                         {
145:                                 return NO_CONVERSION;
146:                         }
147:                 }
148:•                else if(type instanceof VDMSet)
149:                 {
150:                         VDMSet types = (VDMSet) type;
151:                         // The union type case
152:•                        for(Object t : types)
153:                         {
154:                                 Tuple conversionAttempt = seq_of_char2val_(str, t);
155:                                 
156:•                                if(conversionAttempt.size() == 2)
157:                                 {
158:                                         Object first = conversionAttempt.get(0);
159:                                         
160:•                                        if(first instanceof Boolean)
161:                                         {
162:                                                 Boolean success = (Boolean) first;
163:                                                 
164:•                                                if(success)
165:                                                 {
166:                                                         return conversionAttempt;
167:                                                 }
168:                                                 else
169:                                                 {
170:                                                         // Try to convert to the next type
171:                                                         continue;
172:                                                 }
173:                                         }
174:                                 }
175:                                 else
176:                                 {
177:                                         return NO_CONVERSION;
178:                                 }
179:                         }
180:                         
181:                         return NO_CONVERSION;
182:                 }
183:                 else
184:                 {
185:                         throw new UnsupportedOperationException("seq_of_char2val currently only supports" +
186:                                         " the following types: nat, nat1, int, rat, real, bool, char and quotes as" +
187:                                         " well as unions of these types");
188:                 }
189:         }
190:
191:         public static String classname(Object obj)
192:         {
193:•                if (obj != null && obj.getClass().getEnclosingClass() == null)
194:                 {
195:                         return obj.getClass().getSimpleName();
196:                 } else
197:                 {
198:                         return null;
199:                 }
200:         }
201: }