Skip to content

Commit

Permalink
[Java] Tidy up after merge of PR #915.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpt777 committed Oct 5, 2022
1 parent cd88380 commit e46a5e7
Showing 1 changed file with 28 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ enum CodecType
private static final String PACKAGE_INFO = "package-info";
private static final String BASE_INDENT = "";
private static final String INDENT = " ";
private static final Set<String> NO_PACKAGES = Collections.emptySet();
private static final Set<String> PACKAGES_EMPTY_SET = Collections.emptySet();

private final Ir ir;
private final DynamicPackageOutputManager outputManager;
Expand Down Expand Up @@ -178,25 +178,26 @@ public void generateTypeStubs() throws IOException
}

/**
* Register the types explicit package - if it's set and should be supported.
* Register the types explicit package - if set and should be supported.
*
* @param token the 0-th token of the type.
* @param ir the intermediate representation.
* @return the overridden package name of the type if set and supported, or {@link Ir#applicableNamespace()}.
*/
private String registerTypesPackageName(final Token token, final Ir ir)
{

if (!shouldSupportTypesPackageNames)
{
return ir.applicableNamespace();
}

if (token.packageName() != null)
{
packageNameByTypes.add(token.packageName());
outputManager.setPackageName(token.packageName());
return token.packageName();
}

return ir.applicableNamespace();
}

Expand Down Expand Up @@ -1249,8 +1250,8 @@ private void generateBitSet(final List<Token> tokens) throws IOException
try (Writer out = outputManager.createOutput(decoderName))
{
final Encoding encoding = token.encoding();
generateFixedFlyweightHeader(out, token, decoderName, implementsString, readOnlyBuffer, fqReadOnlyBuffer,
NO_PACKAGES);
generateFixedFlyweightHeader(
out, token, decoderName, implementsString, readOnlyBuffer, fqReadOnlyBuffer, PACKAGES_EMPTY_SET);
out.append(generateChoiceIsEmpty(encoding.primitiveType()));

new Formatter(out).format(
Expand All @@ -1270,8 +1271,8 @@ private void generateBitSet(final List<Token> tokens) throws IOException
registerTypesPackageName(token, ir);
try (Writer out = outputManager.createOutput(encoderName))
{
generateFixedFlyweightHeader(out, token, encoderName, implementsString, mutableBuffer, fqMutableBuffer,
NO_PACKAGES);
generateFixedFlyweightHeader(
out, token, encoderName, implementsString, mutableBuffer, fqMutableBuffer, PACKAGES_EMPTY_SET);
generateChoiceClear(out, encoderName, token);
generateChoiceEncoders(out, encoderName, choiceList);
out.append("}\n");
Expand All @@ -1287,8 +1288,7 @@ private void generateFixedFlyweightHeader(
final String fqBuffer,
final Set<String> importedTypesPackages) throws IOException
{
final String packageName = registerTypesPackageName(token, ir);
out.append(generateFileHeader(packageName, importedTypesPackages, fqBuffer));
out.append(generateFileHeader(registerTypesPackageName(token, ir), importedTypesPackages, fqBuffer));
out.append(generateDeclaration(typeName, implementsString, token));
out.append(generateFixedFlyweightCode(typeName, token.encodedLength(), buffer));
}
Expand All @@ -1302,8 +1302,7 @@ private void generateCompositeFlyweightHeader(
final String implementsString,
final Set<String> importedTypesPackages) throws IOException
{
final String packageName = registerTypesPackageName(token, ir);
out.append(generateFileHeader(packageName, importedTypesPackages, fqBuffer));
out.append(generateFileHeader(registerTypesPackageName(token, ir), importedTypesPackages, fqBuffer));
out.append(generateDeclaration(typeName, implementsString, token));
out.append(generateFixedFlyweightCode(typeName, token.encodedLength(), buffer));
}
Expand All @@ -1314,8 +1313,8 @@ private void generateEnum(final List<Token> tokens) throws IOException
final String enumName = formatClassName(enumToken.applicableTypeName());
final Encoding encoding = enumToken.encoding();
final String nullVal = encoding.applicableNullValue().toString();

final String packageName = registerTypesPackageName(enumToken, ir);

try (Writer out = outputManager.createOutput(enumName))
{
out.append(generateEnumFileHeader(packageName));
Expand All @@ -1340,6 +1339,7 @@ private void generateComposite(final List<Token> tokens) throws IOException

registerTypesPackageName(token, ir);
final Set<String> importedTypesPackages = scanPackagesToImport(tokens);

try (Writer out = outputManager.createOutput(decoderName))
{
final String implementsString = implementsInterface(CompositeDecoderFlyweight.class.getSimpleName());
Expand Down Expand Up @@ -1392,8 +1392,8 @@ private void generateComposite(final List<Token> tokens) throws IOException
try (Writer out = outputManager.createOutput(encoderName))
{
final String implementsString = implementsInterface(CompositeEncoderFlyweight.class.getSimpleName());
generateCompositeFlyweightHeader(token, encoderName, out, mutableBuffer, fqMutableBuffer, implementsString,
importedTypesPackages);
generateCompositeFlyweightHeader(
token, encoderName, out, mutableBuffer, fqMutableBuffer, implementsString, importedTypesPackages);

for (int i = 1, end = tokens.size() - 1; i < end;)
{
Expand Down Expand Up @@ -1439,11 +1439,12 @@ private Set<String> scanPackagesToImport(final List<Token> tokens)
{
if (!shouldSupportTypesPackageNames)
{
return NO_PACKAGES;
return PACKAGES_EMPTY_SET;
}

final Set<String> packagesToImport = new HashSet<>();
for (int i = 1; i < tokens.size() - 1; i++)

for (int i = 1, limit = tokens.size() - 1; i < limit; i++)
{
final Token typeToken = tokens.get(i);
if (typeToken.signal() == Signal.BEGIN_ENUM ||
Expand All @@ -1456,6 +1457,7 @@ private Set<String> scanPackagesToImport(final List<Token> tokens)
}
}
}

return packagesToImport;
}

Expand Down Expand Up @@ -1646,19 +1648,22 @@ private CharSequence generateEnumLookupMethod(final List<Token> tokens, final St

private StringBuilder generateImportStatements(final Set<String> packages, final String currentPackage)
{
final StringBuilder importsStatements = new StringBuilder();
final StringBuilder importStatements = new StringBuilder();

for (final String candidatePackage : packages)
{
if (!candidatePackage.equalsIgnoreCase(currentPackage))
if (!candidatePackage.equals(currentPackage))
{
importsStatements.append("import ").append(candidatePackage).append(".*;\n");
importStatements.append("import ").append(candidatePackage).append(".*;\n");
}
}
if (importsStatements.length() > 0)

if (importStatements.length() > 0)
{
importsStatements.append("\n\n");
importStatements.append("\n\n");
}
return importsStatements;

return importStatements;
}

private String interfaceImportLine()
Expand Down Expand Up @@ -3878,13 +3883,6 @@ private String decoderName(final String className)

private String implementsInterface(final String interfaceName)
{
if (!shouldGenerateInterfaces)
{
return "";
}
else
{
return " implements " + interfaceName;
}
return shouldGenerateInterfaces ? " implements " + interfaceName : "";
}
}

0 comments on commit e46a5e7

Please sign in to comment.