summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Core/Messaging/Reflection/MessageDescriptionCollection.cs
blob: 79ef172ab771249e32523d940c6870e9a4e1eb83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//-----------------------------------------------------------------------
// <copyright file="MessageDescriptionCollection.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Messaging.Reflection {
	using System;
	using System.Collections.Generic;
	using System.Diagnostics.CodeAnalysis;
	using System.Diagnostics.Contracts;

	/// <summary>
	/// A cache of <see cref="MessageDescription"/> instances.
	/// </summary>
	[ContractVerification(true)]
	internal class MessageDescriptionCollection : IEnumerable<MessageDescription> {
		/// <summary>
		/// A dictionary of reflected message types and the generated reflection information.
		/// </summary>
		private readonly Dictionary<MessageTypeAndVersion, MessageDescription> reflectedMessageTypes = new Dictionary<MessageTypeAndVersion, MessageDescription>();

		/// <summary>
		/// Initializes a new instance of the <see cref="MessageDescriptionCollection"/> class.
		/// </summary>
		internal MessageDescriptionCollection() {
		}

		#region IEnumerable<MessageDescription> Members

		/// <summary>
		/// Returns an enumerator that iterates through a collection.
		/// </summary>
		/// <returns>
		/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
		/// </returns>
		public IEnumerator<MessageDescription> GetEnumerator() {
			return this.reflectedMessageTypes.Values.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		/// <summary>
		/// Returns an enumerator that iterates through a collection.
		/// </summary>
		/// <returns>
		/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
		/// </returns>
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
			return this.reflectedMessageTypes.Values.GetEnumerator();
		}

		#endregion

		/// <summary>
		/// Gets a <see cref="MessageDescription"/> instance prepared for the
		/// given message type.
		/// </summary>
		/// <param name="messageType">A type that implements <see cref="IMessage"/>.</param>
		/// <param name="messageVersion">The protocol version of the message.</param>
		/// <returns>A <see cref="MessageDescription"/> instance.</returns>
		[SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Diagnostics.Contracts.__ContractsRuntime.Assume(System.Boolean,System.String,System.String)", Justification = "No localization required.")]
		[Pure]
		internal MessageDescription Get(Type messageType, Version messageVersion) {
			Requires.NotNullSubtype<IMessage>(messageType, "messageType");
			Requires.NotNull(messageVersion, "messageVersion");
			Contract.Ensures(Contract.Result<MessageDescription>() != null);

			MessageTypeAndVersion key = new MessageTypeAndVersion(messageType, messageVersion);

			MessageDescription result;
			if (!this.reflectedMessageTypes.TryGetValue(key, out result)) {
				lock (this.reflectedMessageTypes) {
					if (!this.reflectedMessageTypes.TryGetValue(key, out result)) {
						this.reflectedMessageTypes[key] = result = new MessageDescription(messageType, messageVersion);
					}
				}
			}

			Contract.Assume(result != null, "We should never assign null values to this dictionary.");
			return result;
		}

		/// <summary>
		/// Gets a <see cref="MessageDescription"/> instance prepared for the
		/// given message type.
		/// </summary>
		/// <param name="message">The message for which a <see cref="MessageDescription"/> should be obtained.</param>
		/// <returns>
		/// A <see cref="MessageDescription"/> instance.
		/// </returns>
		[Pure]
		internal MessageDescription Get(IMessage message) {
			Requires.NotNull(message, "message");
			Contract.Ensures(Contract.Result<MessageDescription>() != null);
			return this.Get(message.GetType(), message.Version);
		}

		/// <summary>
		/// Gets the dictionary that provides read/write access to a message.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <returns>The dictionary.</returns>
		[Pure]
		internal MessageDictionary GetAccessor(IMessage message) {
			Requires.NotNull(message, "message");
			return this.GetAccessor(message, false);
		}

		/// <summary>
		/// Gets the dictionary that provides read/write access to a message.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="getOriginalValues">A value indicating whether this message dictionary will retrieve original values instead of normalized ones.</param>
		/// <returns>The dictionary.</returns>
		[Pure]
		internal MessageDictionary GetAccessor(IMessage message, bool getOriginalValues) {
			Requires.NotNull(message, "message");
			return this.Get(message).GetDictionary(message, getOriginalValues);
		}

		/// <summary>
		/// A struct used as the key to bundle message type and version.
		/// </summary>
		[ContractVerification(true)]
		private struct MessageTypeAndVersion {
			/// <summary>
			/// Backing store for the <see cref="Type"/> property.
			/// </summary>
			private readonly Type type;

			/// <summary>
			/// Backing store for the <see cref="Version"/> property.
			/// </summary>
			private readonly Version version;

			/// <summary>
			/// Initializes a new instance of the <see cref="MessageTypeAndVersion"/> struct.
			/// </summary>
			/// <param name="messageType">Type of the message.</param>
			/// <param name="messageVersion">The message version.</param>
			internal MessageTypeAndVersion(Type messageType, Version messageVersion) {
				Requires.NotNull(messageType, "messageType");
				Requires.NotNull(messageVersion, "messageVersion");

				this.type = messageType;
				this.version = messageVersion;
			}

			/// <summary>
			/// Gets the message type.
			/// </summary>
			[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Exposes basic identity on the type.")]
			internal Type Type {
				get { return this.type; }
			}

			/// <summary>
			/// Gets the message version.
			/// </summary>
			[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Exposes basic identity on the type.")]
			internal Version Version {
				get { return this.version; }
			}

			/// <summary>
			/// Implements the operator ==.
			/// </summary>
			/// <param name="first">The first object to compare.</param>
			/// <param name="second">The second object to compare.</param>
			/// <returns>The result of the operator.</returns>
			public static bool operator ==(MessageTypeAndVersion first, MessageTypeAndVersion second) {
				// structs cannot be null, so this is safe
				return first.Equals(second);
			}

			/// <summary>
			/// Implements the operator !=.
			/// </summary>
			/// <param name="first">The first object to compare.</param>
			/// <param name="second">The second object to compare.</param>
			/// <returns>The result of the operator.</returns>
			public static bool operator !=(MessageTypeAndVersion first, MessageTypeAndVersion second) {
				// structs cannot be null, so this is safe
				return !first.Equals(second);
			}

			/// <summary>
			/// Indicates whether this instance and a specified object are equal.
			/// </summary>
			/// <param name="obj">Another object to compare to.</param>
			/// <returns>
			/// true if <paramref name="obj"/> and this instance are the same type and represent the same value; otherwise, false.
			/// </returns>
			public override bool Equals(object obj) {
				if (obj is MessageTypeAndVersion) {
					MessageTypeAndVersion other = (MessageTypeAndVersion)obj;
					return this.type == other.type && this.version == other.version;
				} else {
					return false;
				}
			}

			/// <summary>
			/// Returns the hash code for this instance.
			/// </summary>
			/// <returns>
			/// A 32-bit signed integer that is the hash code for this instance.
			/// </returns>
			public override int GetHashCode() {
				return this.type.GetHashCode();
			}
		}
	}
}