Permalink
Browse files

modified the type converters to be more flexible

  • Loading branch information...
1 parent 3b61e2b commit 8876f7ecf4fcfbed6ba9376b134a9e8eb186d732 @nberardi nberardi committed May 25, 2012
@@ -127,7 +127,7 @@ public override byte[] ConvertFromInternal(object value)
}
if (value is char[])
- bytes = ((char[])value).Select(c => (byte)c).ToArray();
+ bytes = ((char[])value).Cast<byte>().ToArray();
if (bytes == null)
{
@@ -199,7 +199,7 @@ public override object ConvertToInternal(byte[] value, Type destinationType)
return new BigDecimal(bytes);
if (destinationType == typeof(char[]))
- return value.Select(b => (char)b).ToArray();
+ return value.Cast<char>().ToArray();
switch (Type.GetTypeCode(destinationType))
{
@@ -210,10 +210,7 @@ public override object ConvertToInternal(byte[] value, Type destinationType)
case TypeCode.Boolean:
return BitConverter.ToBoolean(bytes, 0);
case TypeCode.Char:
- if (bytes.Length == 1)
- return (char)bytes[0];
- else
- return BitConverter.ToChar(bytes, 0);
+ return BitConverter.ToChar(bytes, 0);
case TypeCode.Double:
return BitConverter.ToDouble(bytes, 0);
case TypeCode.Int16:
@@ -56,18 +56,18 @@ public override BigDecimal ConvertFromInternal(object value)
if (value is BigDecimal) return (BigDecimal)value;
- if (value is byte) return (BigDecimal)(byte)value;
- if (value is short) return (BigDecimal)(short)value;
- if (value is int) return (BigDecimal)(int)value;
- if (value is long) return (BigDecimal)(long)value;
- if (value is sbyte) return (BigDecimal)(sbyte)value;
- if (value is ushort) return (BigDecimal)(ushort)value;
- if (value is uint) return (BigDecimal)(uint)value;
- if (value is ulong) return (BigDecimal)(ulong)value;
- if (value is float) return (BigDecimal)(float)value;
- if (value is double) return (BigDecimal)(double)value;
- if (value is decimal) return (BigDecimal)(decimal)value;
- if (value is BigInteger) return (BigDecimal)(BigInteger)value;
+ if (value is byte) return new BigDecimal((byte)value);
+ if (value is short) return new BigDecimal((short)value);
+ if (value is int) return new BigDecimal((int)value);
+ if (value is long) return new BigDecimal((long)value);
+ if (value is sbyte) return new BigDecimal((sbyte)value);
+ if (value is ushort) return new BigDecimal((ushort)value);
+ if (value is uint) return new BigDecimal((uint)value);
+ if (value is ulong) return new BigDecimal((ulong)value);
+ if (value is float) return new BigDecimal((float)value);
+ if (value is double) return new BigDecimal((double)value);
+ if (value is decimal) return new BigDecimal((decimal)value);
+ if (value is BigInteger) return new BigDecimal((BigInteger)value, 0);
return default(BigDecimal);
}
@@ -17,6 +17,9 @@ public override bool CanConvertFrom(Type sourceType)
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
+ case TypeCode.Single:
+ case TypeCode.Double:
+ case TypeCode.Decimal:
return true;
default:
@@ -36,6 +39,9 @@ public override bool CanConvertTo(Type destinationType)
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
+ case TypeCode.Single:
+ case TypeCode.Double:
+ case TypeCode.Decimal:
return true;
default:
@@ -50,14 +56,17 @@ public override BigInteger ConvertFromInternal(object value)
if (value is BigInteger) return (BigInteger)value;
- if (value is byte) return (BigInteger)(byte)value;
- if (value is short) return (BigInteger)(short)value;
- if (value is int) return (BigInteger)(int)value;
- if (value is long) return (BigInteger)(long)value;
- if (value is sbyte) return (BigInteger)(sbyte)value;
- if (value is ushort) return (BigInteger)(ushort)value;
- if (value is uint) return (BigInteger)(uint)value;
- if (value is ulong) return (BigInteger)(ulong)value;
+ if (value is byte) return new BigInteger((byte)value);
+ if (value is short) return new BigInteger((short)value);
+ if (value is int) return new BigInteger((int)value);
+ if (value is long) return new BigInteger((long)value);
+ if (value is sbyte) return new BigInteger((sbyte)value);
+ if (value is ushort) return new BigInteger((ushort)value);
+ if (value is uint) return new BigInteger((uint)value);
+ if (value is ulong) return new BigInteger((ulong)value);
+ if (value is float) return new BigInteger((float)value);
+ if (value is double) return new BigInteger((double)value);
+ if (value is decimal) return new BigInteger((decimal)value);
return default(BigInteger);
}
@@ -77,6 +86,9 @@ public override object ConvertToInternal(BigInteger value, Type destinationType)
if (destinationType == typeof(ushort)) return (ushort)value;
if (destinationType == typeof(uint)) return (uint)value;
if (destinationType == typeof(ulong)) return (ulong)value;
+ if (destinationType == typeof(float)) return (float)value;
+ if (destinationType == typeof(double)) return (double)value;
+ if (destinationType == typeof(decimal)) return (decimal)value;
return null;
}
@@ -6,86 +6,34 @@ class LongTypeConverter : CassandraObjectConverter<long>
{
public override bool CanConvertFrom(Type sourceType)
{
- switch (Type.GetTypeCode(sourceType))
- {
- case TypeCode.Byte:
- case TypeCode.Int16:
- case TypeCode.Int32:
- case TypeCode.Int64:
- case TypeCode.SByte:
- case TypeCode.UInt16:
- case TypeCode.UInt32:
- case TypeCode.UInt64:
- return true;
+ if (Type.GetTypeCode(sourceType) != TypeCode.Object)
+ return true;
- default:
- return sourceType == typeof(byte[]);
- }
+ return sourceType == typeof(byte[]);
}
public override bool CanConvertTo(Type destinationType)
{
- switch (Type.GetTypeCode(destinationType))
- {
- case TypeCode.Byte:
- case TypeCode.Int16:
- case TypeCode.Int32:
- case TypeCode.Int64:
- case TypeCode.SByte:
- case TypeCode.UInt16:
- case TypeCode.UInt32:
- case TypeCode.UInt64:
- return true;
+ if (Type.GetTypeCode(destinationType) != TypeCode.Object)
+ return true;
- default:
- return destinationType == typeof(byte[]);
- }
+ return destinationType == typeof(byte[]);
}
public override long ConvertFromInternal(object value)
{
if (value is byte[])
- {
- var vbytes = (byte[])value;
- var bytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
+ return ((byte[])value).FromBytes<long>();
- for (int i = 0; i < vbytes.Length; i++)
- bytes[i] = vbytes[i];
-
- return bytes.FromBytes<long>();
- }
-
- if (value is long) return (long)value;
-
- if (value is byte) return Convert.ToInt64((byte)value);
- if (value is short) return Convert.ToInt64((short)value);
- if (value is int) return Convert.ToInt64((int)value);
-
- if (value is sbyte) return Convert.ToInt64((sbyte)value);
- if (value is ushort) return Convert.ToInt64((ushort)value);
- if (value is uint) return Convert.ToInt64((uint)value);
- if (value is ulong) return Convert.ToInt64((ulong)value);
-
- return default(long);
+ return (long)Convert.ChangeType(value, typeof(long));
}
public override object ConvertToInternal(long value, Type destinationType)
{
if (destinationType == typeof(byte[]))
return value.ToBytes();
- if (destinationType == typeof(long)) return value;
-
- if (destinationType == typeof(byte)) return (byte)value;
- if (destinationType == typeof(short)) return (short)value;
- if (destinationType == typeof(int)) return (int)value;
-
- if (destinationType == typeof(sbyte)) return (sbyte)value;
- if (destinationType == typeof(ushort)) return (ushort)value;
- if (destinationType == typeof(uint)) return (uint)value;
- if (destinationType == typeof(ulong)) return (ulong)value;
-
- return null;
+ return Convert.ChangeType(value, destinationType);
}
}
}

0 comments on commit 8876f7e

Please sign in to comment.