Skip to content

Commit a4fe52c

Browse files
committed
Implement #522: add AvroReadFeature, AvroWriteFeature (refactoring)
1 parent 9d75e3d commit a4fe52c

15 files changed

+194
-181
lines changed

avro/src/main/java/tools/jackson/dataformat/avro/AvroFactory.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public class AvroFactory
2828
* Bitfield (set of flags) of all parser features that are enabled
2929
* by default.
3030
*/
31-
final static int DEFAULT_AVRO_PARSER_FEATURE_FLAGS = AvroParser.Feature.collectDefaults();
31+
final static int DEFAULT_AVRO_PARSER_FEATURE_FLAGS = AvroReadFeature.collectDefaults();
3232

3333
/**
3434
* Bitfield (set of flags) of all generator features that are enabled
3535
* by default.
3636
*/
37-
final static int DEFAULT_AVRO_GENERATOR_FEATURE_FLAGS = AvroGenerator.Feature.collectDefaults();
37+
final static int DEFAULT_AVRO_GENERATOR_FEATURE_FLAGS = AvroWriteFeature.collectDefaults();
3838

3939
/*
4040
/**********************************************************
@@ -188,14 +188,14 @@ public boolean canParseAsync() {
188188
/**
189189
* Checked whether specified parser feature is enabled.
190190
*/
191-
public final boolean isEnabled(AvroParser.Feature f) {
191+
public final boolean isEnabled(AvroReadFeature f) {
192192
return (_formatReadFeatures & f.getMask()) != 0;
193193
}
194194

195195
/**
196196
* Check whether specified generator feature is enabled.
197197
*/
198-
public final boolean isEnabled(AvroGenerator.Feature f) {
198+
public final boolean isEnabled(AvroWriteFeature f) {
199199
return (_formatWriteFeatures & f.getMask()) != 0;
200200
}
201201

@@ -216,13 +216,13 @@ public boolean canUseSchema(FormatSchema schema) {
216216
}
217217

218218
@Override
219-
public Class<AvroParser.Feature> getFormatReadFeatureType() {
220-
return AvroParser.Feature.class;
219+
public Class<AvroReadFeature> getFormatReadFeatureType() {
220+
return AvroReadFeature.class;
221221
}
222222

223223
@Override
224-
public Class<AvroGenerator.Feature> getFormatWriteFeatureType() {
225-
return AvroGenerator.Feature.class;
224+
public Class<AvroWriteFeature> getFormatWriteFeatureType() {
225+
return AvroWriteFeature.class;
226226
}
227227

228228
/*

avro/src/main/java/tools/jackson/dataformat/avro/AvroFactoryBuilder.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -84,65 +84,65 @@ public AvroFactory build() {
8484

8585
// // // Parser features
8686

87-
public AvroFactoryBuilder enable(AvroParser.Feature f) {
87+
public AvroFactoryBuilder enable(AvroReadFeature f) {
8888
_formatReadFeatures |= f.getMask();
8989
return _this();
9090
}
9191

92-
public AvroFactoryBuilder enable(AvroParser.Feature first, AvroParser.Feature... other) {
92+
public AvroFactoryBuilder enable(AvroReadFeature first, AvroReadFeature... other) {
9393
_formatReadFeatures |= first.getMask();
94-
for (AvroParser.Feature f : other) {
94+
for (AvroReadFeature f : other) {
9595
_formatReadFeatures |= f.getMask();
9696
}
9797
return _this();
9898
}
9999

100-
public AvroFactoryBuilder disable(AvroParser.Feature f) {
100+
public AvroFactoryBuilder disable(AvroReadFeature f) {
101101
_formatReadFeatures &= ~f.getMask();
102102
return _this();
103103
}
104104

105-
public AvroFactoryBuilder disable(AvroParser.Feature first, AvroParser.Feature... other) {
105+
public AvroFactoryBuilder disable(AvroReadFeature first, AvroReadFeature... other) {
106106
_formatReadFeatures &= ~first.getMask();
107-
for (AvroParser.Feature f : other) {
107+
for (AvroReadFeature f : other) {
108108
_formatReadFeatures &= ~f.getMask();
109109
}
110110
return _this();
111111
}
112112

113-
public AvroFactoryBuilder configure(AvroParser.Feature f, boolean state) {
113+
public AvroFactoryBuilder configure(AvroReadFeature f, boolean state) {
114114
return state ? enable(f) : disable(f);
115115
}
116116

117117
// // // Generator features
118118

119-
public AvroFactoryBuilder enable(AvroGenerator.Feature f) {
119+
public AvroFactoryBuilder enable(AvroWriteFeature f) {
120120
_formatWriteFeatures |= f.getMask();
121121
return _this();
122122
}
123123

124-
public AvroFactoryBuilder enable(AvroGenerator.Feature first, AvroGenerator.Feature... other) {
124+
public AvroFactoryBuilder enable(AvroWriteFeature first, AvroWriteFeature... other) {
125125
_formatWriteFeatures |= first.getMask();
126-
for (AvroGenerator.Feature f : other) {
126+
for (AvroWriteFeature f : other) {
127127
_formatWriteFeatures |= f.getMask();
128128
}
129129
return _this();
130130
}
131131

132-
public AvroFactoryBuilder disable(AvroGenerator.Feature f) {
132+
public AvroFactoryBuilder disable(AvroWriteFeature f) {
133133
_formatWriteFeatures &= ~f.getMask();
134134
return _this();
135135
}
136136

137-
public AvroFactoryBuilder disable(AvroGenerator.Feature first, AvroGenerator.Feature... other) {
137+
public AvroFactoryBuilder disable(AvroWriteFeature first, AvroWriteFeature... other) {
138138
_formatWriteFeatures &= ~first.getMask();
139-
for (AvroGenerator.Feature f : other) {
139+
for (AvroWriteFeature f : other) {
140140
_formatWriteFeatures &= ~f.getMask();
141141
}
142142
return _this();
143143
}
144144

145-
public AvroFactoryBuilder configure(AvroGenerator.Feature f, boolean state) {
145+
public AvroFactoryBuilder configure(AvroWriteFeature f, boolean state) {
146146
return state ? enable(f) : disable(f);
147147
}
148148
}

avro/src/main/java/tools/jackson/dataformat/avro/AvroGenerator.java

+6-79
Original file line numberDiff line numberDiff line change
@@ -20,79 +20,6 @@
2020

2121
public class AvroGenerator extends GeneratorBase
2222
{
23-
/**
24-
* Enumeration that defines all togglable features for Avro generators
25-
*/
26-
public enum Feature
27-
implements FormatFeature
28-
{
29-
/**
30-
* Feature that can be disabled to prevent Avro from buffering any more
31-
* data then absolutely necessary.
32-
* This affects buffering by underlying codec.
33-
* Note that disabling buffer is likely to reduce performance if the underlying
34-
* input/output is unbuffered.
35-
*<p>
36-
* Enabled by default to preserve the existing behavior.
37-
*/
38-
AVRO_BUFFERING(true),
39-
40-
/**
41-
* Feature that tells Avro to write data in file format (i.e. including the schema with the data)
42-
* rather than the RPC format which is otherwise default
43-
*<p>
44-
* NOTE: reader-side will have to be aware of distinction as well, since possible inclusion
45-
* of this header is not 100% reliably auto-detectable (while header has distinct marker,
46-
* "raw" Avro content has no limitations and could theoretically have same pre-amble from data).
47-
*/
48-
AVRO_FILE_OUTPUT(false),
49-
50-
/**
51-
* Feature that enables addition of {@code null} as default value in generated schema
52-
* when no real default value is defined and {@code null} is legal value for type
53-
* (union type with {@code null} included).
54-
*<p>
55-
* Disabled by default.
56-
*
57-
* @since 3.0
58-
*
59-
*/
60-
ADD_NULL_AS_DEFAULT_VALUE_IN_SCHEMA(false)
61-
;
62-
63-
protected final boolean _defaultState;
64-
protected final int _mask;
65-
66-
/**
67-
* Method that calculates bit set (flags) of all features that
68-
* are enabled by default.
69-
*/
70-
public static int collectDefaults()
71-
{
72-
int flags = 0;
73-
for (Feature f : values()) {
74-
if (f.enabledByDefault()) {
75-
flags |= f.getMask();
76-
}
77-
}
78-
return flags;
79-
}
80-
81-
private Feature(boolean defaultState) {
82-
_defaultState = defaultState;
83-
_mask = (1 << ordinal());
84-
}
85-
86-
@Override
87-
public boolean enabledByDefault() { return _defaultState; }
88-
89-
@Override
90-
public int getMask() { return _mask; }
91-
92-
@Override
93-
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
94-
}
95-
9623
/*
9724
/**********************************************************************
9825
/* Configuration
@@ -111,7 +38,7 @@ private Feature(boolean defaultState) {
11138

11239
/**
11340
* Bit flag composed of bits that indicate which
114-
* {@link AvroGenerator.Feature}s
41+
* {@link AvroWriteFeature}s
11542
* are enabled.
11643
*/
11744
protected int _formatWriteFeatures;
@@ -165,7 +92,7 @@ public AvroGenerator(ObjectWriteContext writeCtxt, IOContext ioCtxt,
16592
_output = output;
16693
_streamWriteContext = AvroWriteContext.nullContext();
16794
_apacheCodecRecycler = apacheCodecRecycler;
168-
final boolean buffering = isEnabled(Feature.AVRO_BUFFERING);
95+
final boolean buffering = isEnabled(AvroWriteFeature.AVRO_BUFFERING);
16996
BinaryEncoder encoderToReuse = apacheCodecRecycler.acquireEncoder();
17097
_encoder = buffering
17198
? ENCODER_FACTORY.binaryEncoder(output, encoderToReuse)
@@ -247,21 +174,21 @@ public JacksonFeatureSet<StreamWriteCapability> streamWriteCapabilities() {
247174
/**********************************************************************
248175
*/
249176

250-
public AvroGenerator enable(Feature f) {
177+
public AvroGenerator enable(AvroWriteFeature f) {
251178
_formatWriteFeatures |= f.getMask();
252179
return this;
253180
}
254181

255-
public AvroGenerator disable(Feature f) {
182+
public AvroGenerator disable(AvroWriteFeature f) {
256183
_formatWriteFeatures &= ~f.getMask();
257184
return this;
258185
}
259186

260-
public final boolean isEnabled(Feature f) {
187+
public final boolean isEnabled(AvroWriteFeature f) {
261188
return (_formatWriteFeatures & f.getMask()) != 0;
262189
}
263190

264-
public AvroGenerator configure(Feature f, boolean state) {
191+
public AvroGenerator configure(AvroWriteFeature f, boolean state) {
265192
if (state) {
266193
enable(f);
267194
} else {

avro/src/main/java/tools/jackson/dataformat/avro/AvroMapper.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ protected MapperBuilderState _saveState() {
6767
/******************************************************************
6868
*/
6969

70-
public Builder enable(AvroParser.Feature... features) {
71-
for (AvroParser.Feature f : features) {
70+
public Builder enable(AvroReadFeature... features) {
71+
for (AvroReadFeature f : features) {
7272
_formatReadFeatures |= f.getMask();
7373
}
7474
return this;
7575
}
7676

77-
public Builder disable(AvroParser.Feature... features) {
78-
for (AvroParser.Feature f : features) {
77+
public Builder disable(AvroReadFeature... features) {
78+
for (AvroReadFeature f : features) {
7979
_formatReadFeatures &= ~f.getMask();
8080
}
8181
return this;
8282
}
8383

84-
public Builder configure(AvroParser.Feature feature, boolean state)
84+
public Builder configure(AvroReadFeature feature, boolean state)
8585
{
8686
if (state) {
8787
_formatReadFeatures |= feature.getMask();
@@ -91,21 +91,21 @@ public Builder configure(AvroParser.Feature feature, boolean state)
9191
return this;
9292
}
9393

94-
public Builder enable(AvroGenerator.Feature... features) {
95-
for (AvroGenerator.Feature f : features) {
94+
public Builder enable(AvroWriteFeature... features) {
95+
for (AvroWriteFeature f : features) {
9696
_formatWriteFeatures |= f.getMask();
9797
}
9898
return this;
9999
}
100100

101-
public Builder disable(AvroGenerator.Feature... features) {
102-
for (AvroGenerator.Feature f : features) {
101+
public Builder disable(AvroWriteFeature... features) {
102+
for (AvroWriteFeature f : features) {
103103
_formatWriteFeatures &= ~f.getMask();
104104
}
105105
return this;
106106
}
107107

108-
public Builder configure(AvroGenerator.Feature feature, boolean state)
108+
public Builder configure(AvroWriteFeature feature, boolean state)
109109
{
110110
if (state) {
111111
_formatWriteFeatures |= feature.getMask();

0 commit comments

Comments
 (0)