This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
BytesType.cs
195 lines (161 loc) · 7.41 KB
/
BytesType.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Linq;
namespace FluentCassandra.Types
{
public class BytesType : CassandraType
{
private static readonly BytesTypeConverter Converter = new BytesTypeConverter();
#region Implimentation
public override object GetValue(Type type)
{
if (!_hasBeenConverted)
{
_value = Converter.FromBigEndian(_bigEndianValue, type);
_sourceType = type;
_hasBeenConverted = true;
}
return GetValue(_value, type, Converter);
}
public override void SetValue(object obj)
{
_hasBeenConverted = true;
_sourceType = obj.GetType();
_value = SetValue(obj, Converter);
}
public override byte[] ToBigEndian()
{
return Converter.ToBigEndian(_value, _sourceType);
}
public override void SetValueFromBigEndian(byte[] value)
{
_bigEndianValue = value;
_hasBeenConverted = false;
}
protected override TypeCode TypeCode
{
get { return TypeCode.Object; }
}
public override string ToString()
{
return GetValue<string>();
}
#endregion
public int Length { get { return _value != null ? _value.Length : _bigEndianValue.Length; } }
private Type _sourceType;
private bool _hasBeenConverted;
private byte[] _bigEndianValue;
private byte[] _value;
#region Equality
public override bool Equals(object obj)
{
if (obj is BytesType)
{
BytesType b1 = this;
BytesType b2 = (BytesType)obj;
if (b1._bigEndianValue != null && b2._bigEndianValue != null)
return b1._bigEndianValue.SequenceEqual(b2._bigEndianValue);
if (b1._sourceType != null && b2._sourceType == null)
b2.GetValue(b1._sourceType);
if (b2._sourceType != null && b1._sourceType == null)
b1.GetValue(b2._sourceType);
if (b1._value == null && b2._value == null && b1._bigEndianValue != null && b2._bigEndianValue != null)
{
b1.GetValue(typeof(byte[]));
b2.GetValue(typeof(byte[]));
}
return b1._value.SequenceEqual(b2._value);
}
return _value.SequenceEqual(CassandraType.GetValue<byte[]>(obj, Converter));
}
/// <remarks>
/// Uses a modified FMV technique to generate a hashcode for a byte array.
/// See: http://stackoverflow.com/questions/16340/how-do-i-generate-a-hashcode-from-a-byte-array-in-c/468084#468084
/// </remarks>
public override int GetHashCode()
{
unchecked
{
const int p = 16777619;
int hash = (int)2166136261;
for (int i = 0; i < _value.Length; i++)
hash = (hash ^ _value[i]) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return hash;
}
}
#endregion
#region Conversion
public static implicit operator BytesType(byte[] o) { return ConvertFrom(o); }
public static implicit operator byte[](BytesType o) { return ConvertTo<byte[]>(o); }
public static implicit operator BytesType(char[] o) { return ConvertFrom(o); }
public static implicit operator char[](BytesType o) { return ConvertTo<char[]>(o); }
public static implicit operator BytesType(byte o) { return ConvertFrom(o); }
public static implicit operator BytesType(sbyte o) { return ConvertFrom(o); }
public static implicit operator BytesType(short o) { return ConvertFrom(o); }
public static implicit operator BytesType(ushort o) { return ConvertFrom(o); }
public static implicit operator BytesType(int o) { return ConvertFrom(o); }
public static implicit operator BytesType(uint o) { return ConvertFrom(o); }
public static implicit operator BytesType(long o) { return ConvertFrom(o); }
public static implicit operator BytesType(ulong o) { return ConvertFrom(o); }
public static implicit operator BytesType(float o) { return ConvertFrom(o); }
public static implicit operator BytesType(double o) { return ConvertFrom(o); }
public static implicit operator BytesType(decimal o) { return ConvertFrom(o); }
public static implicit operator BytesType(bool o) { return ConvertFrom(o); }
public static implicit operator BytesType(string o) { return ConvertFrom(o); }
public static implicit operator BytesType(char o) { return ConvertFrom(o); }
public static implicit operator BytesType(Guid o) { return ConvertFrom(o); }
public static implicit operator BytesType(DateTime o) { return ConvertFrom(o); }
public static implicit operator BytesType(DateTimeOffset o) { return ConvertFrom(o); }
public static implicit operator byte(BytesType o) { return ConvertTo<byte>(o); }
public static implicit operator sbyte(BytesType o) { return ConvertTo<sbyte>(o); }
public static implicit operator short(BytesType o) { return ConvertTo<short>(o); }
public static implicit operator ushort(BytesType o) { return ConvertTo<ushort>(o); }
public static implicit operator int(BytesType o) { return ConvertTo<int>(o); }
public static implicit operator uint(BytesType o) { return ConvertTo<uint>(o); }
public static implicit operator long(BytesType o) { return ConvertTo<long>(o); }
public static implicit operator ulong(BytesType o) { return ConvertTo<ulong>(o); }
public static implicit operator float(BytesType o) { return ConvertTo<float>(o); }
public static implicit operator double(BytesType o) { return ConvertTo<double>(o); }
public static implicit operator decimal(BytesType o) { return ConvertTo<decimal>(o); }
public static implicit operator bool(BytesType o) { return ConvertTo<bool>(o); }
public static implicit operator string(BytesType o) { return ConvertTo<string>(o); }
public static implicit operator char(BytesType o) { return ConvertTo<char>(o); }
public static implicit operator Guid(BytesType o) { return ConvertTo<Guid>(o); }
public static implicit operator DateTime(BytesType o) { return ConvertTo<DateTime>(o); }
public static implicit operator DateTimeOffset(BytesType o) { return ConvertTo<DateTimeOffset>(o); }
public static implicit operator byte?(BytesType o) { return ConvertTo<byte>(o); }
public static implicit operator sbyte?(BytesType o) { return ConvertTo<sbyte>(o); }
public static implicit operator short?(BytesType o) { return ConvertTo<short>(o); }
public static implicit operator ushort?(BytesType o) { return ConvertTo<ushort>(o); }
public static implicit operator int?(BytesType o) { return ConvertTo<int>(o); }
public static implicit operator uint?(BytesType o) { return ConvertTo<uint>(o); }
public static implicit operator long?(BytesType o) { return ConvertTo<long>(o); }
public static implicit operator ulong?(BytesType o) { return ConvertTo<ulong>(o); }
public static implicit operator float?(BytesType o) { return ConvertTo<float>(o); }
public static implicit operator double?(BytesType o) { return ConvertTo<double>(o); }
public static implicit operator decimal?(BytesType o) { return ConvertTo<decimal>(o); }
public static implicit operator bool?(BytesType o) { return ConvertTo<bool>(o); }
//public static implicit operator string(BytesType o) { return ConvertTo<string>(o); }
public static implicit operator char?(BytesType o) { return ConvertTo<char>(o); }
public static implicit operator Guid?(BytesType o) { return ConvertTo<Guid>(o); }
public static implicit operator DateTime?(BytesType o) { return ConvertTo<DateTime>(o); }
public static implicit operator DateTimeOffset?(BytesType o) { return ConvertTo<DateTimeOffset>(o); }
private static T ConvertTo<T>(BytesType type)
{
if (type == null)
return default(T);
return type.GetValue<T>();
}
private static BytesType ConvertFrom(object o)
{
var type = new BytesType();
type.SetValue(o);
return type;
}
#endregion
}
}