annotate MetroWpf/Stocks.Common/Models/Price.cs @ 20:6109bc268b90

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