Mercurial > silverbladetech
comparison Stocks/Stocks.Common/Models/Price.cs @ 0:e5d46bb6cdb0
Initial commit
author | adminSH stevenhollidge@hotmail.com |
---|---|
date | Mon, 20 Feb 2012 13:52:35 +0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e5d46bb6cdb0 |
---|---|
1 using Stocks.Common.Core; | |
2 using System.Collections; | |
3 using System; | |
4 using System.Collections.Generic; | |
5 | |
6 namespace Stocks.Common.Models | |
7 { | |
8 public class Price : IEquatable<Price>, IComparable<Price>, IComparable | |
9 { | |
10 public string Symbol { get; set; } | |
11 public decimal CurrentPrice { get; set; } | |
12 public decimal PreviousPrice { get; set; } | |
13 | |
14 public Price() | |
15 { | |
16 } | |
17 | |
18 public Price(string symbol, decimal currentPrice, decimal previousPrice) | |
19 : this() | |
20 { | |
21 Symbol = symbol; | |
22 CurrentPrice = currentPrice; | |
23 PreviousPrice = previousPrice; | |
24 } | |
25 | |
26 public override bool Equals(object obj) | |
27 { | |
28 if (obj is Price) | |
29 return Equals(obj as Price); | |
30 else | |
31 return false; | |
32 } | |
33 | |
34 public bool Equals(Price other) | |
35 { | |
36 return (Symbol == other.Symbol | |
37 && CurrentPrice == other.CurrentPrice | |
38 && PreviousPrice == other.PreviousPrice); | |
39 } | |
40 | |
41 public int CompareTo(Price other) | |
42 { | |
43 return Symbol.CompareTo(other.Symbol); | |
44 } | |
45 | |
46 public int CompareTo(Price other, PriceComparisonType comparisonType) | |
47 { | |
48 switch (comparisonType) | |
49 { | |
50 case PriceComparisonType.NotSet: | |
51 case PriceComparisonType.Symbol: | |
52 return Symbol.CompareTo(other.Symbol); | |
53 case PriceComparisonType.CurrentPrice: | |
54 return CurrentPrice.CompareTo(other.CurrentPrice); | |
55 case PriceComparisonType.PreviousPrice: | |
56 return PreviousPrice.CompareTo(other.PreviousPrice); | |
57 default: | |
58 throw new Exception("Unknown comparison type"); | |
59 } | |
60 } | |
61 | |
62 public int CompareTo(object obj) | |
63 { | |
64 Price other; | |
65 if (obj is Price) | |
66 other = obj as Price; | |
67 else | |
68 throw new ArgumentException("obj is not a Price"); | |
69 | |
70 return CompareTo(other); | |
71 } | |
72 public override int GetHashCode() | |
73 { | |
74 int hash = 13; | |
75 hash = (hash * 7) + Symbol.GetHashCode(); | |
76 hash = (hash * 7) + CurrentPrice.GetHashCode(); | |
77 hash = (hash * 7) + PreviousPrice.GetHashCode(); | |
78 return hash; | |
79 } | |
80 | |
81 public static bool operator ==(Price lhs, Price rhs) | |
82 { | |
83 if (System.Object.ReferenceEquals(lhs, rhs)) | |
84 return true; | |
85 | |
86 if (((object)lhs == null) || ((object)rhs == null)) | |
87 return false; | |
88 | |
89 return lhs.Symbol == rhs.Symbol | |
90 && lhs.CurrentPrice == rhs.CurrentPrice | |
91 && lhs.PreviousPrice == rhs.PreviousPrice; | |
92 } | |
93 | |
94 public static bool operator !=(Price lhs, Price rhs) | |
95 { | |
96 return !(lhs == rhs); | |
97 } | |
98 | |
99 | |
100 public class PriceComparer : IComparer<Price>, IComparer | |
101 { | |
102 public PriceComparisonType ComparisonMethod { get; set; } | |
103 | |
104 public int Compare(Price x, Price y) | |
105 { | |
106 return x.CompareTo(y, ComparisonMethod); | |
107 } | |
108 | |
109 public int Compare(object x, object y) | |
110 { | |
111 Price lhs, rhs; | |
112 | |
113 if (x is Price) | |
114 lhs = x as Price; | |
115 else | |
116 throw new ArgumentException("x is not a Price"); | |
117 | |
118 if (y is Price) | |
119 rhs = y as Price; | |
120 else | |
121 throw new ArgumentException("y is not a Price"); | |
122 | |
123 return lhs.CompareTo(rhs, ComparisonMethod); | |
124 } | |
125 } | |
126 } | |
127 | |
128 public enum PriceComparisonType { NotSet = 0, Symbol, CurrentPrice, PreviousPrice } | |
129 } |