1 /*
2 * hunt-proton: AMQP Protocol library for D programming language.
3 *
4 * Copyright (C) 2018-2019 HuntLabs
5 *
6 * Website: https://www.huntlabs.net/
7 *
8 * Licensed under the Apache-2.0 License.
9 *
10 */
11
12 module hunt.proton.codec.TimestampType;
13
14 import hunt.proton.codec.EncoderImpl;
15 import hunt.proton.codec.DecoderImpl;
16 import hunt.proton.codec.AbstractPrimitiveType;
17 import hunt.collection.Collection;
18 import hunt.collection.Collections;
19 import hunt.time.LocalDateTime;
20
21 import hunt.proton.codec.TypeEncoding;
22 import hunt.proton.codec.FixedSizePrimitiveTypeEncoding;
23 import hunt.proton.codec.EncodingCodes;
24 import hunt.proton.codec.PrimitiveTypeEncoding;
25 import hunt.logging;
26
27 alias Date = LocalDateTime;
28
29 class TimestampType : AbstractPrimitiveType!(Date)
30 {
31 private TimestampEncoding _timestampEncoding;
32
33 this(EncoderImpl encoder, DecoderImpl decoder)
34 {
35 _timestampEncoding = new TimestampEncoding(encoder, decoder);
36 encoder.register(typeid(Date), this);
37 decoder.register(this);
38 }
39
40 public TypeInfo getTypeClass()
41 {
42 return typeid(Date);
43 }
44
45 public ITypeEncoding getEncoding(Object val)
46 {
47 return _timestampEncoding;
48 }
49
50 public void fastWrite(EncoderImpl encoder, long timestamp)
51 {
52 encoder.writeRaw(EncodingCodes.TIMESTAMP);
53 encoder.writeRaw(timestamp);
54 }
55
56 public TimestampEncoding getCanonicalEncoding()
57 {
58 return _timestampEncoding;
59 }
60
61 public Collection!(TypeEncoding!(LocalDateTime)) getAllEncodings()
62 {
63 return Collections.singleton!(TypeEncoding!(LocalDateTime))(_timestampEncoding);
64 }
65
66 //public Collection!(PrimitiveTypeEncoding!(LocalDateTime)) getAllEncodings()
67 //{
68 // return super.getAllEncodings();
69 //}
70
71 public void write(long l)
72 {
73 _timestampEncoding.write(l);
74 }
75
76 class TimestampEncoding : FixedSizePrimitiveTypeEncoding!(Date)
77 {
78
79 this(EncoderImpl encoder, DecoderImpl decoder)
80 {
81 super(encoder, decoder);
82 }
83
84 override
85 protected int getFixedSize()
86 {
87 return 8;
88 }
89
90 override
91 public byte getEncodingCode()
92 {
93 return EncodingCodes.TIMESTAMP;
94 }
95
96 public TimestampType getType()
97 {
98 return this.outer;
99 }
100
101 public void writeValue(Object val)
102 {
103 getEncoder().writeRaw((cast(Date)val).toEpochMilli());
104 }
105
106 public void write(long l)
107 {
108 writeConstructor();
109 getEncoder().writeRaw(l);
110
111 }
112
113 public bool encodesSuperset(TypeEncoding!(Date) encoding)
114 {
115 return (getType() == encoding.getType());
116 }
117
118 public Date readValue()
119 {
120 return Date.ofEpochMilli(getDecoder().readRawLong());
121 }
122 }
123 }