This repository has been archived by the owner on May 25, 2021. It is now read-only.
/
TimeUUIDType.cs
110 lines (82 loc) · 2.46 KB
/
TimeUUIDType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
namespace FluentCassandra.Types
{
public class TimeUUIDType : CassandraObject
{
private static readonly TimeUUIDTypeConverter Converter = new TimeUUIDTypeConverter();
#region Implimentation
protected override object GetValueInternal(Type type)
{
return Converter.ConvertTo(_value, type);
}
public override void SetValue(object obj)
{
_value = Converter.ConvertFrom(obj);
}
public override byte[] ToBigEndian()
{
return Converter.ToBigEndian(_value);
}
public override void SetValueFromBigEndian(byte[] value)
{
_value = Converter.FromBigEndian(value);
}
protected override TypeCode TypeCode
{
get { return TypeCode.Object; }
}
public override string ToString()
{
return _value.ToString("D");
}
#endregion
protected override object GetRawValue() { return _value; }
private Guid _value;
#region Equality
public override bool Equals(object obj)
{
if (obj is TimeUUIDType)
return _value == ((TimeUUIDType)obj)._value;
return _value == Converter.ConvertFrom(obj);
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
#endregion
#region Conversion
public static implicit operator Guid(TimeUUIDType type)
{
return type._value;
}
public static implicit operator Guid?(TimeUUIDType type)
{
return type._value;
}
public static implicit operator TimeUUIDType(Guid s)
{
return new TimeUUIDType { _value = s };
}
public static implicit operator byte[](TimeUUIDType o) { return ConvertTo<byte[]>(o); }
public static implicit operator TimeUUIDType(byte[] o) { return ConvertFrom(o); }
public static implicit operator TimeUUIDType(DateTime o) { return ConvertFrom(o); }
public static implicit operator TimeUUIDType(DateTimeOffset o) { return ConvertFrom(o); }
public static implicit operator DateTime(TimeUUIDType o) { return ConvertTo<DateTime>(o); }
public static implicit operator DateTimeOffset(TimeUUIDType o) { return ConvertTo<DateTimeOffset>(o); }
public static implicit operator DateTime?(TimeUUIDType o) { return ConvertTo<DateTime>(o); }
public static implicit operator DateTimeOffset?(TimeUUIDType o) { return ConvertTo<DateTimeOffset>(o); }
private static T ConvertTo<T>(TimeUUIDType type)
{
if (type == null)
return default(T);
return type.GetValue<T>();
}
private static TimeUUIDType ConvertFrom(object o)
{
var type = new TimeUUIDType();
type.SetValue(o);
return type;
}
#endregion
}
}