Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4da15fc

Browse files
committed
[Java] Don't keep looking up node by index.
1 parent 9698c38 commit 4da15fc

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/xml/Message.java

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -201,98 +201,101 @@ private List<Field> parseFieldsAndGroups(final Node node) throws XPathExpression
201201

202202
private Field parseGroupField(final NodeList nodeList, final int nodeIndex) throws XPathExpressionException
203203
{
204-
final String dimensionTypeName = getAttributeValue(nodeList.item(nodeIndex), "dimensionType", "groupSizeEncoding");
204+
final Node node = nodeList.item(nodeIndex);
205+
final String dimensionTypeName = getAttributeValue(node, "dimensionType", "groupSizeEncoding");
205206
Type dimensionType = typeByNameMap.get(dimensionTypeName);
206207
if (dimensionType == null)
207208
{
208-
handleError(nodeList.item(nodeIndex), "could not find dimensionType: " + dimensionTypeName);
209+
handleError(node, "could not find dimensionType: " + dimensionTypeName);
209210
}
210211
else if (!(dimensionType instanceof CompositeType))
211212
{
212-
handleError(nodeList.item(nodeIndex), "dimensionType should be a composite type: " + dimensionTypeName);
213+
handleError(node, "dimensionType should be a composite type: " + dimensionTypeName);
213214
dimensionType = null;
214215
}
215216
else
216217
{
217-
((CompositeType)dimensionType).checkForWellFormedGroupSizeEncoding(nodeList.item(nodeIndex));
218+
((CompositeType)dimensionType).checkForWellFormedGroupSizeEncoding(node);
218219
}
219220

220221
final Field field = new Field.Builder()
221-
.name(getAttributeValue(nodeList.item(nodeIndex), "name"))
222-
.description(getAttributeValueOrNull(nodeList.item(nodeIndex), "description"))
223-
.id(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "id")))
224-
.blockLength(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "blockLength", "0")))
225-
.sinceVersion(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "sinceVersion", "0")))
222+
.name(getAttributeValue(node, "name"))
223+
.description(getAttributeValueOrNull(node, "description"))
224+
.id(Integer.parseInt(getAttributeValue(node, "id")))
225+
.blockLength(Integer.parseInt(getAttributeValue(node, "blockLength", "0")))
226+
.sinceVersion(Integer.parseInt(getAttributeValue(node, "sinceVersion", "0")))
226227
.dimensionType((CompositeType)dimensionType)
227228
.build();
228229

229-
XmlSchemaParser.checkForValidName(nodeList.item(nodeIndex), field.name());
230+
XmlSchemaParser.checkForValidName(node, field.name());
230231

231-
field.groupFields(parseFieldsAndGroups(nodeList.item(nodeIndex))); // recursive call
232+
field.groupFields(parseFieldsAndGroups(node)); // recursive call
232233

233234
return field;
234235
}
235236

236237
private Field parseField(final NodeList nodeList, final int nodeIndex)
237238
{
238-
final String typeName = getAttributeValue(nodeList.item(nodeIndex), "type");
239+
final Node node = nodeList.item(nodeIndex);
240+
final String typeName = getAttributeValue(node, "type");
239241
final Type fieldType = typeByNameMap.get(typeName);
240242
if (fieldType == null)
241243
{
242-
handleError(nodeList.item(nodeIndex), "could not find type: " + typeName);
244+
handleError(node, "could not find type: " + typeName);
243245
}
244246

245247
final Field field = new Field.Builder()
246-
.name(getAttributeValue(nodeList.item(nodeIndex), "name"))
247-
.description(getAttributeValueOrNull(nodeList.item(nodeIndex), "description"))
248-
.id(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "id")))
249-
.offset(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "offset", "0")))
250-
.semanticType(getAttributeValueOrNull(nodeList.item(nodeIndex), "semanticType"))
251-
.presence(Presence.get(getAttributeValue(nodeList.item(nodeIndex), "presence", "required")))
252-
.sinceVersion(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "sinceVersion", "0")))
253-
.epoch(getAttributeValue(nodeList.item(nodeIndex), "epoch", "unix"))
254-
.timeUnit(getAttributeValue(nodeList.item(nodeIndex), "timeUnit", "nanosecond"))
248+
.name(getAttributeValue(node, "name"))
249+
.description(getAttributeValueOrNull(node, "description"))
250+
.id(Integer.parseInt(getAttributeValue(node, "id")))
251+
.offset(Integer.parseInt(getAttributeValue(node, "offset", "0")))
252+
.semanticType(getAttributeValueOrNull(node, "semanticType"))
253+
.presence(Presence.get(getAttributeValue(node, "presence", "required")))
254+
.sinceVersion(Integer.parseInt(getAttributeValue(node, "sinceVersion", "0")))
255+
.epoch(getAttributeValue(node, "epoch", "unix"))
256+
.timeUnit(getAttributeValue(node, "timeUnit", "nanosecond"))
255257
.type(fieldType)
256258
.build();
257259

258-
field.validate(nodeList.item(nodeIndex));
260+
field.validate(node);
259261

260262
return field;
261263
}
262264

263265
private Field parseDataField(final NodeList nodeList, final int nodeIndex)
264266
{
265-
final String typeName = getAttributeValue(nodeList.item(nodeIndex), "type");
267+
final Node node = nodeList.item(nodeIndex);
268+
final String typeName = getAttributeValue(node, "type");
266269
final Type fieldType = typeByNameMap.get(typeName);
267270
if (fieldType == null)
268271
{
269-
handleError(nodeList.item(nodeIndex), "could not find type: " + typeName);
272+
handleError(node, "could not find type: " + typeName);
270273
}
271274
else if (!(fieldType instanceof CompositeType))
272275
{
273-
handleError(nodeList.item(nodeIndex), "data type is not composite type: " + typeName);
276+
handleError(node, "data type is not composite type: " + typeName);
274277
}
275278
else
276279
{
277-
((CompositeType)fieldType).checkForWellFormedVariableLengthDataEncoding(nodeList.item(nodeIndex));
280+
((CompositeType)fieldType).checkForWellFormedVariableLengthDataEncoding(node);
278281
((CompositeType)fieldType).makeDataFieldCompositeType();
279282
}
280283

281284
final Field field = new Field.Builder()
282-
.name(getAttributeValue(nodeList.item(nodeIndex), "name"))
283-
.description(getAttributeValueOrNull(nodeList.item(nodeIndex), "description"))
284-
.id(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "id")))
285-
.offset(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "offset", "0")))
286-
.semanticType(getAttributeValueOrNull(nodeList.item(nodeIndex), "semanticType"))
287-
.presence(Presence.get(getAttributeValue(nodeList.item(nodeIndex), "presence", "required")))
288-
.sinceVersion(Integer.parseInt(getAttributeValue(nodeList.item(nodeIndex), "sinceVersion", "0")))
289-
.epoch(getAttributeValue(nodeList.item(nodeIndex), "epoch", "unix"))
290-
.timeUnit(getAttributeValue(nodeList.item(nodeIndex), "timeUnit", "nanosecond"))
285+
.name(getAttributeValue(node, "name"))
286+
.description(getAttributeValueOrNull(node, "description"))
287+
.id(Integer.parseInt(getAttributeValue(node, "id")))
288+
.offset(Integer.parseInt(getAttributeValue(node, "offset", "0")))
289+
.semanticType(getAttributeValueOrNull(node, "semanticType"))
290+
.presence(Presence.get(getAttributeValue(node, "presence", "required")))
291+
.sinceVersion(Integer.parseInt(getAttributeValue(node, "sinceVersion", "0")))
292+
.epoch(getAttributeValue(node, "epoch", "unix"))
293+
.timeUnit(getAttributeValue(node, "timeUnit", "nanosecond"))
291294
.type(fieldType)
292295
.variableLength(true)
293296
.build();
294297

295-
field.validate(nodeList.item(nodeIndex));
298+
field.validate(node);
296299

297300
return field;
298301
}

0 commit comments

Comments
 (0)