1 /* Copyright (C) 2004 - 2009  Versant Inc.  http://www.db4o.com */
2 
3 using System;
4 using Db4objects.Db4o.Foundation;
5 using Db4objects.Db4o.Internal;
6 using Db4objects.Db4o.Internal.Handlers;
7 using Db4objects.Db4o.Internal.Marshall;
8 using Db4objects.Db4o.Marshall;
9 using Db4objects.Db4o.Reflect;
10 
11 namespace Db4objects.Db4o.Internal.Handlers
12 {
13 	/// <exclude></exclude>
14 	public class DoubleHandler : LongHandler
15 	{
16 		private static readonly double Defaultvalue = System.Convert.ToDouble(0);
17 
Coerce(IReflectClass claxx, object obj)18 		public override object Coerce(IReflectClass claxx, object obj)
19 		{
20 			return Coercion4.ToDouble(obj);
21 		}
22 
DefaultValue()23 		public override object DefaultValue()
24 		{
25 			return Defaultvalue;
26 		}
27 
PrimitiveJavaClass()28 		public override Type PrimitiveJavaClass()
29 		{
30 			return typeof(double);
31 		}
32 
33 		/// <exception cref="Db4objects.Db4o.CorruptionException"></exception>
Read(MarshallerFamily mf, StatefulBuffer buffer, bool redirect )34 		public override object Read(MarshallerFamily mf, StatefulBuffer buffer, bool redirect
35 			)
36 		{
37 			return mf._primitive.ReadDouble(buffer);
38 		}
39 
Read1(ByteArrayBuffer buffer)40 		internal override object Read1(ByteArrayBuffer buffer)
41 		{
42 			return PrimitiveMarshaller().ReadDouble(buffer);
43 		}
44 
Write(object a_object, ByteArrayBuffer a_bytes)45 		public override void Write(object a_object, ByteArrayBuffer a_bytes)
46 		{
47 			a_bytes.WriteLong(Platform4.DoubleToLong(((double)a_object)));
48 		}
49 
Read(IReadContext context)50 		public override object Read(IReadContext context)
51 		{
52 			long l = (long)base.Read(context);
53 			return Platform4.LongToDouble(l);
54 		}
55 
Write(IWriteContext context, object obj)56 		public override void Write(IWriteContext context, object obj)
57 		{
58 			context.WriteLong(Platform4.DoubleToLong(((double)obj)));
59 		}
60 
InternalPrepareComparison(object source)61 		public override IPreparedComparison InternalPrepareComparison(object source)
62 		{
63 			double sourceDouble = ((double)source);
64 			return new _IPreparedComparison_55(sourceDouble);
65 		}
66 
67 		private sealed class _IPreparedComparison_55 : IPreparedComparison
68 		{
_IPreparedComparison_55(double sourceDouble)69 			public _IPreparedComparison_55(double sourceDouble)
70 			{
71 				this.sourceDouble = sourceDouble;
72 			}
73 
CompareTo(object target)74 			public int CompareTo(object target)
75 			{
76 				if (target == null)
77 				{
78 					return 1;
79 				}
80 				double targetDouble = ((double)target);
81 				return sourceDouble == targetDouble ? 0 : (sourceDouble < targetDouble ? -1 : 1);
82 			}
83 
84 			private readonly double sourceDouble;
85 		}
86 	}
87 }
88