Skip to content

Package: DefinitionFile

DefinitionFile

nameinstructionbranchcomplexitylinemethod
DefinitionFile()
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%
DefinitionFile(Map, List)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
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%
createPosDefMap()
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%
equals(Object)
M: 0 C: 21
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 3
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%
getDefinitions()
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: 7 C: 17
71%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 5
71%
M: 0 C: 1
100%
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%

Coverage

1: package model.definition;
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:
12: import model.Comment;
13: import model.Position;
14: import printer.PrinterException;
15: import printer.PrinterUtility;
16:
17: /**
18: * DefinitionFileFile represents the "\check\<package>.txt" files.
19: *
20: * @author HFW410RA - Philipp Rammos
21: *
22: */
23: public class DefinitionFile {
24:         /**
25:          * HashMap of all definitions from the DefinitionFile.
26:          */
27:         private final Map<String, AbstractVariableDefinition> definitions;
28:
29:         /**
30:          * HashMap of all comments from the AssignmentFile.
31:          */
32:         private final java.util.List<Comment> comments;
33:
34:         /**
35:          * Default constructor.
36:          */
37:         public DefinitionFile() {
38:                 this.definitions = new HashMap<String, AbstractVariableDefinition>();
39:                 this.comments = new ArrayList<Comment>();
40:         }
41:
42:         /**
43:          * Constructor, just sets the fields.
44:          *
45:          * @param definitions2
46:          * The definitions to set.
47:          * @param comments
48:          * The comments to set.
49:          */
50:         public DefinitionFile(final Map<String, AbstractVariableDefinition> definitions2,
51:                         final List<Comment> comments) {
52:                 this.definitions = definitions2;
53:                 this.comments = comments;
54:         }
55:
56:         @Override
57:         public boolean equals(final Object obj) {
58:•                return obj instanceof DefinitionFile
59:•                                && ((DefinitionFile) obj).getComments().equals(this.getComments())
60:•                                && ((DefinitionFile) obj).getDefinitions().equals(this.getDefinitions());
61:         }
62:
63:         @Override
64:         public int hashCode() {
65:                 return this.getComments().hashCode() + this.getDefinitions().hashCode();
66:         }
67:
68:         /**
69:          * Returns the field definitions. No side effects.
70:          *
71:          * @return this.definitions
72:          */
73:         public java.util.Map<String, AbstractVariableDefinition> getDefinitions() {
74:                 return this.definitions;
75:         }
76:
77:         /**
78:          * @return the comments
79:          */
80:         public java.util.List<Comment> getComments() {
81:                 return this.comments;
82:         }
83:
84:         /**
85:          * Prints a DefinitionFile containing the VariableDefinitions and Comments.
86:          *
87:          * @param pathToPrint
88:          * The path where to print the file.
89:          * @throws printer.PrinterException
90:          * If a IOException is thrown.
91:          * @throws printer.PrinterException
92:          * @return Returns a string representing the printed lines (for testing).
93:          */
94:         public String print(final String pathToPrint) throws printer.PrinterException {
95:
96:                 final StringBuffer stringBuffer = prepareStringToPrint();
97:
98:                 printTheLinesToFile(pathToPrint, stringBuffer);
99:
100:                 return stringBuffer.toString();
101:         }
102:
103:         /**
104:          * Prepares the StringBuffer for printing. After this method, the buffer is ready for printing
105:          * and it will contain the needed lines.
106:          *
107:          * @return The print-ready Buffer.
108:          */
109:         private StringBuffer prepareStringToPrint() {
110:                 final PrinterUtility printUt = new PrinterUtility();
111:                 final StringBuffer stringBuffer = new StringBuffer();
112:                 final List<Position> posSorted = sortPositions();
113:                 final Map<Position, Comment> commentMap = createPosCommentMap();
114:                 final Map<Position, AbstractVariableDefinition> defMap = createPosDefMap();
115:
116:                 final Iterator<Position> posIt = posSorted.iterator();
117:                 int row = 0;
118:                 int col = 0;
119:                 int size = 0;
120:•                while (posIt.hasNext()) {
121:                         final Position current = posIt.next();
122:•                        if (row < current.getRow()) {
123:                                 row += printUt.writeLineBreaks(stringBuffer, row, current.getRow());
124:                                 col = 0;
125:                         }
126:•                        if (row == current.getRow() && col < current.getColumn()) {
127:                                 col += printUt.writeSpaces(stringBuffer, col, current.getColumn());
128:                         }
129:•                        if (defMap.containsKey(current)) {
130:                                 final AbstractVariableDefinition defToPrint = defMap.get(current);
131:                                 stringBuffer.append(defToPrint.print());
132:                                 size = defToPrint.print().length();
133:                         } else {
134:                                 final Comment commentToPrint = commentMap.get(current);
135:                                 stringBuffer.append(commentToPrint.print());
136:                                 size = commentToPrint.print().length();
137:                         }
138:                         col += size;
139:
140:                 }
141:                 return stringBuffer;
142:         }
143:
144:         /**
145:          * Creates a Map containing definition.getPosition() as Key and definition as Value. No side
146:          * effects to fields.
147:          *
148:          * @return The created map.
149:          */
150:         private Map<Position, AbstractVariableDefinition> createPosDefMap() {
151:                 final Map<Position, AbstractVariableDefinition> defMap =
152:                                 new HashMap<Position, AbstractVariableDefinition>();
153:                 final Iterator<AbstractVariableDefinition> defIt = this.definitions.values().iterator();
154:•                while (defIt.hasNext()) {
155:                         final AbstractVariableDefinition current = defIt.next();
156:                         defMap.put(current.getPosition(), current);
157:                 }
158:                 return defMap;
159:         }
160:
161:         /**
162:          * Creates a Map containing comment.getPosition() as Key and comment as Value.No side effects to
163:          * fields.
164:          *
165:          * @return The created map.
166:          */
167:         private Map<Position, Comment> createPosCommentMap() {
168:                 final Map<Position, Comment> commentMap = new HashMap<Position, Comment>();
169:                 final Iterator<Comment> commentIt = this.comments.iterator();
170:•                while (commentIt.hasNext()) {
171:                         final Comment current = commentIt.next();
172:                         commentMap.put(current.getPosition(), current);
173:                 }
174:                 return commentMap;
175:         }
176:
177:         /**
178:          * The only one method, that real prints lines into the file.
179:          *
180:          * @param pathToPrint
181:          * The path where to print the file-
182:          * @param stringBuffer
183:          * The buffer with the lines to print.
184:          * @throws PrinterException
185:          * If an IOException is caused by the printer.
186:          */
187:         private void printTheLinesToFile(final String pathToPrint, final StringBuffer stringBuffer)
188:                         throws PrinterException {
189:                 try {
190:                         final OutputStreamWriter writer =
191:                                         new OutputStreamWriter(new FileOutputStream(pathToPrint),
192:                                                         basic.PrinterConstants.ENCODING);
193:                         try {
194:                                 writer.write(stringBuffer.toString());
195:                         } finally {
196:                                 writer.close();
197:                         }
198:                 } catch (final IOException ioEx) {
199:                         throw new PrinterException(basic.PrinterConstants.PRINTEXCEPTION, ioEx);
200:
201:                 }
202:         }
203:
204:         /**
205:          * Sorts the Positions of VariableAssignments and Comments in one List.
206:          *
207:          * @return The list of sorted Positions.
208:          */
209:         private List<Position> sortPositions() {
210:                 final List<Position> sortedPositions = new ArrayList<Position>();
211:                 final Iterator<Comment> commantIt = this.comments.iterator();
212:•                while (commantIt.hasNext()) {
213:                         sortedPositions.add(commantIt.next().getPosition());
214:                 }
215:                 final Iterator<AbstractVariableDefinition> defIt = this.definitions.values().iterator();
216:•                while (defIt.hasNext()) {
217:                         sortedPositions.add(defIt.next().getPosition());
218:                 }
219:                 java.util.Collections.sort(sortedPositions);
220:                 return sortedPositions;
221:         }
222:
223: }