Package: VdmDebugState

VdmDebugState

nameinstructionbranchcomplexitylinemethod
VdmDebugState(VdmDebugState.DebugState)
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%
canChange(VdmDebugState.DebugState)
M: 67 C: 0
0%
M: 26 C: 0
0%
M: 16 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
inState(VdmDebugState.DebugState)
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%
setState(VdmDebugState.DebugState)
M: 95 C: 0
0%
M: 11 C: 0
0%
M: 9 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%
toString()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
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.ide.debug.core.model;
23:
24: import java.util.List;
25: import java.util.Vector;
26:
27: import org.eclipse.core.runtime.Assert;
28:
29: /**
30: * State controller for IDebugTarget and IThreads
31: *
32: * @author kela
33: */
34: public class VdmDebugState
35: {
36:         private List<DebugState> states = new Vector<DebugState>();
37:
38:         /**
39:          * Legal states
40:          *
41:          * @author kela
42:          */
43:         public enum DebugState
44:         {
45:                 Terminated, Suspended, Disconnected, IsStepping, Resumed, Deadlocked
46:         };
47:
48:         public VdmDebugState(DebugState initialState)
49:         {
50:•                if (initialState != null)
51:                 {
52:                         this.states.add(initialState);
53:                 }
54:         }
55:
56:         /**
57:          * Sets a new state, an Assert.IsLegal is asserted if the given state is not valid based on the current state
58:          *
59:          * @param newState
60:          * the new state to change into
61:          */
62:         public synchronized void setState(DebugState newState)
63:         {
64:•                if (!states.contains(newState))
65:                 {
66:•                        switch (newState)
67:                         {
68:                                 case Disconnected:
69:                                         Assert.isLegal(canChange(DebugState.Disconnected), "Cannot disconnect a terminated state");
70:                                 case Terminated:
71:                                         Assert.isLegal(canChange(DebugState.Terminated), "Cannot terminate a terminated state");
72:                                         states.clear();
73:                                         states.add(newState);
74:                                         break;
75:                                 case Suspended:
76:                                         Assert.isLegal(canChange(DebugState.Suspended), "Can only suspend if resumed");
77:                                         states.remove(DebugState.Resumed);
78:                                         states.add(newState);
79:                                         break;
80:                                 case IsStepping:
81:                                         Assert.isLegal(canChange(DebugState.IsStepping), "Cannot step if not suspended");
82:                                         states.add(newState);
83:                                         break;
84:                                 case Resumed:
85:                                         Assert.isLegal(canChange(DebugState.Resumed), "Cannot resume in a terminated state");
86:•                                        if (states.contains(DebugState.IsStepping))
87:                                         {
88:                                                 states.clear();
89:                                                 states.add(DebugState.IsStepping);
90:                                         } else
91:                                         {
92:                                                 states.clear();
93:                                         }
94:                                         states.add(newState);
95:                                         break;
96:                                 case Deadlocked:
97:                                         states.add(newState);
98:                                         break;
99:                         }
100:                 }
101:         }
102:
103:         /**
104:          * Checks the current state
105:          *
106:          * @param state
107:          * the state to check for
108:          * @return true if in the requested state else false
109:          */
110:         public synchronized boolean inState(DebugState state)
111:         {
112:                 return states.contains(state);
113:         }
114:
115:         /**
116:          * Checks if a change to the newState is allowed
117:          *
118:          * @param newState
119:          * the new state requested
120:          * @return true if allowed else false
121:          */
122:         public synchronized boolean canChange(DebugState newState)
123:         {
124:•                switch (newState)
125:                 {
126:                         case Disconnected:
127:•                                return !inState(DebugState.Terminated)
128:•                                                && !inState(DebugState.Disconnected);
129:                         case Terminated:
130:•                                return !inState(DebugState.Terminated);
131:                         case Suspended:
132:                                 return inState(DebugState.Resumed);// states.size()==1 &&
133:                         case IsStepping:
134:•                                return (!inState(DebugState.Terminated)
135:•                                                || !inState(DebugState.Disconnected) || !inState(DebugState.Deadlocked))
136:•                                                && inState(DebugState.Suspended);
137:                         case Resumed:
138:•                                return states.size() == 0 || inState(DebugState.IsStepping)
139:•                                                || inState(DebugState.Suspended);
140:                         default:
141:                                 return false;
142:                 }
143:         }
144:
145:         @Override
146:         public String toString()
147:         {
148:                 StringBuffer string = new StringBuffer();
149:•                for (DebugState s : states)
150:                 {
151:                         string.append(s);
152:                         string.append(" ");
153:                 }
154:                 return string.toString();
155:         }
156: }