Skip to content

Package: AssignmentFile

AssignmentFile

nameinstructionbranchcomplexitylinemethod
AssignmentFile()
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
AssignmentFile(Map, List)
M: 0 C: 28
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
addAssignment(VariableAssignment)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
configureAndAdd(UnconfiguredVariableAssignment, String, RouterConfiguration)
M: 210 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 46 C: 0
0%
M: 1 C: 0
0%
createPosAssignmentMap()
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
createPosCommentMap()
M: 0 C: 24
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
equals(Object)
M: 0 C: 21
100%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 0 C: 3
100%
M: 0 C: 1
100%
getAssignments()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getComments()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hashCode()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
prepareStringToPrint()
M: 0 C: 108
100%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 0 C: 27
100%
M: 0 C: 1
100%
print(String)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
printTheLinesToFile(String, StringBuffer)
M: 0 C: 31
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
removeAssignment(VariableAssignment)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
sortPositions()
M: 0 C: 39
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
update(Observable, Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: package model.assignment;
2:
3: import java.io.FileOutputStream;
4: import java.io.IOException;
5: import java.io.OutputStreamWriter;
6: import java.util.ArrayList;
7: import java.util.HashMap;
8: import java.util.Iterator;
9: import java.util.List;
10: import java.util.Map;
11: import java.util.Observable;
12:
13: import model.Comment;
14: import model.Position;
15: import model.RouterConfiguration;
16: import model.definition.AbstractVariableDefinition;
17: import parser.ParserException;
18: import parser.nodes.AbsoluteIndex;
19: import parser.nodes.ConfiguredVariableAssignment;
20: import parser.nodes.EmptyTarget;
21: import parser.nodes.FreeIndex;
22: import parser.nodes.SubLevelTarget;
23: import parser.nodes.Target;
24: import parser.nodes.UnconfiguredVariableAssignment;
25: import parser.states.GlobalFunctions;
26: import printer.PrinterException;
27: import printer.PrinterUtility;
28:
29: /**
30: * AssignmentFile represents the "\config\<package>.txt" files.
31: *
32: * @author HFW410RA - Philipp Rammos
33: */
34: public class AssignmentFile extends java.util.Observable implements java.util.Observer {
35:         /**
36:          * HashMap of all assignments from the AssignmentFile.
37:          */
38:         private final java.util.Map<String, VariableAssignment> assignments =
39:                         new HashMap<String, VariableAssignment>();
40:
41:         /**
42:          * HashMap of all comments from the AssignmentFile.
43:          */
44:         private final java.util.List<Comment> comments;
45:
46:         /**
47:          * Default constructor.
48:          */
49:         public AssignmentFile() {
50:                 this.comments = new ArrayList<Comment>();
51:         }
52:
53:         /**
54:          * Constructor for AssignmentFile, just sets the field.
55:          *
56:          * @param assignments2
57:          * The assignments to set.
58:          * @param comments
59:          * The comments to set.
60:          */
61:         public AssignmentFile(final Map<String, VariableAssignment> assignments2,
62:                         final List<Comment> comments) {
63:•                for (Map.Entry<String, VariableAssignment> assignment : assignments2.entrySet()) {
64:                         this.addAssignment(assignment.getValue());
65:                 }
66:                 this.comments = comments;
67:         }
68:
69:         @Override
70:         public boolean equals(final Object obj) {
71:•                return obj instanceof AssignmentFile
72:•                                && ((AssignmentFile) obj).getComments().equals(this.getComments())
73:•                                && ((AssignmentFile) obj).getAssignments().equals(this.getAssignments());
74:         }
75:
76:         @Override
77:         public int hashCode() {
78:                 return this.getComments().hashCode() + this.getAssignments().hashCode();
79:         }
80:
81:         /**
82:          * Returns the field assignments. No side effects.
83:          *
84:          * @return this.assignments;
85:          */
86:         public java.util.Map<String, VariableAssignment> getAssignments() {
87:                 return this.assignments;
88:         }
89:
90:         /**
91:          * Adds an assignment to the assigemnt file.
92:          *
93:          * @param va
94:          * the variable assignment to add.
95:          *
96:          */
97:         public void addAssignment(final VariableAssignment va) {
98:                 this.assignments.put(va.getNameOfAssignment(), va);
99:                 va.addObserver(this);
100:         }
101:
102:         /**
103:          * Remove an assignment from the assigemnt file.
104:          *
105:          * @param va
106:          * the variable assignment to delete.
107:          *
108:          */
109:         public void removeAssignment(final VariableAssignment va) {
110:                 this.assignments.remove(va.getNameOfAssignment());
111:                 va.deleteObserver(this);
112:         }
113:
114:         /**
115:          * @return the comments
116:          */
117:         public java.util.List<Comment> getComments() {
118:                 return this.comments;
119:         }
120:
121:         /**
122:          * Prints an AssignmentFile containing the VariableAssignments and Comments.
123:          *
124:          * @param pathToPrint
125:          * The path where to print the file.
126:          * @throws printer.PrinterException
127:          * If a IOException is thrown.
128:          * @throws printer.PrinterException
129:          * @return Returns a string representing the printed lines (for testing).
130:          */
131:         public String print(final String pathToPrint) throws printer.PrinterException {
132:
133:                 final StringBuffer stringBuffer = prepareStringToPrint();
134:
135:                 printTheLinesToFile(pathToPrint, stringBuffer);
136:
137:                 return stringBuffer.toString();
138:         }
139:
140:         /**
141:          * Prepares the StringBuffer for printing. After this method, the buffer is ready for printing
142:          * and it will contain the needed lines.
143:          *
144:          * @return The print-ready Buffer.
145:          */
146:         private StringBuffer prepareStringToPrint() {
147:                 final PrinterUtility printUt = new PrinterUtility();
148:                 final StringBuffer stringBuffer = new StringBuffer();
149:                 final List<Position> posSorted = sortPositions();
150:                 final Map<Position, Comment> commentMap = createPosCommentMap();
151:                 final Map<Position, VariableAssignment> assignmentMap = createPosAssignmentMap();
152:
153:                 final Iterator<Position> posIt = posSorted.iterator();
154:                 int row = 0;
155:                 int col = 0;
156:                 int size = 0;
157:•                while (posIt.hasNext()) {
158:                         final Position current = posIt.next();
159:•                        if (row < current.getRow()) {
160:                                 row += printUt.writeLineBreaks(stringBuffer, row, current.getRow());
161:                                 col = 0;
162:                         }
163:•                        if (row == current.getRow() && col < current.getColumn()) {
164:                                 col += printUt.writeSpaces(stringBuffer, col, current.getColumn());
165:                         }
166:•                        if (assignmentMap.containsKey(current)) {
167:                                 final VariableAssignment assignToPrint = assignmentMap.get(current);
168:                                 stringBuffer.append(assignToPrint.print());
169:                                 size = assignToPrint.print().length();
170:                         } else {
171:                                 final Comment commentToPrint = commentMap.get(current);
172:                                 stringBuffer.append(commentToPrint.print());
173:                                 size = commentToPrint.print().length();
174:                         }
175:                         col += size;
176:
177:                 }
178:                 return stringBuffer;
179:         }
180:
181:         /**
182:          * Creates a Map containing assignment.getPosition() as Key and assignment as Value. No side
183:          * effects to fields.
184:          *
185:          * @return The created map.
186:          */
187:         private Map<Position, VariableAssignment> createPosAssignmentMap() {
188:                 final Map<Position, VariableAssignment> assignmentMap =
189:                                 new HashMap<Position, VariableAssignment>();
190:                 final Iterator<VariableAssignment> assignit = this.assignments.values().iterator();
191:•                while (assignit.hasNext()) {
192:                         final VariableAssignment current = assignit.next();
193:                         assignmentMap.put(current.getPosition(), current);
194:                 }
195:                 return assignmentMap;
196:         }
197:
198:         /**
199:          * Creates a Map containing comment.getPosition() as Key and comment as Value.No side effects to
200:          * fields.
201:          *
202:          * @return The created map.
203:          */
204:         private Map<Position, Comment> createPosCommentMap() {
205:                 final Map<Position, Comment> commentMap = new HashMap<Position, Comment>();
206:                 final Iterator<Comment> commentIt = this.comments.iterator();
207:•                while (commentIt.hasNext()) {
208:                         final Comment current = commentIt.next();
209:                         commentMap.put(current.getPosition(), current);
210:                 }
211:                 return commentMap;
212:         }
213:
214:         /**
215:          * The only one method, that real prints lines into the file.
216:          *
217:          * @param pathToPrint
218:          * The path where to print the file-
219:          * @param stringBuffer
220:          * The buffer with the lines to print.
221:          * @throws PrinterException
222:          * If an IOException is caused by the printer.
223:          */
224:         private void printTheLinesToFile(final String pathToPrint, final StringBuffer stringBuffer)
225:                         throws PrinterException {
226:                 try {
227:                         final OutputStreamWriter writer = new OutputStreamWriter(
228:                                         new FileOutputStream(pathToPrint), basic.PrinterConstants.ENCODING);
229:                         try {
230:                                 writer.write(stringBuffer.toString());
231:                         } finally {
232:                                 writer.close();
233:                         }
234:                 } catch (final IOException ioEx) {
235:                         throw new PrinterException(basic.PrinterConstants.PATHNOTAVAILABLAE + pathToPrint,
236:                                         ioEx);
237:                 }
238:         }
239:
240:         /**
241:          * Sorts the Positions of VariableAssignments and Comments in one List.
242:          *
243:          * @return The list of sorted Positions.
244:          */
245:         private List<Position> sortPositions() {
246:                 final List<Position> sortedPositions = new ArrayList<Position>();
247:                 final Iterator<Comment> commantIt = this.comments.iterator();
248:•                while (commantIt.hasNext()) {
249:                         sortedPositions.add(commantIt.next().getPosition());
250:                 }
251:                 final Iterator<VariableAssignment> assignIt = this.assignments.values().iterator();
252:•                while (assignIt.hasNext()) {
253:                         sortedPositions.add(assignIt.next().getPosition());
254:                 }
255:                 java.util.Collections.sort(sortedPositions);
256:                 return sortedPositions;
257:         }
258:
259:         public void configureAndAdd(UnconfiguredVariableAssignment ass, String path,
260:                         RouterConfiguration rc) throws ParserException {
261:                 try {
262:                         Integer countConfiguredList = 0;
263:                         HashMap<String, ArrayList<Integer>> indexMap =
264:                                         new HashMap<String, ArrayList<Integer>>();
265:                         Target myTarget = ass.getTarget();
266:                         String nameOfAssignment = myTarget.toString();
267:                         AbstractVariableDefinition myDefinition = GlobalFunctions.getTheInstance()
268:                                         .searchVariableDefinition(rc.getRouterSoftware(), myTarget.getName());
269:•                        if (!(ass.getValue() == null)) {
270:                                 this.getAssignments().put(nameOfAssignment, new VariableAssignment(
271:                                                 nameOfAssignment, myDefinition, ass.getValue(), ass.getPosition()));
272:                         }
273:                         ArrayList<ConfiguredVariableAssignment> list = ass.configure();
274:•                        while (countConfiguredList < list.size()) {
275:                                 ConfiguredVariableAssignment listElement = list.get(countConfiguredList);
276:                                 ArrayList<Integer> currentIndize;
277:                                 SubLevelTarget target;
278:•                                if (listElement.getTarget() instanceof EmptyTarget) {
279:                                         currentIndize = indexMap.get(ass.getTarget().getName());
280:                                         target = (SubLevelTarget) listElement.getTarget().getSub();
281:                                 } else {
282:                                         currentIndize = indexMap.get(listElement.getTarget().getName());
283:                                         target = (SubLevelTarget) listElement.getTarget();
284:                                 }
285:                                 SubLevelTarget newTarget = new SubLevelTarget(ass.getTarget().getName());
286:
287:•                                if (listElement.getTarget().getIndex() instanceof FreeIndex) {
288:•                                        if (currentIndize.size() == 0) {
289:                                                 currentIndize.add(0);
290:                                                 newTarget.setIndex(new AbsoluteIndex(0));
291:                                         } else {
292:                                                 Integer counter = 0;
293:                                                 boolean foundIndex = false;
294:•                                                while (counter < currentIndize.size()) {
295:•                                                        if (counter < currentIndize.get(counter)) {
296:                                                                 currentIndize.add(counter, counter);
297:                                                                 newTarget.setIndex(new AbsoluteIndex(counter));
298:                                                                 counter = currentIndize.size();
299:                                                                 foundIndex = true;
300:                                                         } else {
301:                                                                 counter++;
302:                                                         }
303:                                                 }
304:•                                                if (foundIndex == false) {
305:                                                         currentIndize.add(counter);
306:                                                         newTarget.setIndex(new AbsoluteIndex(counter));
307:                                                 }
308:                                         }
309:•                                } else if (listElement.getTarget().getIndex() instanceof AbsoluteIndex) {
310:                                         AbsoluteIndex index = (AbsoluteIndex) listElement.getTarget().getIndex();
311:•                                        if (index.getValue() > currentIndize.size()) {
312:                                                 currentIndize.add(index.getValue());
313:                                         } else {
314:                                                 currentIndize.add(index.getValue(), index.getValue());
315:                                         }
316:                                         newTarget.setIndex(index);
317:                                 }
318:                                 newTarget.addSub(target);
319:                                 // TODO: aus den configured Assignments und ass VariableAssignments erstellen und
320:                                 // diese in die Liste einfuegen
321:                                 // mglw. noch Aenderungen noetig
322:                         }
323:                 } catch (ParserException e) {
324:                         // TODO
325:                 }
326:         }
327:
328:         @Override
329:         public void update(final Observable o, final Object arg) {
330:                 this.setChanged();
331:                 this.notifyObservers();
332:         }
333: }