Skip to content

Commit 06f8f36

Browse files
committed
refactoring
1 parent 0f8a6b2 commit 06f8f36

File tree

6 files changed

+54
-46
lines changed

6 files changed

+54
-46
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/JBBPCompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private static void assertTokenNotNamed(final String fieldType, final JBBPToken
177177

178178
private static void assertTokenHasExtraData(final String fieldType, final JBBPToken token) {
179179
if (token.getFieldTypeParameters().getExtraData() == null) {
180-
throw new JBBPCompilationException('\'' + fieldType + "\' doesn't have extra value", token);
180+
throw new JBBPCompilationException('\'' + fieldType + "' doesn't have extra value", token);
181181
}
182182
}
183183

@@ -511,7 +511,7 @@ public static JBBPCompiledBlock compile(final String script,
511511
private static void assertTokenHasNotExtraData(final String fieldType,
512512
final JBBPToken token) {
513513
if (token.getFieldTypeParameters().getExtraData() != null) {
514-
throw new JBBPCompilationException('\'' + fieldType + "\' has extra value", token);
514+
throw new JBBPCompilationException('\'' + fieldType + "' has extra value", token);
515515
}
516516
}
517517

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/tokenizer/JBBPTokenizer.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ public static boolean isGlobalReservedName(final String name) {
135135
return GLOBAL_RESERVED_TYPE_NAMES.contains(name);
136136
}
137137

138+
private static JBBPByteOrder getJbbpByteOrder(String groupTypeByteOrder, String fieldType) {
139+
JBBPByteOrder byteOrder;
140+
if (groupTypeByteOrder != null) {
141+
if (">".equals(groupTypeByteOrder)) {
142+
byteOrder = JBBPByteOrder.BIG_ENDIAN;
143+
} else if ("<".equals(groupTypeByteOrder)) {
144+
byteOrder = JBBPByteOrder.LITTLE_ENDIAN;
145+
} else {
146+
throw new Error(
147+
"Illegal byte order char, unexpected error, contact developer please ["
148+
+ fieldType + ']');
149+
}
150+
} else {
151+
byteOrder = JBBPByteOrder.BIG_ENDIAN;
152+
}
153+
return byteOrder;
154+
}
155+
138156
/**
139157
* Inside method to read the next token from the string and place it into
140158
* inside storage.
@@ -222,20 +240,7 @@ private void readNextItem() {
222240

223241
wrongFormat = false;
224242

225-
JBBPByteOrder byteOrder;
226-
if (groupTypeByteOrder != null) {
227-
if (">".equals(groupTypeByteOrder)) {
228-
byteOrder = JBBPByteOrder.BIG_ENDIAN;
229-
} else if ("<".equals(groupTypeByteOrder)) {
230-
byteOrder = JBBPByteOrder.LITTLE_ENDIAN;
231-
} else {
232-
throw new Error(
233-
"Illegal byte order char, unexpected error, contact developer please ["
234-
+ fieldType + ']');
235-
}
236-
} else {
237-
byteOrder = JBBPByteOrder.BIG_ENDIAN;
238-
}
243+
JBBPByteOrder byteOrder = getJbbpByteOrder(groupTypeByteOrder, fieldType);
239244

240245
parsedType =
241246
new JBBPFieldTypeParameterContainer(byteOrder, groupTypeName, groupTypeExtraField);

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/varlen/JBBPExpressionEvaluator.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -362,29 +362,7 @@ public JBBPExpressionEvaluator(final String expression,
362362

363363
prevoperator = false;
364364
try {
365-
int parsed = Integer.parseInt(number);
366-
367-
if (unaryOperatorCode >= 0) {
368-
switch (unaryOperatorCode) {
369-
case CODE_UNARYPLUS:
370-
case CODE_ADD: {
371-
// do nothing
372-
}
373-
break;
374-
case CODE_UNARYMINUS:
375-
case CODE_MINUS: {
376-
parsed = -parsed;
377-
}
378-
break;
379-
case CODE_NOT: {
380-
parsed = ~parsed;
381-
}
382-
break;
383-
default: {
384-
throw new Error("Unsupported unary operator [" + SYMBOLS[unaryOperatorCode] + ']');
385-
}
386-
}
387-
}
365+
int parsed = getParsed(number, unaryOperatorCode);
388366

389367
unaryOperatorCode = -1;
390368
compiledScript.write(CODE_CONST);
@@ -437,6 +415,33 @@ public JBBPExpressionEvaluator(final String expression,
437415
this.maxStackDepth = calculateMaxStackDepth();
438416
}
439417

418+
private static int getParsed(String number, int unaryOperatorCode) {
419+
int parsed = Integer.parseInt(number);
420+
421+
if (unaryOperatorCode >= 0) {
422+
switch (unaryOperatorCode) {
423+
case CODE_UNARYPLUS:
424+
case CODE_ADD: {
425+
// do nothing
426+
}
427+
break;
428+
case CODE_UNARYMINUS:
429+
case CODE_MINUS: {
430+
parsed = -parsed;
431+
}
432+
break;
433+
case CODE_NOT: {
434+
parsed = ~parsed;
435+
}
436+
break;
437+
default: {
438+
throw new Error("Unsupported unary operator [" + SYMBOLS[unaryOperatorCode] + ']');
439+
}
440+
}
441+
}
442+
return parsed;
443+
}
444+
440445
/**
441446
* Encode code of an operator to code of similar unary operator.
442447
*

jbbp/src/main/java/com/igormaznitsa/jbbp/exceptions/JBBPTokenizerException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public JBBPTokenizerException(final String message, final String script, final i
5555
* @since 2.0.3
5656
*/
5757
private static String extractErrorPartText(final String script, final int errorPosition) {
58-
if (script.isEmpty() || errorPosition >= script.length() || errorPosition < 0) {
58+
if (errorPosition >= script.length() || errorPosition < 0) {
5959
return "";
6060
}
6161
final int maxLengthWing = 16;

jbbp/src/main/java/com/igormaznitsa/jbbp/model/JBBPFieldStruct.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.igormaznitsa.jbbp.model;
1818

19+
import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_FIELD_EMPTY;
20+
1921
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
2022
import com.igormaznitsa.jbbp.exceptions.JBBPFinderException;
2123
import com.igormaznitsa.jbbp.exceptions.JBBPTooManyFieldsFoundException;
@@ -25,11 +27,8 @@
2527
import com.igormaznitsa.jbbp.model.finder.JBBPFieldFinder;
2628
import com.igormaznitsa.jbbp.utils.Function;
2729
import com.igormaznitsa.jbbp.utils.JBBPUtils;
28-
2930
import java.util.List;
3031

31-
import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_FIELD_EMPTY;
32-
3332
/**
3433
* Describes a structure.
3534
*
@@ -86,7 +85,6 @@ public JBBPAbstractField findFieldForPath(final String fieldPath) {
8685
firstIndex = 0;
8786
} else if (parsedName[0].equals(this.getNameInfo().getFieldName())) {
8887
firstIndex = 1;
89-
found = this;
9088
} else {
9189
firstIndex = 0;
9290
found = null;
@@ -350,7 +348,7 @@ public final <T> T mapTo(final T objectToMap, final BinFieldFilter binFieldFilte
350348
@SafeVarargs
351349
public final <T> T mapTo(final T instance, final int flags,
352350
final Function<Class<?>, Object>... instantiators) {
353-
return this.mapTo(instance, (JBBPMapperCustomFieldProcessor) null, flags, instantiators);
351+
return this.mapTo(instance, null, flags, instantiators);
354352
}
355353

356354
/**

jbbp/src/main/java/com/igormaznitsa/jbbp/utils/JBBPDslBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ protected static class BinFieldContainer extends BinField {
21342134
}
21352135

21362136
BinFieldContainer(final Class<?> klazz, final Field field) {
2137-
super((Bin) null, false, field);
2137+
super(null, false, field);
21382138
this.klazz = klazz;
21392139
}
21402140

0 commit comments

Comments
 (0)