Added GetHashCode() methods to the AX classes, and fixed their Equals methods.
[dotnetoauth.git] / src / DotNetOpenAuth / OpenId / Extensions / AttributeExchange / StoreResponse.cs
blob97178b608d62a2a554301049211fe1f353c6d3c2
1 //-----------------------------------------------------------------------
2 // <copyright file="StoreResponse.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth.OpenId.Extensions.AttributeExchange {
8 using System;
9 using DotNetOpenAuth.Messaging;
10 using DotNetOpenAuth.OpenId.Messages;
12 /// <summary>
13 /// The Attribute Exchange Store message, response leg.
14 /// </summary>
15 public sealed class StoreResponse : ExtensionBase {
16 /// <summary>
17 /// The factory method that may be used in deserialization of this message.
18 /// </summary>
19 internal static readonly OpenIdExtensionFactory.CreateDelegate Factory = (typeUri, data, baseMessage) => {
20 if (typeUri == Constants.TypeUri && baseMessage is IndirectSignedResponse) {
21 string mode;
22 if (data.TryGetValue("mode", out mode) && (mode == SuccessMode || mode == FailureMode)) {
23 return new StoreResponse();
27 return null;
30 /// <summary>
31 /// The value of the mode parameter used to express a successful store operation.
32 /// </summary>
33 private const string SuccessMode = "store_response_success";
35 /// <summary>
36 /// The value of the mode parameter used to express a store operation failure.
37 /// </summary>
38 private const string FailureMode = "store_response_failure";
40 /// <summary>
41 /// Initializes a new instance of the <see cref="StoreResponse"/> class
42 /// to represent a successful store operation.
43 /// </summary>
44 public StoreResponse()
45 : base(new Version(1, 0), Constants.TypeUri, null) {
46 this.Succeeded = true;
49 /// <summary>
50 /// Initializes a new instance of the <see cref="StoreResponse"/> class
51 /// to represent a failed store operation.
52 /// </summary>
53 /// <param name="failureReason">The reason for failure.</param>
54 public StoreResponse(string failureReason)
55 : this() {
56 this.Succeeded = false;
57 this.FailureReason = failureReason;
60 /// <summary>
61 /// Gets or sets a value indicating whether the storage request succeeded.
62 /// </summary>
63 /// <value>Defaults to <c>true</c>.</value>
64 public bool Succeeded {
65 get { return this.Mode == SuccessMode; }
66 set { this.Mode = value ? SuccessMode : FailureMode; }
69 /// <summary>
70 /// Gets or sets the reason for the failure, if applicable.
71 /// </summary>
72 [MessagePart("error", IsRequired = false)]
73 public string FailureReason { get; set; }
75 /// <summary>
76 /// Gets or sets the mode argument.
77 /// </summary>
78 /// <value>One of 'store_response_success' or 'store_response_failure'.</value>
79 [MessagePart("mode", IsRequired = true)]
80 private string Mode { get; set; }
82 /// <summary>
83 /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
84 /// </summary>
85 /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
86 /// <returns>
87 /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
88 /// </returns>
89 /// <exception cref="T:System.NullReferenceException">
90 /// The <paramref name="obj"/> parameter is null.
91 /// </exception>
92 public override bool Equals(object obj) {
93 var other = obj as StoreResponse;
94 if (other == null) {
95 return false;
98 if (this.Version != other.Version) {
99 return false;
102 if (this.Succeeded != other.Succeeded) {
103 return false;
106 if (this.FailureReason != other.FailureReason) {
107 return false;
110 return true;
113 /// <summary>
114 /// Serves as a hash function for a particular type.
115 /// </summary>
116 /// <returns>
117 /// A hash code for the current <see cref="T:System.Object"/>.
118 /// </returns>
119 public override int GetHashCode() {
120 unchecked {
121 int hashCode = this.Version.GetHashCode();
122 hashCode += this.Succeeded ? 0 : 1;
123 if (this.FailureReason != null) {
124 hashCode += this.FailureReason.GetHashCode();
127 return hashCode;
131 /// <summary>
132 /// Checks the message state for conformity to the protocol specification
133 /// and throws an exception if the message is invalid.
134 /// </summary>
135 /// <remarks>
136 /// <para>Some messages have required fields, or combinations of fields that must relate to each other
137 /// in specialized ways. After deserializing a message, this method checks the state of the
138 /// message to see if it conforms to the protocol.</para>
139 /// <para>Note that this property should <i>not</i> check signatures or perform any state checks
140 /// outside this scope of this particular message.</para>
141 /// </remarks>
142 /// <exception cref="ProtocolException">Thrown if the message is invalid.</exception>
143 protected override void EnsureValidMessage() {
144 base.EnsureValidMessage();
146 ErrorUtilities.VerifyProtocol(
147 this.Mode == SuccessMode || this.Mode == FailureMode,
148 MessagingStrings.UnexpectedMessagePartValue,
149 "mode",
150 this.Mode);