Skip to content

Commit de3c234

Browse files
committed
refactoring
1 parent ede4b9d commit de3c234

25 files changed

+156
-226
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/compiler/conversion/JBBPToJavaConverter.java

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -82,64 +82,15 @@ public final class JBBPToJavaConverter extends CompiledBlockVisitor {
8282
private static final String NAME_OUTPUT_STREAM = "Out";
8383

8484
static {
85-
final Set<String> reserved = new HashSet<>();
86-
87-
reserved.add("abstract");
88-
reserved.add("assert");
89-
reserved.add("boolean");
90-
reserved.add("break");
91-
reserved.add("byte");
92-
reserved.add("case");
93-
reserved.add("catch");
94-
reserved.add("char");
95-
reserved.add("class");
96-
reserved.add("continue");
97-
reserved.add("default");
98-
reserved.add("do");
99-
reserved.add("double");
100-
reserved.add("else");
101-
reserved.add("enum");
102-
reserved.add("extends");
103-
reserved.add("final");
104-
reserved.add("finally");
105-
reserved.add("float");
106-
reserved.add("for");
107-
reserved.add("if");
108-
reserved.add("implements");
109-
reserved.add("import");
110-
reserved.add("instanceof");
111-
reserved.add("int");
112-
reserved.add("interface");
113-
reserved.add("long");
114-
reserved.add("native");
115-
reserved.add("new");
116-
reserved.add("package");
117-
reserved.add("private");
118-
reserved.add("protected");
119-
reserved.add("public");
120-
reserved.add("return");
121-
reserved.add("short");
122-
reserved.add("static");
123-
reserved.add("strictfp");
124-
reserved.add("super");
125-
reserved.add("switch");
126-
reserved.add("synchronized");
127-
reserved.add("this");
128-
reserved.add("throw");
129-
reserved.add("throws");
130-
reserved.add("transient");
131-
reserved.add("try");
132-
reserved.add("void");
133-
reserved.add("volatile");
134-
reserved.add("while");
135-
reserved.add("true");
136-
reserved.add("null");
137-
reserved.add("false");
138-
reserved.add("var");
139-
reserved.add("const");
140-
reserved.add("goto");
141-
142-
RESERVED_JAVA_KEYWORDS = Collections.unmodifiableSet(reserved);
85+
86+
RESERVED_JAVA_KEYWORDS =
87+
Set.of("abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class",
88+
"continue", "default", "do", "double", "else", "enum", "extends", "final", "finally",
89+
"float", "for", "if", "implements", "import", "instanceof", "int", "interface", "long",
90+
"native", "new", "package", "private", "protected", "public", "return", "short",
91+
"static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws",
92+
"transient", "try", "void", "volatile", "while", "true", "null", "false", "var",
93+
"const", "goto");
14394
}
14495

14596
/**
@@ -258,7 +209,7 @@ public void visitEnd() {
258209
buffer.printCommentMultiLinesWithIndent(this.builder.headComment);
259210
}
260211

261-
if (this.builder.mainClassPackage != null && this.builder.mainClassPackage.length() != 0) {
212+
if (this.builder.mainClassPackage != null && !this.builder.mainClassPackage.isEmpty()) {
262213
buffer.print("package ").print(this.builder.mainClassPackage).println(";");
263214
}
264215

@@ -377,7 +328,7 @@ public void visitEnd() {
377328
this.builder.mapSubClassesInterfaces,
378329
this.builder.mapSubClassesSuperclasses,
379330
this.specialSection.toString(),
380-
specialMethodsText.length() == 0 ? null : specialMethodsText,
331+
specialMethodsText.isEmpty() ? null : specialMethodsText,
381332
this.builder.mainClassCustomText,
382333
true
383334
);
@@ -429,10 +380,10 @@ public void visitStructureStart(final int offsetInCompiledBlock,
429380
this.getCurrentStruct().getReadFunc().indent()
430381
.printf("if ( this.%1$s == null) { this.%1$s = new %2$s(%3$s);}", structName, structType,
431382
pathToRootObject)
432-
.printf(" %s.read(%s);%n", toType.length() == 0 ? "this." + structName :
383+
.printf(" %s.read(%s);%n", toType.isEmpty() ? "this." + structName :
433384
'(' + toType + "this." + structName + ')', NAME_INPUT_STREAM);
434385
this.getCurrentStruct().getWriteFunc().indent()
435-
.print(toType.length() == 0 ? structName : '(' + toType + structName + ')')
386+
.print(toType.isEmpty() ? structName : '(' + toType + structName + ')')
436387
.println(".write(Out);");
437388
} else {
438389
structType = structBaseTypeName + " []";
@@ -453,7 +404,7 @@ public void visitStructureStart(final int offsetInCompiledBlock,
453404
NAME_INPUT_STREAM);
454405
this.getCurrentStruct().getWriteFunc().indent()
455406
.printf("for (int I=0;I<this.%1$s.length;I++){ %2$s.write(%3$s); }%n", structName,
456-
toType.length() == 0 ? "this." + structName + "[I]" :
407+
toType.isEmpty() ? "this." + structName + "[I]" :
457408
'(' + toType + "this." + structName + "[I])", NAME_OUTPUT_STREAM);
458409
} else {
459410
this.getCurrentStruct().getReadFunc().indent()
@@ -1129,7 +1080,7 @@ private String arg2str(final Object obj) {
11291080
}
11301081
} else if (obj instanceof String) {
11311082
return String.format("%s.getNamedValue(this, \"%s\")",
1132-
(getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT), obj.toString());
1083+
(getCurrentStruct().isRoot() ? "this" : "this." + NAME_ROOT_STRUCT), obj);
11331084
} else if (obj instanceof JBBPNamedFieldInfo) {
11341085
final NamedFieldInfo namedFieldInfo = foundNamedFields.get(obj);
11351086
final String fieldPath = namedFieldInfo.makeSrcPath(getCurrentStruct());
@@ -1186,7 +1137,7 @@ public ExpressionEvaluatorVisitor visitEnd() {
11861137

11871138
final Object result = this.stack.remove(0);
11881139
if (result instanceof ExprTreeItem) {
1189-
buffer.append('(').append(result.toString()).append(')');
1140+
buffer.append('(').append(result).append(')');
11901141
} else {
11911142
buffer.append(arg2str(result));
11921143
}
@@ -1778,7 +1729,7 @@ private static class Struct {
17781729
private final String path;
17791730

17801731
private Struct(final Struct parent, final String className, final String classModifiers) {
1781-
this.path = parent == null ? "" : parent.path + (parent.path.length() == 0 ? "" : ".") +
1732+
this.path = parent == null ? "" : parent.path + (parent.path.isEmpty() ? "" : ".") +
17821733
className.toLowerCase(Locale.ENGLISH);
17831734
this.classModifiers = classModifiers;
17841735
this.className = className;
@@ -1909,7 +1860,7 @@ void write(
19091860
buffer.println();
19101861
}
19111862

1912-
if (customText != null && customText.length() != 0) {
1863+
if (customText != null && !customText.isEmpty()) {
19131864
buffer.printCommentLinesWithIndent("------ Custom section START");
19141865
buffer.printLinesWithIndent(customText);
19151866
buffer.printCommentLinesWithIndent("------ Custom section END");

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

Lines changed: 22 additions & 17 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.
@@ -153,7 +171,7 @@ private void readNextItem() {
153171
final String skipString =
154172
this.processingString.substring(Math.max(this.lastCharSubstringFound, 0), matcher.start())
155173
.trim();
156-
if (skipString.length() != 0 && !skipString.startsWith("//")) {
174+
if (!skipString.isEmpty() && !skipString.startsWith("//")) {
157175
this.detectedException =
158176
new JBBPTokenizerException(skipString, this.processingString,
159177
Math.max(this.lastCharSubstringFound, 0));
@@ -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);
@@ -259,7 +264,7 @@ private void readNextItem() {
259264
new JBBPTokenizerException("Wrong format of whole string", this.processingString, 0);
260265
} else {
261266
final String restOfString = this.processingString.substring(this.lastCharSubstringFound);
262-
if (restOfString.trim().length() != 0) {
267+
if (!restOfString.trim().isEmpty()) {
263268
throw new JBBPTokenizerException(
264269
"Can't recognize a part of script [" + restOfString + ']',
265270
this.processingString,
@@ -286,7 +291,7 @@ private JBBPTokenizerException checkFieldName(final String name, final int posit
286291
this.processingString, position);
287292
}
288293

289-
if (normalized.length() > 0) {
294+
if (!normalized.isEmpty()) {
290295
if (normalized.equals("_")
291296
|| normalized.equals("$$")
292297
|| normalized.startsWith("$")

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

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public JBBPExpressionEvaluator(final String expression,
193193
if (lastFound >= 0) {
194194
// check for skipped substring
195195
final String subString = expression.substring(lastFound, matcher.start());
196-
if (subString.trim().length() != 0) {
196+
if (!subString.trim().isEmpty()) {
197197
throw new JBBPCompilationException(
198198
"Can't recognize part of expression '" + subString + "' [" + expression + ']');
199199
}
@@ -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.length() == 0 || errorPosition >= script.length() || errorPosition < 0) {
58+
if (script.isEmpty() || errorPosition >= script.length() || errorPosition < 0) {
5959
return "";
6060
}
6161
final int maxLengthWing = 16;

jbbp/src/main/java/com/igormaznitsa/jbbp/io/JBBPBitOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public void resetCounter() {
400400
public void writeString(final String value, final JBBPByteOrder order) throws IOException {
401401
if (value == null) {
402402
this.write(0xFF);
403-
} else if (value.length() == 0) {
403+
} else if (value.isEmpty()) {
404404
this.write(0);
405405
} else {
406406
final byte[] array = JBBPUtils.strToUtf8(value);

jbbp/src/main/java/com/igormaznitsa/jbbp/io/JBBPOut.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @since 1.0
3636
*/
37-
public final class JBBPOut extends AbstractMappedClassFieldObserver {
37+
public class JBBPOut extends AbstractMappedClassFieldObserver {
3838

3939
/**
4040
* The Default byte outOrder.

jbbp/src/main/java/com/igormaznitsa/jbbp/mapper/Bin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
* The annotation describes a field in a class which can be mapped and loaded
3333
* from parsed a JBBP structure, also it can be used for whole class but in the
3434
* case be careful and use default name and path values. The Class is not thread safe.
35-
*
3635
* <b>Since 2.0.0 was removed prefix 'out' for fields which contained it</b>.
3736
*
3837
* @since 1.0

jbbp/src/main/java/com/igormaznitsa/jbbp/mapper/JBBPMapper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
import com.igormaznitsa.jbbp.utils.JBBPUtils;
2626
import com.igormaznitsa.jbbp.utils.NullableTriple;
2727
import com.igormaznitsa.jbbp.utils.ReflectUtils;
28-
2928
import java.lang.reflect.Field;
3029
import java.lang.reflect.Method;
3130
import java.lang.reflect.Modifier;
32-
import java.util.*;
31+
import java.util.ArrayList;
32+
import java.util.Collections;
33+
import java.util.List;
34+
import java.util.Locale;
35+
import java.util.Map;
3336
import java.util.concurrent.ConcurrentHashMap;
3437

3538
/**
@@ -239,8 +242,8 @@ private static void processFieldOfMappedClass(
239242
} else {
240243
final JBBPAbstractField binField;
241244

242-
if (record.fieldPath.length() == 0) {
243-
binField = record.fieldName.length() == 0 ?
245+
if (record.fieldPath.isEmpty()) {
246+
binField = record.fieldName.isEmpty() ?
244247
rootStructure.findFieldForType(record.fieldType.getFieldClass()) : rootStructure
245248
.findFieldForNameAndType(record.fieldName, record.fieldType.getFieldClass());
246249
} else {

jbbp/src/main/java/com/igormaznitsa/jbbp/mapper/MappedFieldRecord.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public final class MappedFieldRecord implements Comparable<MappedFieldRecord> {
230230
this.bitWideField = this.fieldType == BinType.BIT || fieldType == BinType.BIT_ARRAY;
231231

232232
this.fieldName =
233-
binAnnotation.name().length() == 0 ? mappingField.getName() : binAnnotation.name();
233+
binAnnotation.name().isEmpty() ? mappingField.getName() : binAnnotation.name();
234234
this.fieldPath = binAnnotation.path();
235235

236236
if (this.mappingField.getType().isArray()) {
@@ -609,7 +609,7 @@ public int compareTo(final MappedFieldRecord o) {
609609
return result;
610610
}
611611

612-
interface FieldProcessor {
612+
public interface FieldProcessor {
613613
@SuppressWarnings("unchecked")
614614
void apply(
615615
MappedFieldRecord record,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public JBBPAbstractArrayField(final JBBPNamedFieldInfo name) {
6969
*/
7070
@Override
7171
public Iterator<T> iterator() {
72-
return new Iterator<T>() {
72+
return new Iterator<>() {
7373
private int index = 0;
7474

7575
@Override

0 commit comments

Comments
 (0)