Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
raydac committed Nov 24, 2018
1 parent 6ca79d5 commit e050d0a
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 20 deletions.
Expand Up @@ -28,6 +28,8 @@
import java.util.Locale;
import java.util.Set;

import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_STRING_EMPTY;

/**
* Task to translate found JBBP scripts in source files.
*
Expand Down Expand Up @@ -58,7 +60,7 @@ protected void doTaskAction(@Nonnull final JBBPExtension ext) {
}
getLogger().debug("Defined normalized custom types : " + normalizedCustomTypeNames);
}
final String[] customTypesArray = normalizedCustomTypeNames.toArray(new String[normalizedCustomTypeNames.size()]);
final String[] customTypesArray = normalizedCustomTypeNames.toArray(ARRAY_STRING_EMPTY);

final JBBPCustomFieldTypeProcessor customFieldProcessor = new JBBPCustomFieldTypeProcessor() {
@Override
Expand Down
Expand Up @@ -44,6 +44,7 @@
import java.util.Set;

import static com.igormaznitsa.jbbp.plugin.common.utils.CommonUtils.ensureEncodingName;
import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_STRING_EMPTY;

/**
* The Mojo looks for all JBBP scripts in source and generate sources.
Expand Down Expand Up @@ -330,7 +331,7 @@ protected void executeMojo() throws MojoExecutionException, MojoFailureException
}
getLog().debug("Defined normalized custom types : " + normalizedCustomTypeNames);

final String[] customTypesArray = normalizedCustomTypeNames.toArray(new String[normalizedCustomTypeNames.size()]);
final String[] customTypesArray = normalizedCustomTypeNames.toArray(ARRAY_STRING_EMPTY);

final JBBPCustomFieldTypeProcessor customFieldProcessor = new JBBPCustomFieldTypeProcessor() {
@Override
Expand Down
Expand Up @@ -15,6 +15,8 @@
import java.util.Collections;
import java.util.Set;

import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_STRING_EMPTY;

public class Java16Converter implements JBBPScriptTranslator {
@Override
@Nonnull
Expand All @@ -33,7 +35,7 @@ public Set<File> translate(@Nonnull final Parameters parameters, final boolean d

final JBBPParser parser = JBBPParser.prepare(text, JBBPBitOrder.LSB0, parameters.customFieldTypeProcessor, parameters.getParserFlags());

final String[] implementsSorted = parameters.getClassImplements().toArray(new String[parameters.getClassImplements().size()]);
final String[] implementsSorted = parameters.getClassImplements().toArray(ARRAY_STRING_EMPTY);
Arrays.sort(implementsSorted);

final JBBPToJava6Converter.Builder builder = JBBPToJava6Converter.makeBuilder(parser)
Expand Down
6 changes: 4 additions & 2 deletions jbbp/src/main/java/com/igormaznitsa/jbbp/JBBPParser.java
Expand Up @@ -68,6 +68,8 @@
import java.util.Map;
import java.util.Properties;

import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_FIELD_EMPTY;

/**
* the Main class allows a user to parse a binary stream or block for predefined
* and precompiled script.
Expand Down Expand Up @@ -508,7 +510,7 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream, f
// skip offset
JBBPUtils.unpackInt(compiled, positionAtCompiledBlock);
if (resultNotIgnored) {
structureFields.add(new JBBPFieldStruct(name, structFields.toArray(new JBBPAbstractField[structFields.size()])));
structureFields.add(new JBBPFieldStruct(name, structFields.toArray(ARRAY_FIELD_EMPTY)));
}
} else {
final int nameFieldCurrent = positionAtNamedFieldList.get();
Expand All @@ -533,7 +535,7 @@ private List<JBBPAbstractField> parseStruct(final JBBPBitInputStream inStream, f
}
}

result = list.isEmpty() ? EMPTY_STRUCT_ARRAY : list.toArray(new JBBPFieldStruct[list.size()]);
result = list.isEmpty() ? EMPTY_STRUCT_ARRAY : list.toArray(EMPTY_STRUCT_ARRAY);
} else {
// read number of items
if (arrayLength == 0) {
Expand Down
Expand Up @@ -33,6 +33,10 @@
*/
public final class JBBPCompiledBlock {

private static final JBBPNamedFieldInfo[] ARRAY_FIELDINFO_EMPTY = new JBBPNamedFieldInfo[0];
private static final JBBPIntegerValueEvaluator[] ARRAY_INTEVALUATOR_EMPTY = new JBBPIntegerValueEvaluator[0];
private static final JBBPFieldTypeParameterContainer[] ARRAY_FIELDTYPE_EMPTY = new JBBPFieldTypeParameterContainer[0];

/**
* The Array of named field info items.
*/
Expand Down Expand Up @@ -211,7 +215,7 @@ public JBBPCompiledBlock build() {
JBBPUtils.assertNotNull(source, "Source is not defined");
JBBPUtils.assertNotNull(compiledData, "Compiled data is not defined");

return new JBBPCompiledBlock(this.source, this.namedFields.toArray(new JBBPNamedFieldInfo[this.namedFields.size()]), this.varLenProcessors.isEmpty() ? null : this.varLenProcessors.toArray(new JBBPIntegerValueEvaluator[this.varLenProcessors.size()]), this.compiledData, this.hasVarFields, this.customTypeFields.toArray(new JBBPFieldTypeParameterContainer[this.customTypeFields.size()]));
return new JBBPCompiledBlock(this.source, this.namedFields.toArray(ARRAY_FIELDINFO_EMPTY), this.varLenProcessors.isEmpty() ? null : this.varLenProcessors.toArray(ARRAY_INTEVALUATOR_EMPTY), this.compiledData, this.hasVarFields, this.customTypeFields.toArray(ARRAY_FIELDTYPE_EMPTY));
}

/**
Expand Down
Expand Up @@ -163,15 +163,13 @@ private static void assertTokenNotArray(final String fieldType, final JBBPToken

private static void assertTokenNamed(final String fieldType, final JBBPToken token) {
if (token.getFieldName() == null) {
final String fieldName = token.getFieldName() == null ? "<ANONYM>" : token.getFieldName();
throw new JBBPCompilationException('\'' + fieldType + "' must be named (" + fieldName + ')', token);
throw new JBBPCompilationException('\'' + fieldType + "' must be named", token);
}
}

private static void assertTokenNotNamed(final String fieldType, final JBBPToken token) {
if (token.getFieldName() != null) {
final String fieldName = token.getFieldName() == null ? "<ANONYM>" : token.getFieldName();
throw new JBBPCompilationException('\'' + fieldType + "' must not be named (" + fieldName + ')', token);
throw new JBBPCompilationException('\'' + fieldType + "' must not be named (" + token.getFieldName() + ')', token);
}
}

Expand Down
Expand Up @@ -35,6 +35,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_STRING_EMPTY;

/**
* The Class implements an evaluator which can calculate an expression.
*
Expand Down Expand Up @@ -399,7 +401,7 @@ public JBBPExpressionEvaluator(final String expression, final List<JBBPNamedFiel
}

this.compiledExpression = compiledScript.toByteArray();
this.externalValueNames = externalValueNameList.isEmpty() ? null : externalValueNameList.toArray(new String[externalValueNameList.size()]);
this.externalValueNames = externalValueNameList.isEmpty() ? null : externalValueNameList.toArray(ARRAY_STRING_EMPTY);

this.maxStackDepth = calculateMaxStackDepth();
}
Expand Down
Expand Up @@ -19,12 +19,12 @@
import com.igormaznitsa.jbbp.exceptions.JBBPException;
import com.igormaznitsa.jbbp.exceptions.JBBPIllegalArgumentException;
import com.igormaznitsa.jbbp.mapper.Bin;
import com.igormaznitsa.jbbp.utils.DslBinCustom;
import com.igormaznitsa.jbbp.mapper.BinType;
import com.igormaznitsa.jbbp.model.JBBPFieldInt;
import com.igormaznitsa.jbbp.model.JBBPFieldLong;
import com.igormaznitsa.jbbp.model.JBBPFieldShort;
import com.igormaznitsa.jbbp.model.JBBPFieldString;
import com.igormaznitsa.jbbp.utils.DslBinCustom;
import com.igormaznitsa.jbbp.utils.JBBPUtils;
import com.igormaznitsa.jbbp.utils.ReflectUtils;

Expand Down
Expand Up @@ -26,6 +26,8 @@

import java.util.List;

import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_FIELD_EMPTY;

/**
* Describes a structure.
*
Expand Down Expand Up @@ -59,7 +61,7 @@ public JBBPFieldStruct(final JBBPNamedFieldInfo name, final JBBPAbstractField[]
* @param fields a field list, it must not be null
*/
public JBBPFieldStruct(final JBBPNamedFieldInfo name, final List<JBBPAbstractField> fields) {
this(name, fields.toArray(new JBBPAbstractField[fields.size()]));
this(name, fields.toArray(ARRAY_FIELD_EMPTY));
}

/**
Expand Down
Expand Up @@ -27,6 +27,8 @@
import java.util.HashMap;
import java.util.Map;

import static com.igormaznitsa.jbbp.utils.JBBPUtils.ARRAY_STRING_EMPTY;

/**
* The Aggregator allows to join several custom field type processors.
*
Expand All @@ -52,7 +54,7 @@ public JBBPCustomFieldTypeProcessorAggregator(final JBBPCustomFieldTypeProcessor
this.customTypeMap.put(s, p);
}
}
this.types = this.customTypeMap.keySet().toArray(new String[this.customTypeMap.size()]);
this.types = this.customTypeMap.keySet().toArray(ARRAY_STRING_EMPTY);
}

@Override
Expand Down
Expand Up @@ -1494,7 +1494,7 @@ public String End(final boolean format) {
} else if (item instanceof ItemAlign) {
doTabs(format, buffer, structCounter).append("align").append(item.sizeExpression == null ? "" : ':' + item.makeExpressionForExtraField(item.sizeExpression)).append(';');
} else if (item instanceof ItemVal) {
doTabs(format, buffer, structCounter).append("val").append(':' + item.makeExpressionForExtraField(item.sizeExpression)).append(' ').append(item.name).append(';');
doTabs(format, buffer, structCounter).append("val").append(':').append(item.makeExpressionForExtraField(item.sizeExpression)).append(' ').append(item.name).append(';');
} else if (item instanceof ItemResetCounter) {
doTabs(format, buffer, structCounter).append("reset$$;");
} else if (item instanceof ItemSkip) {
Expand Down
3 changes: 3 additions & 0 deletions jbbp/src/main/java/com/igormaznitsa/jbbp/utils/JBBPUtils.java
Expand Up @@ -38,6 +38,9 @@
*/
public final class JBBPUtils {

public static final String [] ARRAY_STRING_EMPTY = new String[0];
public static final JBBPAbstractField[] ARRAY_FIELD_EMPTY = new JBBPAbstractField[0];

private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");

private JBBPUtils() {
Expand Down
Expand Up @@ -38,7 +38,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand Down
Expand Up @@ -29,7 +29,6 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;

import static com.igormaznitsa.jbbp.io.JBBPOut.BeginBin;
import static org.junit.jupiter.api.Assertions.*;
Expand Down
Expand Up @@ -20,7 +20,6 @@
import com.igormaznitsa.jbbp.io.JBBPBitNumber;
import com.igormaznitsa.jbbp.io.JBBPBitOrder;
import com.igormaznitsa.jbbp.io.JBBPBitOutputStream;
import com.igormaznitsa.jbbp.io.JBBPByteOrder;
import com.igormaznitsa.jbbp.io.JBBPCustomFieldWriter;
import com.igormaznitsa.jbbp.io.JBBPOut;
import com.igormaznitsa.jbbp.io.JBBPOutVarProcessor;
Expand Down
Expand Up @@ -3,7 +3,8 @@
import com.igormaznitsa.jbbp.compiler.JBBPNamedFieldInfo;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class JBBPFieldStringTest {

Expand Down
Expand Up @@ -20,7 +20,6 @@
import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
import com.igormaznitsa.jbbp.mapper.Bin;
import com.igormaznitsa.jbbp.mapper.BinType;
import com.igormaznitsa.jbbp.utils.TargetSources;
import org.openjdk.jmh.annotations.Benchmark;

import java.io.ByteArrayInputStream;
Expand Down

0 comments on commit e050d0a

Please sign in to comment.