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