Skip to content

Commit bdb0a0a

Browse files
committed
[Java] Move flyweight interfaces to Agrona.
1 parent 14899e2 commit bdb0a0a

File tree

13 files changed

+24
-289
lines changed

13 files changed

+24
-289
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def validationXsdPath = project(':sbe-tool').projectDir.toString() + '/src/main/
171171

172172
project(':sbe-tool') {
173173
dependencies {
174-
compile 'org.agrona:Agrona:0.5.1'
174+
compile 'org.agrona:Agrona:0.5.2-SNAPSHOT'
175175

176176
testCompile files('build/classes/generated')
177177
}

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/TargetCodeGenerator.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import uk.co.real_logic.sbe.ir.Ir;
2323

2424
import java.io.IOException;
25-
import org.agrona.generation.OutputManager;
2625

2726
import static uk.co.real_logic.sbe.SbeTool.*;
2827

@@ -35,21 +34,13 @@ public enum TargetCodeGenerator
3534
{
3635
public CodeGenerator newInstance(final Ir ir, final String outputDir) throws IOException
3736
{
38-
final OutputManager interfaceOutputManager;
39-
if (Boolean.getBoolean(JAVA_GENERATE_INTERFACES))
40-
{
41-
interfaceOutputManager = new PackageOutputManager(outputDir, JAVA_INTERFACE_PACKAGE);
42-
}
43-
else
44-
{
45-
interfaceOutputManager = null;
46-
}
4737
return new JavaGenerator(
4838
ir,
4939
System.getProperty(JAVA_ENCODING_BUFFER_TYPE, JAVA_DEFAULT_ENCODING_BUFFER_TYPE),
5040
System.getProperty(JAVA_DECODING_BUFFER_TYPE, JAVA_DEFAULT_DECODING_BUFFER_TYPE),
5141
Boolean.getBoolean(JAVA_GROUP_ORDER_ANNOTATION),
52-
new PackageOutputManager(outputDir, ir.applicableNamespace()), interfaceOutputManager);
42+
Boolean.getBoolean(JAVA_GENERATE_INTERFACES),
43+
new PackageOutputManager(outputDir, ir.applicableNamespace()));
5344
}
5445
},
5546

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaGenerator.java

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@
2424
import uk.co.real_logic.sbe.ir.*;
2525

2626
import java.io.IOException;
27-
import java.io.InputStream;
28-
import java.io.InputStreamReader;
29-
import java.io.Reader;
3027
import java.io.Writer;
31-
import java.nio.charset.StandardCharsets;
3228
import java.util.ArrayList;
3329
import java.util.List;
3430
import java.util.function.BiConsumer;
@@ -45,30 +41,25 @@ public class JavaGenerator implements CodeGenerator
4541
private static final String INDENT = " ";
4642
private static final String GEN_COMPOSITE_DECODER_FLYWEIGHT = "CompositeDecoderFlyweight";
4743
private static final String GEN_COMPOSITE_ENCODER_FLYWEIGHT = "CompositeEncoderFlyweight";
48-
private static final String GEN_DECODER_FLYWEIGHT = "DecoderFlyweight";
49-
private static final String GEN_ENCODER_FLYWEIGHT = "EncoderFlyweight";
50-
private static final String GEN_FLYWEIGHT = "Flyweight";
5144
private static final String GEN_MESSAGE_DECODER_FLYWEIGHT = "MessageDecoderFlyweight";
5245
private static final String GEN_MESSAGE_ENCODER_FLYWEIGHT = "MessageEncoderFlyweight";
53-
private static final String GEN_MESSAGE_FLYWEIGHT = "MessageFlyweight";
5446

5547
private final Ir ir;
5648
private final OutputManager outputManager;
57-
private final OutputManager interfaceOutputManager;
5849
private final String fqMutableBuffer;
5950
private final String mutableBuffer;
6051
private final String fqReadOnlyBuffer;
6152
private final String readOnlyBuffer;
6253
private final boolean shouldGenerateGroupOrderAnnotation;
63-
private final boolean generateInterfaces;
54+
private final boolean shouldGenerateInterfaces;
6455

6556
public JavaGenerator(
6657
final Ir ir,
6758
final String mutableBuffer,
6859
final String readOnlyBuffer,
6960
final boolean shouldGenerateGroupOrderAnnotation,
70-
final OutputManager outputManager,
71-
final OutputManager interfaceOutputManager)
61+
final boolean shouldGenerateInterfaces,
62+
final OutputManager outputManager)
7263
throws IOException
7364
{
7465
Verify.notNull(ir, "ir");
@@ -84,8 +75,7 @@ public JavaGenerator(
8475
this.fqReadOnlyBuffer = readOnlyBuffer;
8576

8677
this.shouldGenerateGroupOrderAnnotation = shouldGenerateGroupOrderAnnotation;
87-
this.generateInterfaces = interfaceOutputManager != null;
88-
this.interfaceOutputManager = interfaceOutputManager;
78+
this.shouldGenerateInterfaces = shouldGenerateInterfaces;
8979
}
9080

9181
private static String validateBufferImplementation(
@@ -121,41 +111,9 @@ private String decoderName(final String className)
121111
return className + "Decoder";
122112
}
123113

124-
private void copyInterface(String simpleInterfaceName) throws IOException
114+
private String implementsInterface(final String interfaceName)
125115
{
126-
final String resource = String.format("/java/interfaces/%s.java", simpleInterfaceName);
127-
final InputStream is = JavaGenerator.class.getResourceAsStream(resource);
128-
Verify.notNull(is, resource);
129-
final Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
130-
try (final Writer out = interfaceOutputManager.createOutput(simpleInterfaceName))
131-
{
132-
int bytes;
133-
final char[] buffer = new char[4096];
134-
while (-1 != (bytes = reader.read(buffer)))
135-
{
136-
out.write(buffer, 0, bytes);
137-
}
138-
}
139-
}
140-
141-
public void generateInterfaces() throws IOException
142-
{
143-
if (generateInterfaces)
144-
{
145-
copyInterface(GEN_COMPOSITE_DECODER_FLYWEIGHT);
146-
copyInterface(GEN_COMPOSITE_ENCODER_FLYWEIGHT);
147-
copyInterface(GEN_DECODER_FLYWEIGHT);
148-
copyInterface(GEN_ENCODER_FLYWEIGHT);
149-
copyInterface(GEN_FLYWEIGHT);
150-
copyInterface(GEN_MESSAGE_DECODER_FLYWEIGHT);
151-
copyInterface(GEN_MESSAGE_ENCODER_FLYWEIGHT);
152-
copyInterface(GEN_MESSAGE_FLYWEIGHT);
153-
}
154-
}
155-
156-
private String implementsInterface(final String tokenName, final String interfaceName)
157-
{
158-
if (!generateInterfaces)
116+
if (!shouldGenerateInterfaces)
159117
{
160118
return "";
161119
}
@@ -208,7 +166,6 @@ public void generateTypeStubs() throws IOException
208166

209167
public void generate() throws IOException
210168
{
211-
generateInterfaces();
212169
generateMessageHeaderStub();
213170
generateTypeStubs();
214171

@@ -240,7 +197,7 @@ private void generateEncoder(
240197
final Token msgToken) throws IOException
241198
{
242199
final String className = formatClassName(encoderName(msgToken.name()));
243-
final String implementsString = implementsInterface(msgToken.name(), GEN_MESSAGE_ENCODER_FLYWEIGHT);
200+
final String implementsString = implementsInterface(GEN_MESSAGE_ENCODER_FLYWEIGHT);
244201

245202
try (final Writer out = outputManager.createOutput(className))
246203
{
@@ -268,7 +225,7 @@ private void generateDecoder(
268225
final Token msgToken) throws IOException
269226
{
270227
final String className = formatClassName(decoderName(msgToken.name()));
271-
final String implementsString = implementsInterface(msgToken.name(), GEN_MESSAGE_DECODER_FLYWEIGHT);
228+
final String implementsString = implementsInterface(GEN_MESSAGE_DECODER_FLYWEIGHT);
272229

273230
try (final Writer out = outputManager.createOutput(className))
274231
{
@@ -1035,7 +992,7 @@ private void generateComposite(final List<Token> tokens) throws IOException
1035992

1036993
try (final Writer out = outputManager.createOutput(decoderName))
1037994
{
1038-
final String implementsString = implementsInterface(token.name(), GEN_COMPOSITE_DECODER_FLYWEIGHT);
995+
final String implementsString = implementsInterface(GEN_COMPOSITE_DECODER_FLYWEIGHT);
1039996
generateFixedFlyweightHeader(token, decoderName, out, readOnlyBuffer, fqReadOnlyBuffer, implementsString);
1040997

1041998
for (int i = 1, end = tokens.size() - 1; i < end; i++)
@@ -1070,7 +1027,7 @@ private void generateComposite(final List<Token> tokens) throws IOException
10701027

10711028
try (final Writer out = outputManager.createOutput(encoderName))
10721029
{
1073-
final String implementsString = implementsInterface(token.name(), GEN_COMPOSITE_ENCODER_FLYWEIGHT);
1030+
final String implementsString = implementsInterface(GEN_COMPOSITE_ENCODER_FLYWEIGHT);
10741031
generateFixedFlyweightHeader(token, encoderName, out, mutableBuffer, fqMutableBuffer, implementsString);
10751032

10761033
for (int i = 1, end = tokens.size() - 1; i < end; i++)
@@ -1253,9 +1210,9 @@ private CharSequence generateEnumLookupMethod(final List<Token> tokens, final St
12531210
return sb;
12541211
}
12551212

1256-
private CharSequence interfaceImportLine(final String packageName)
1213+
private CharSequence interfaceImportLine()
12571214
{
1258-
if (!generateInterfaces)
1215+
if (!shouldGenerateInterfaces)
12591216
{
12601217
return "\n";
12611218
}
@@ -1273,7 +1230,7 @@ private CharSequence generateFileHeader(final String className, final String pac
12731230
"@javax.annotation.Generated(value = {\"%s.%s\"})\n",
12741231
packageName,
12751232
fqBuffer,
1276-
interfaceImportLine(packageName),
1233+
interfaceImportLine(),
12771234
packageName,
12781235
className
12791236
);
@@ -1291,7 +1248,7 @@ private CharSequence generateMainHeader(final String className, final String pac
12911248
"@javax.annotation.Generated(value = {\"%s.%s\"})\n",
12921249
packageName,
12931250
fqMutableBuffer,
1294-
interfaceImportLine(packageName),
1251+
interfaceImportLine(),
12951252
packageName,
12961253
className
12971254
);
@@ -1308,7 +1265,7 @@ private CharSequence generateMainHeader(final String className, final String pac
13081265
packageName,
13091266
fqMutableBuffer,
13101267
fqReadOnlyBuffer,
1311-
interfaceImportLine(packageName),
1268+
interfaceImportLine(),
13121269
packageName,
13131270
className
13141271
);

sbe-tool/src/main/resources/java/interfaces/CompositeDecoderFlyweight.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

sbe-tool/src/main/resources/java/interfaces/CompositeEncoderFlyweight.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

sbe-tool/src/main/resources/java/interfaces/DecoderFlyweight.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

sbe-tool/src/main/resources/java/interfaces/EncoderFlyweight.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

sbe-tool/src/main/resources/java/interfaces/Flyweight.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

sbe-tool/src/main/resources/java/interfaces/MessageDecoderFlyweight.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)