Skip to content

Commit fd1935a

Browse files
committed
[Java]: Added epoch and time unit support to IR encoding.
1 parent 1f3bb3e commit fd1935a

File tree

9 files changed

+183
-124
lines changed

9 files changed

+183
-124
lines changed

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

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import uk.co.real_logic.sbe.util.Verify;
2121

2222
import java.nio.ByteOrder;
23+
import java.util.concurrent.TimeUnit;
2324

2425
/**
2526
* Optional settings that can be associated with {@link Token}s.
@@ -49,6 +50,8 @@ public static enum Presence
4950
private final PrimitiveValue nullVal;
5051
private final PrimitiveValue constVal;
5152
private final String characterEncoding;
53+
private final String epoch;
54+
private final TimeUnit timeUnit;
5255

5356
public Encoding()
5457
{
@@ -60,6 +63,8 @@ public Encoding()
6063
nullVal = null;
6164
constVal = null;
6265
characterEncoding = "";
66+
epoch = null;
67+
timeUnit = null;
6368
}
6469

6570
public Encoding(final PrimitiveType primitiveType,
@@ -69,7 +74,9 @@ public Encoding(final PrimitiveType primitiveType,
6974
final PrimitiveValue maxVal,
7075
final PrimitiveValue nullVal,
7176
final PrimitiveValue constVal,
72-
final String characterEncoding)
77+
final String characterEncoding,
78+
final String epoch,
79+
final TimeUnit timeUnit)
7380
{
7481
Verify.notNull(presence, "presence");
7582
Verify.notNull(byteOrder, "byteOrder");
@@ -82,6 +89,8 @@ public Encoding(final PrimitiveType primitiveType,
8289
this.nullVal = nullVal;
8390
this.constVal = constVal;
8491
this.characterEncoding = characterEncoding;
92+
this.epoch = epoch;
93+
this.timeUnit = timeUnit;
8594
}
8695

8796
/**
@@ -210,17 +219,39 @@ public String characterEncoding()
210219
return characterEncoding;
211220
}
212221

222+
/**
223+
* The epoch from which a timestamp is offset. The default is "unix".
224+
*
225+
* @return the epoch from which a timestamp is offset.
226+
*/
227+
public String epoch()
228+
{
229+
return epoch;
230+
}
231+
232+
/**
233+
* The {@link TimeUnit} of the timestamp.
234+
*
235+
* @return the {@link TimeUnit} of the timestamp.
236+
*/
237+
public TimeUnit timeUnit()
238+
{
239+
return timeUnit;
240+
}
241+
213242
public String toString()
214243
{
215244
return "Encoding{" +
216245
"primitiveType=" + primitiveType +
246+
", presence=" + presence +
217247
", byteOrder=" + byteOrder +
218-
", presence=" + presence() +
219248
", minVal=" + minVal +
220249
", maxVal=" + maxVal +
221250
", nullVal=" + nullVal +
222251
", constVal=" + constVal +
223-
", characterEncoding=" + characterEncoding +
252+
", characterEncoding='" + characterEncoding + '\'' +
253+
", epoch='" + epoch + '\'' +
254+
", timeUnit=" + timeUnit +
224255
'}';
225256
}
226257

@@ -237,6 +268,8 @@ public static class Builder
237268
private PrimitiveValue nullVal = null;
238269
private PrimitiveValue constVal = null;
239270
private String characterEncoding = "";
271+
private String epoch = null;
272+
private TimeUnit timeUnit = null;
240273

241274
public Builder primitiveType(final PrimitiveType primitiveType)
242275
{
@@ -286,9 +319,21 @@ public Builder characterEncoding(final String characterEncoding)
286319
return this;
287320
}
288321

322+
public Builder epoch(final String epoch)
323+
{
324+
this.epoch = epoch;
325+
return this;
326+
}
327+
328+
public Builder timeUnit(final TimeUnit timeUnit)
329+
{
330+
this.timeUnit = timeUnit;
331+
return this;
332+
}
333+
289334
public Encoding build()
290335
{
291-
return new Encoding(primitiveType, presence, byteOrder, minVal, maxVal, nullVal, constVal, characterEncoding);
336+
return new Encoding(primitiveType, presence, byteOrder, minVal, maxVal, nullVal, constVal, characterEncoding, epoch, timeUnit);
292337
}
293338
}
294339
}

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

Lines changed: 8 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -32,104 +32,6 @@
3232

3333
/**
3434
* SBE compositeType.
35-
* <p>
36-
* Decimal types can use mantissa and exponent portions as min/max/null.
37-
* <table>
38-
* <thead>
39-
* <tr>
40-
* <th></th>
41-
* <th></th>
42-
* <th>Length</th>
43-
* <th>Exponent</th>
44-
* <th>Min</th>
45-
* <th>Max</th>
46-
* <th>Null</th>
47-
* </tr>
48-
* </thead>
49-
* <tbody>
50-
* <tr>
51-
* <td>decimal</td>
52-
* <td></td>
53-
* <td>9</td>
54-
* <td>-128 to 127</td>
55-
* <td></td>
56-
* <td></td>
57-
* <td></td>
58-
* </tr>
59-
* <tr>
60-
* <td></td>
61-
* <td>int64 mantissa</td>
62-
* <td></td>
63-
* <td></td>
64-
* <td>-2^63 + 1</td>
65-
* <td>2^64(2^63 - 1)</td>
66-
* <td>-2^63</td>
67-
* </tr>
68-
* <tr>
69-
* <td></td>
70-
* <td>int8 exponent</td>
71-
* <td></td>
72-
* <td></td>
73-
* <td>10^127</td>
74-
* <td>10^-128</td>
75-
* <td>10^127</td>
76-
* </tr>
77-
* <tr>
78-
* <td>decimal64</td>
79-
* <td></td>
80-
* <td>8</td>
81-
* <td>-128 to 127</td>
82-
* <td></td>
83-
* <td></td>
84-
* <td></td>
85-
* </tr>
86-
* <tr>
87-
* <td></td>
88-
* <td>int64 mantissa</td>
89-
* <td></td>
90-
* <td></td>
91-
* <td>-2^63 + 1</td>
92-
* <td>2^64(2^63 - 1)</td>
93-
* <td>-2^63</td>
94-
* </tr>
95-
* <tr>
96-
* <td></td>
97-
* <td>constant exponent</td>
98-
* <td></td>
99-
* <td></td>
100-
* <td>10^127</td>
101-
* <td>10^-128</td>
102-
* <td>10^127</td>
103-
* </tr>
104-
* <tr>
105-
* <td>decimal32</td>
106-
* <td></td>
107-
* <td>4</td>
108-
* <td>-128 to 127</td>
109-
* <td></td>
110-
* <td></td>
111-
* <td></td>
112-
* </tr>
113-
* <tr>
114-
* <td></td>
115-
* <td>int32 mantissa</td>
116-
* <td></td>
117-
* <td></td>
118-
* <td>-2^31 + 1</td>
119-
* <td>2^32(2^31 - 1)</td>
120-
* <td>-2^63</td>
121-
* </tr>
122-
* <tr>
123-
* <td></td>
124-
* <td>constant exponent</td>
125-
* <td></td>
126-
* <td></td>
127-
* <td>10^127</td>
128-
* <td>10^-128</td>
129-
* <td>10^127</td>
130-
* </tr>
131-
* </tbody>
132-
* </table>
13335
*/
13436
public class CompositeType extends Type
13537
{
@@ -148,12 +50,12 @@ public CompositeType(final Node node)
14850
super(node);
14951

15052
sinceVersion = Integer.parseInt(XmlSchemaParser.getAttributeValue(node, "sinceVersion", "0"));
151-
XPath xPath = XPathFactory.newInstance().newXPath();
152-
NodeList list = (NodeList)xPath.compile("type").evaluate(node, XPathConstants.NODESET);
53+
final XPath xPath = XPathFactory.newInstance().newXPath();
54+
final NodeList list = (NodeList)xPath.compile("type").evaluate(node, XPathConstants.NODESET);
15355

15456
for (int i = 0, size = list.getLength(); i < size; i++)
15557
{
156-
EncodedDataType type = new EncodedDataType(list.item(i));
58+
final EncodedDataType type = new EncodedDataType(list.item(i));
15759

15860
if (compositeMap.get(type.name()) != null)
15961
{
@@ -226,8 +128,7 @@ public List<EncodedDataType> getTypeList()
226128
*/
227129
public void makeDataFieldCompositeType()
228130
{
229-
EncodedDataType edt = compositeMap.get("varData");
230-
131+
final EncodedDataType edt = compositeMap.get("varData");
231132
if (edt != null)
232133
{
233134
edt.variableLength(true);
@@ -246,6 +147,7 @@ public void checkForWellFormedGroupSizeEncoding(final Node node)
246147
{
247148
XmlSchemaParser.handleError(node, "composite for group size encoding must have \"blockLength\"");
248149
}
150+
249151
if (compositeMap.get("numInGroup") == null)
250152
{
251153
XmlSchemaParser.handleError(node, "composite for group size encoding must have \"numInGroup\"");
@@ -264,6 +166,7 @@ public void checkForWellFormedVariableLengthDataEncoding(final Node node)
264166
{
265167
XmlSchemaParser.handleError(node, "composite for variable length data encoding must have \"length\"");
266168
}
169+
267170
if (compositeMap.get("varData") == null)
268171
{
269172
XmlSchemaParser.handleError(node, "composite for variable length data encoding must have \"varData\"");
@@ -282,10 +185,12 @@ public void checkForWellFormedMessageHeader(final Node node)
282185
{
283186
XmlSchemaParser.handleError(node, "composite for message header must have \"blockLength\"");
284187
}
188+
285189
if (compositeMap.get("templateId") == null)
286190
{
287191
XmlSchemaParser.handleError(node, "composite for message header must have \"templateId\"");
288192
}
193+
289194
if (compositeMap.get("version") == null)
290195
{
291196
XmlSchemaParser.handleError(node, "composite for message header must have \"version\"");

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ public EncodedDataType(final Node node)
107107
}
108108
else
109109
{
110-
// TODO: should we check for presence=optional and flag it? No, should default to primitiveType nullVal
111-
nullVal = null; // this value is invalid unless nullVal specified for type
110+
nullVal = null;
112111
}
113112
}
114113

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ else if (Integer.parseInt(getAttributeValue(encodingTypeNode, "length", "1")) !=
139139
}
140140
}
141141

142+
/**
143+
* The {@link PrimitiveType} used to encode the enum.
144+
*
145+
* @return the {@link PrimitiveType} used to encode the enum.
146+
*/
142147
public PrimitiveType encodingType()
143148
{
144149
return encodingType;
@@ -157,7 +162,7 @@ public int size()
157162
/**
158163
* Get the {@link ValidValue} represented by a {@link PrimitiveValue}.
159164
*
160-
* @param value to get.
165+
* @param value to lookup
161166
* @return the {@link ValidValue} represented by a {@link PrimitiveValue} or null.
162167
*/
163168
public ValidValue getValidValue(final PrimitiveValue value)
@@ -168,7 +173,7 @@ public ValidValue getValidValue(final PrimitiveValue value)
168173
/**
169174
* Get the {@link ValidValue} represented by a string name.
170175
*
171-
* @param name to get.
176+
* @param name to lookup
172177
* @return the {@link ValidValue} represented by a string name or null.
173178
*/
174179
public ValidValue getValidValue(final String name)

0 commit comments

Comments
 (0)