Skip to content

Commit 2f4e6e4

Browse files
committed
[Java]: Added semanticType support to IR encoding.
1 parent 8ad21b1 commit 2f4e6e4

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

main/java/uk/co/real_logic/sbe/ir/Encoding.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import java.util.concurrent.TimeUnit;
2424

2525
/**
26-
* Optional settings that can be associated with {@link Token}s.
26+
* Optional encoding settings that can be associated with {@link Token}s.
2727
*/
2828
public class Encoding
2929
{
@@ -52,6 +52,7 @@ public static enum Presence
5252
private final String characterEncoding;
5353
private final String epoch;
5454
private final TimeUnit timeUnit;
55+
private final String semanticType;
5556

5657
public Encoding()
5758
{
@@ -65,6 +66,7 @@ public Encoding()
6566
characterEncoding = "";
6667
epoch = null;
6768
timeUnit = null;
69+
semanticType = null;
6870
}
6971

7072
public Encoding(final PrimitiveType primitiveType,
@@ -76,7 +78,8 @@ public Encoding(final PrimitiveType primitiveType,
7678
final PrimitiveValue constVal,
7779
final String characterEncoding,
7880
final String epoch,
79-
final TimeUnit timeUnit)
81+
final TimeUnit timeUnit,
82+
final String semanticType)
8083
{
8184
Verify.notNull(presence, "presence");
8285
Verify.notNull(byteOrder, "byteOrder");
@@ -91,6 +94,7 @@ public Encoding(final PrimitiveType primitiveType,
9194
this.characterEncoding = characterEncoding;
9295
this.epoch = epoch;
9396
this.timeUnit = timeUnit;
97+
this.semanticType = semanticType;
9498
}
9599

96100
/**
@@ -239,6 +243,16 @@ public TimeUnit timeUnit()
239243
return timeUnit;
240244
}
241245

246+
/**
247+
* The semantic type of an encoding which can have relevance to the application layer.
248+
*
249+
* @return semantic type of an encoding which can have relevance to the application layer.
250+
*/
251+
public String semanticType()
252+
{
253+
return semanticType;
254+
}
255+
242256
public String toString()
243257
{
244258
return "Encoding{" +
@@ -252,6 +266,7 @@ public String toString()
252266
", characterEncoding='" + characterEncoding + '\'' +
253267
", epoch='" + epoch + '\'' +
254268
", timeUnit=" + timeUnit +
269+
", semanticType='" + semanticType + '\'' +
255270
'}';
256271
}
257272

@@ -270,6 +285,7 @@ public static class Builder
270285
private String characterEncoding = "";
271286
private String epoch = null;
272287
private TimeUnit timeUnit = null;
288+
private String semanticType = null;
273289

274290
public Builder primitiveType(final PrimitiveType primitiveType)
275291
{
@@ -331,9 +347,25 @@ public Builder timeUnit(final TimeUnit timeUnit)
331347
return this;
332348
}
333349

350+
public Builder semanticType(final String semanticType)
351+
{
352+
this.semanticType = semanticType;
353+
return this;
354+
}
355+
334356
public Encoding build()
335357
{
336-
return new Encoding(primitiveType, presence, byteOrder, minVal, maxVal, nullVal, constVal, characterEncoding, epoch, timeUnit);
358+
return new Encoding(primitiveType,
359+
presence,
360+
byteOrder,
361+
minVal,
362+
maxVal,
363+
nullVal,
364+
constVal,
365+
characterEncoding,
366+
epoch,
367+
timeUnit,
368+
semanticType);
337369
}
338370
}
339371
}

main/java/uk/co/real_logic/sbe/xml/IrGenerator.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ private void addFieldSignal(final Field field, final Signal signal)
9999
.name(field.name())
100100
.schemaId(field.id())
101101
.version(field.sinceVersion())
102+
.encoding(new Encoding.Builder()
103+
.semanticType(semanticTypeOf(null, field)).build())
102104
.build();
103105

104106
tokenList.add(token);
@@ -159,7 +161,9 @@ private void add(final CompositeType type, final int currOffset, final Field fie
159161
.signal(Signal.BEGIN_COMPOSITE)
160162
.name(type.name())
161163
.offset(currOffset)
162-
.size(type.size());
164+
.size(type.size())
165+
.encoding(new Encoding.Builder()
166+
.semanticType(semanticTypeOf(type, field)).build());
163167

164168
if (field != null)
165169
{
@@ -183,6 +187,7 @@ private void add(final EnumType type, final int offset, final Field field)
183187
final PrimitiveType encodingType = type.encodingType();
184188
final Encoding.Builder encodingBuilder = new Encoding.Builder()
185189
.primitiveType(encodingType)
190+
.semanticType(semanticTypeOf(type, field))
186191
.byteOrder(byteOrder);
187192

188193
if (type.presence() == Presence.OPTIONAL)
@@ -243,6 +248,7 @@ private void add(final SetType type, final int offset, final Field field)
243248
.size(encodingType.size())
244249
.offset(offset)
245250
.encoding(new Encoding.Builder()
251+
.semanticType(semanticTypeOf(type, field))
246252
.primitiveType(encodingType)
247253
.build());
248254

@@ -289,6 +295,8 @@ private void add(final EncodedDataType type, final int offset, final Field field
289295
.byteOrder(byteOrder)
290296
.characterEncoding(type.characterEncoding());
291297

298+
encodingBuilder.semanticType(semanticTypeOf(type, field));
299+
292300
if (null != field)
293301
{
294302
encodingBuilder.epoch(field.epoch());
@@ -332,4 +340,15 @@ private void add(final EncodedDataType type, final int offset, final Field field
332340

333341
tokenList.add(token);
334342
}
335-
}
343+
344+
private String semanticTypeOf(final Type type, final Field field)
345+
{
346+
final String typeSemanticType = null != type ? type.semanticType() : null;
347+
if (typeSemanticType != null)
348+
{
349+
return typeSemanticType;
350+
}
351+
352+
return null != field ? field.semanticType() : null;
353+
}
354+
}

0 commit comments

Comments
 (0)