Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refactored VoidType and NullType

  • Loading branch information...
commit 9ced334c61b1ba3378823219992e905800f31686 1 parent d9f2a11
@nberardi nberardi authored
View
1  src/Types/CassandraObject.cs
@@ -172,6 +172,7 @@ private static CassandraObject ConvertFrom(object o)
public static implicit operator DateTimeOffset?(CassandraObject o) { return ConvertTo<DateTimeOffset?>(o); }
public static implicit operator BigInteger?(CassandraObject o) { return ConvertTo<BigInteger?>(o); }
public static implicit operator BigDecimal?(CassandraObject o) { return ConvertTo<BigDecimal?>(o); }
+ //public static implicit operator IPAddress(CassandraObject o) { return ConvertTo<IPAddress>(o); }
public static explicit operator object[](CassandraObject o) { return ConvertTo<object[]>(o); }
public static explicit operator List<object>(CassandraObject o) { return ConvertTo<List<object>>(o); }
View
96 src/Types/EmptyType.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Net;
+using System.Numerics;
using System.Text;
namespace FluentCassandra.Types
@@ -14,12 +16,13 @@ public override void SetValue(object obj)
if (obj == null)
return;
- var type = obj.GetType();
+ if (obj is byte[] && ((byte[])obj).Length == 0)
+ return;
- if (type == typeof(string) && String.IsNullOrEmpty((string)obj))
+ if (obj is string && obj == "")
return;
- if (type == typeof(byte[]) && ((byte[])obj).Length == 0)
+ if (obj.GetType().IsValueType && Activator.CreateInstance(obj.GetType()) == obj)
return;
throw new NotSupportedException("You cannot set the value of an EmptyType.");
@@ -27,17 +30,19 @@ public override void SetValue(object obj)
protected override object GetValueInternal(Type type)
{
- if (type == typeof(string))
- return "";
- else if (type == typeof(byte[]))
+ if (type == typeof(byte[]))
return _value;
- else
- throw new InvalidCastException(String.Format("{0} cannot be cast to {1}", typeof(byte[]), type));
+ else if (type == typeof(string))
+ return "";
+ else if (type.IsValueType)
+ return Activator.CreateInstance(type);
+ else
+ return null;
}
protected override TypeCode TypeCode
{
- get { return TypeCode.Object; }
+ get { return TypeCode.DBNull; }
}
public override byte[] ToBigEndian()
@@ -62,8 +67,11 @@ public override object GetValue()
public override bool Equals(object obj)
{
+ if (obj == null)
+ return true;
+
if (obj is EmptyType)
- return _value == ((EmptyType)obj)._value;
+ return true;
if (obj is string)
return String.IsNullOrEmpty((string)obj);
@@ -83,11 +91,79 @@ public override int GetHashCode()
#region Conversion
+ public static implicit operator EmptyType(byte[] o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(char[] o) { return ConvertFrom(o); }
+
public static implicit operator EmptyType(byte o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(sbyte o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(short o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(ushort o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(int o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(uint o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(long o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(ulong o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(float o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(double o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(decimal o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(bool o) { return ConvertFrom(o); }
public static implicit operator EmptyType(string o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(char o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(Guid o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(DateTime o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(DateTimeOffset o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(BigInteger o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(BigDecimal o) { return ConvertFrom(o); }
+ public static implicit operator EmptyType(IPAddress o) { return ConvertFrom(o); }
+
+ public static implicit operator byte[](EmptyType o) { return ConvertTo<byte[]>(o); }
+ public static implicit operator char[](EmptyType o) { return ConvertTo<char[]>(o); }
public static implicit operator byte(EmptyType o) { return ConvertTo<byte>(o); }
+ public static implicit operator sbyte(EmptyType o) { return ConvertTo<sbyte>(o); }
+ public static implicit operator short(EmptyType o) { return ConvertTo<short>(o); }
+ public static implicit operator ushort(EmptyType o) { return ConvertTo<ushort>(o); }
+ public static implicit operator int(EmptyType o) { return ConvertTo<int>(o); }
+ public static implicit operator uint(EmptyType o) { return ConvertTo<uint>(o); }
+ public static implicit operator long(EmptyType o) { return ConvertTo<long>(o); }
+ public static implicit operator ulong(EmptyType o) { return ConvertTo<ulong>(o); }
+ public static implicit operator float(EmptyType o) { return ConvertTo<float>(o); }
+ public static implicit operator double(EmptyType o) { return ConvertTo<double>(o); }
+ public static implicit operator decimal(EmptyType o) { return ConvertTo<decimal>(o); }
+ public static implicit operator bool(EmptyType o) { return ConvertTo<bool>(o); }
public static implicit operator string(EmptyType o) { return ConvertTo<string>(o); }
+ public static implicit operator char(EmptyType o) { return ConvertTo<char>(o); }
+ public static implicit operator Guid(EmptyType o) { return ConvertTo<Guid>(o); }
+ public static implicit operator DateTime(EmptyType o) { return ConvertTo<DateTime>(o); }
+ public static implicit operator DateTimeOffset(EmptyType o) { return ConvertTo<DateTimeOffset>(o); }
+ public static implicit operator BigInteger(EmptyType o) { return ConvertTo<BigInteger>(o); }
+ public static implicit operator BigDecimal(EmptyType o) { return ConvertTo<BigDecimal>(o); }
+ public static implicit operator IPAddress(EmptyType o) { return ConvertTo<IPAddress>(o); }
+
+ public static implicit operator byte?(EmptyType o) { return ConvertTo<byte?>(o); }
+ public static implicit operator sbyte?(EmptyType o) { return ConvertTo<sbyte?>(o); }
+ public static implicit operator short?(EmptyType o) { return ConvertTo<short?>(o); }
+ public static implicit operator ushort?(EmptyType o) { return ConvertTo<ushort?>(o); }
+ public static implicit operator int?(EmptyType o) { return ConvertTo<int?>(o); }
+ public static implicit operator uint?(EmptyType o) { return ConvertTo<uint?>(o); }
+ public static implicit operator long?(EmptyType o) { return ConvertTo<long?>(o); }
+ public static implicit operator ulong?(EmptyType o) { return ConvertTo<ulong?>(o); }
+ public static implicit operator float?(EmptyType o) { return ConvertTo<float?>(o); }
+ public static implicit operator double?(EmptyType o) { return ConvertTo<double?>(o); }
+ public static implicit operator decimal?(EmptyType o) { return ConvertTo<decimal?>(o); }
+ public static implicit operator bool?(EmptyType o) { return ConvertTo<bool?>(o); }
+ //public static implicit operator string(EmptyType o) { return Convert<string>(o); }
+ public static implicit operator char?(EmptyType o) { return ConvertTo<char?>(o); }
+ public static implicit operator Guid?(EmptyType o) { return ConvertTo<Guid?>(o); }
+ public static implicit operator DateTime?(EmptyType o) { return ConvertTo<DateTime?>(o); }
+ public static implicit operator DateTimeOffset?(EmptyType o) { return ConvertTo<DateTimeOffset?>(o); }
+ public static implicit operator BigInteger?(EmptyType o) { return ConvertTo<BigInteger?>(o); }
+ public static implicit operator BigDecimal?(EmptyType o) { return ConvertTo<BigDecimal?>(o); }
+ //public static implicit operator IPAddress(EmptyType o) { return ConvertTo<IPAddress>(o); }
+
+ public static explicit operator object[](EmptyType o) { return ConvertTo<object[]>(o); }
+ public static explicit operator List<object>(EmptyType o) { return ConvertTo<List<object>>(o); }
+ public static explicit operator CassandraObject[](EmptyType o) { return ConvertTo<CassandraObject[]>(o); }
+ public static explicit operator List<CassandraObject>(EmptyType o) { return ConvertTo<List<CassandraObject>>(o); }
private static T ConvertTo<T>(EmptyType type)
{
View
77 src/Types/NullType.cs
@@ -9,13 +9,19 @@ internal class NullType : CassandraObject
private NullType() { }
+ public override object GetValue()
+ {
+ return null;
+ }
+
public override void SetValue(object obj)
{
+ throw new NotSupportedException();
}
protected override object GetValueInternal(Type type)
{
- return type.IsValueType ? Activator.CreateInstance(type) : null;
+ return null;
}
protected override TypeCode TypeCode
@@ -23,10 +29,28 @@ protected override TypeCode TypeCode
get { return TypeCode.DBNull; }
}
- #region Equality
+ public override byte[] ToBigEndian()
+ {
+ return new byte[0];
+ }
+
+ public override void SetValueFromBigEndian(byte[] value)
+ {
+ throw new NotSupportedException();
+ }
+
+ public override int GetHashCode()
+ {
+ return -1494218850;
+ }
public override bool Equals(object obj)
{
+ return EqualsNull(obj);
+ }
+
+ private static bool EqualsNull(object obj)
+ {
if (obj == null)
return true;
@@ -36,57 +60,24 @@ public override bool Equals(object obj)
return false;
}
- public override int GetHashCode()
- {
- return 0;
- }
-
- public static bool operator ==(NullType type, object obj)
+ public static bool operator ==(object a, NullType b)
{
- if (Object.Equals(type, null))
- type = Value;
-
- return type.Equals(obj);
+ return EqualsNull(a);
}
- public static bool operator !=(NullType type, object obj)
+ public static bool operator ==(NullType a, object b)
{
- if (Object.Equals(type, null))
- type = Value;
-
- return !type.Equals(obj);
+ return EqualsNull(b);
}
- #endregion
-
- public override byte[] ToBigEndian()
+ public static bool operator !=(object a, NullType b)
{
- return new byte[0];
+ return !EqualsNull(a);
}
- public override void SetValueFromBigEndian(byte[] value)
+ public static bool operator !=(NullType a, object b)
{
+ return !EqualsNull(b);
}
-
- public override object GetValue() { return null; }
-
- public static implicit operator byte?(NullType o) { return null; }
- public static implicit operator sbyte?(NullType o) { return null; }
- public static implicit operator short?(NullType o) { return null; }
- public static implicit operator ushort?(NullType o) { return null; }
- public static implicit operator int?(NullType o) { return null; }
- public static implicit operator uint?(NullType o) { return null; }
- public static implicit operator long?(NullType o) { return null; }
- public static implicit operator ulong?(NullType o) { return null; }
- public static implicit operator float?(NullType o) { return null; }
- public static implicit operator double?(NullType o) { return null; }
- public static implicit operator decimal?(NullType o) { return null; }
- public static implicit operator bool?(NullType o) { return null; }
- public static implicit operator string(NullType o) { return null; }
- public static implicit operator char?(NullType o) { return null; }
- public static implicit operator Guid?(NullType o) { return null; }
- public static implicit operator DateTime?(NullType o) { return null; }
- public static implicit operator DateTimeOffset?(NullType o) { return null; }
- public static implicit operator BigInteger?(NullType o) { return null; }
}
}
View
14 src/Types/VoidType.cs
@@ -7,34 +7,36 @@ namespace FluentCassandra.Types
/// </summary>
internal class VoidType : CassandraObject
{
+ private VoidType() { }
+
public override void SetValue(object obj)
{
- throw new NotImplementedException();
+ throw new NotSupportedException();
}
protected override object GetValueInternal(Type type)
{
- throw new NotImplementedException();
+ throw new NotSupportedException();
}
protected override TypeCode TypeCode
{
- get { throw new NotImplementedException(); }
+ get { throw new NotSupportedException(); }
}
public override void SetValueFromBigEndian(byte[] value)
{
- throw new NotImplementedException();
+ throw new NotSupportedException();
}
public override byte[] ToBigEndian()
{
- throw new NotImplementedException();
+ throw new NotSupportedException();
}
public override object GetValue()
{
- throw new NotImplementedException();
+ throw new NotSupportedException();
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.