Skip to content

Commit cdaac41

Browse files
committed
Generalized method parameters as IList<Topic>
Previously, `ReadOnlyTopicCollection` and `ReadOnlyTopicCollection<T>` had to be constructed explicitly using a `TopicCollection` or `TopicCollection<T>`. To make them more flexible, modified the behavior to accept an `IList<Topic>`. If the `IList<Topic>` is a `TopicCollection<T>` then the underlying collection uses it as is for keying; otherwise, it is converted to a `TopicCollection<T>`. This provides more flexibility, while not changing the normal day-to-day usage of the methods.
1 parent e927c63 commit cdaac41

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

Ignia.Topics/Collections/ReadOnlyTopicCollection.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66

7+
using System.Collections.Generic;
8+
using System.Diagnostics.Contracts;
9+
710
namespace Ignia.Topics.Collections {
811

912
/*============================================================================================================================
@@ -21,7 +24,23 @@ public class ReadOnlyTopicCollection : ReadOnlyTopicCollection<Topic> {
2124
/// Establishes a new <see cref="ReadOnlyTopicCollection"/> based on an existing <see cref="TopicCollection"/>.
2225
/// </summary>
2326
/// <param name="innerCollection">The underlying <see cref="TopicCollection"/>.</param>
24-
public ReadOnlyTopicCollection(TopicCollection innerCollection) : base(innerCollection) {
27+
public ReadOnlyTopicCollection(IList<Topic> innerCollection) : base(innerCollection) {
28+
}
29+
30+
/*==========================================================================================================================
31+
| FACTORY METHOD: FROM LIST
32+
\-------------------------------------------------------------------------------------------------------------------------*/
33+
/// <summary>
34+
/// Establishes a new <see cref="ReadOnlyTopicCollection"/> based on an existing <see cref="List{T}"/>.
35+
/// </summary>
36+
/// <remarks>
37+
/// The <paramref name="innerCollection"/> will be converted to a <see cref="TopicCollection{T}"/>.
38+
/// </remarks>
39+
/// <param name="innerCollection">The underlying <see cref="TopicCollection{T}"/>.</param>
40+
public new static ReadOnlyTopicCollection FromList(IList<Topic> innerCollection) {
41+
Contract.Requires(innerCollection != null, "innerCollection should not be null");
42+
Contract.Ensures(Contract.Result<ReadOnlyTopicCollection>() != null);
43+
return new ReadOnlyTopicCollection(innerCollection);
2544
}
2645

2746
}

Ignia.Topics/Collections/ReadOnlyTopicCollection{T}.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ public class ReadOnlyTopicCollection<T> : ReadOnlyCollection<T> where T : Topic
1919
/*==========================================================================================================================
2020
| PRIVATE VARIABLES
2121
\-------------------------------------------------------------------------------------------------------------------------*/
22-
TopicCollection<T> _innerCollection;
22+
private readonly TopicCollection<T> _innerCollection;
2323

2424
/*==========================================================================================================================
2525
| CONSTRUCTOR
2626
\-------------------------------------------------------------------------------------------------------------------------*/
2727
/// <summary>
28-
/// Establishes a new <see cref="ReadOnlyTopicCollection{T}"/> based on an existing <see cref="TopicCollection{T}"/>.
28+
/// Establishes a new <see cref="ReadOnlyTopicCollection{T}"/> based on an existing <see cref="IList{T}"/>.
2929
/// </summary>
3030
/// <param name="innerCollection">The underlying <see cref="TopicCollection{T}"/>.</param>
31-
public ReadOnlyTopicCollection(TopicCollection<T> innerCollection) : base(innerCollection) {
31+
public ReadOnlyTopicCollection(IList<T> innerCollection) : base(innerCollection) {
3232
Contract.Requires(innerCollection != null, "innerCollection should not be null");
33-
_innerCollection = innerCollection;
33+
_innerCollection = innerCollection as TopicCollection<T>?? new TopicCollection<T>(innerCollection);
3434
}
3535

3636
/*==========================================================================================================================
@@ -57,11 +57,10 @@ public T GetTopic(string key) {
5757
/// The <paramref name="innerCollection"/> will be converted to a <see cref="TopicCollection{T}"/>.
5858
/// </remarks>
5959
/// <param name="innerCollection">The underlying <see cref="TopicCollection{T}"/>.</param>
60-
public static ReadOnlyTopicCollection<T> FromList(List<T> innerCollection) {
60+
public static ReadOnlyTopicCollection<T> FromList(IList<T> innerCollection) {
6161
Contract.Requires(innerCollection != null, "innerCollection should not be null");
6262
Contract.Ensures(Contract.Result<ReadOnlyTopicCollection<T>>() != null);
63-
var topicCollection = new TopicCollection<T>(innerCollection);
64-
return new ReadOnlyTopicCollection<T>(topicCollection);
63+
return new ReadOnlyTopicCollection<T>(innerCollection);
6564
}
6665

6766
/*==========================================================================================================================

0 commit comments

Comments
 (0)