Skip to content

Package: CastNode

CastNode

nameinstructionbranchcomplexitylinemethod
CastNode(AbstractNode, IDNode)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
castStringToBoolean(AbstractReturnValue, CommunicationManager, String)
M: 11 C: 23
68%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 2 C: 6
75%
M: 0 C: 1
100%
castStringToNumeric(AbstractReturnValue, CommunicationManager, String)
M: 11 C: 8
42%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
interpret(CommunicationManager)
M: 0 C: 102
100%
M: 0 C: 12
100%
M: 0 C: 7
100%
M: 0 C: 14
100%
M: 0 C: 1
100%

Coverage

1: package pruefskript.parser.nodes;
2:
3: import model.type.AbstractType;
4: import pruefskript.CommunicationManager;
5: import pruefskript.parser.exceptions.CheckScriptException;
6: import pruefskript.parser.values.AbstractReturnValue;
7: import pruefskript.parser.values.BooleanValue;
8: import pruefskript.parser.values.NumericValue;
9: import pruefskript.parser.values.StringValue;
10:
11: /**
12: *
13: * @author Pruefskript-Gruppe III
14: *
15: */
16: public class CastNode extends AbstractBinaryOperationNode {
17:
18:         private final IDNode type;
19:
20:         public CastNode(AbstractNode operand, IDNode type) {
21:                 super(operand, type);
22:                 this.type = type;
23:         }
24:
25:         /**
26:          * This method is responsible for casting the operand1 into type. If the type is not found by
27:          * the TypeManager or the operand is not castable into the type, an Error is returned.
28:          */
29:         @Override
30:         public AbstractReturnValue interpret(CommunicationManager mgr) throws CheckScriptException {
31:                 String errString =
32:                                 this.getOperand1().toString() + " is not castable into " + this.type.getName();
33:
34:                 AbstractType t =
35:                                 mgr.getRouterConfiguration().getRouterSoftware().findTypeByName(type.getName());
36:
37:•                if (t == null)
38:                         return new ErrorNode(new StringNode("Type " + type + " not found.")).interpret(mgr);
39:
40:•                if (t.matches(this.getOperand1().interpret(mgr).getStringRepresentation())) {
41:                         final AbstractReturnValue value = this.getOperand1().interpret(mgr);
42:•                        if (type.getName().equals("NUMERIC") && value.getClass() == StringValue.class)
43:                                 return castStringToNumeric(value, mgr, errString);
44:•                        else if (type.getName().equals("YESNO") && value.getClass() == StringValue.class)
45:                                 return castStringToBoolean(value, mgr, errString);
46:                         else
47:                                 return value;
48:                 } else {
49:                         return new ErrorNode(new StringNode(errString)).interpret(mgr);
50:
51:                 }
52:         }
53:
54:         private AbstractReturnValue castStringToBoolean(AbstractReturnValue value,
55:                         CommunicationManager mgr, String errString) throws CheckScriptException {
56:                 try {
57:                         Boolean trueOrFalse = null;
58:•                        if (value.getStringRepresentation().equals("yes"))
59:                                 trueOrFalse = true;
60:•                        if (value.getStringRepresentation().equals("no"))
61:                                 trueOrFalse = false;
62:                         return new BooleanValue(trueOrFalse);
63:                 } catch (Exception e) {
64:                         return new ErrorNode(new StringNode(errString)).interpret(mgr);
65:                 }
66:         }
67:
68:         private AbstractReturnValue castStringToNumeric(AbstractReturnValue value,
69:                         CommunicationManager mgr, String errString) throws CheckScriptException {
70:                 try {
71:                         return new NumericValue(Integer.parseInt(value.getStringRepresentation()));
72:                 } catch (Exception e) {
73:                         return new ErrorNode(new StringNode(errString)).interpret(mgr);
74:                 }
75:         }
76:
77: }