Skip to content

Commit 4c15216

Browse files
author
Kapil Borle
committed
Make ReadOnly method return a ReadOnlyCollection type
1 parent 22015c9 commit 4c15216

File tree

3 files changed

+8
-26
lines changed

3 files changed

+8
-26
lines changed

Engine/EditableText.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
45
using System.Globalization;
56
using System.Linq;
67
using System.Management.Automation.Language;
@@ -29,7 +30,7 @@ public class EditableText
2930
/// <summary>
3031
/// The lines in the Text.
3132
/// </summary>
32-
public IList<string> Lines => lines.ReadOnly();
33+
public ReadOnlyCollection<string> Lines => lines.ReadOnly();
3334

3435
/// <summary>
3536
/// The new line character in the Text.

Engine/Strings.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,6 @@
294294
<data name="TextLinesNoNullItem" xml:space="preserve">
295295
<value>Line element cannot be null.</value>
296296
</data>
297-
<data name="TextLinesReadOnlyCollection" xml:space="preserve">
298-
<value>Collection is read-only.</value>
299-
</data>
300297
<data name="TextEditNoNullItem" xml:space="preserve">
301298
<value>Line element cannot be null.</value>
302299
</data>

Engine/TextLines.cs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Collections.ObjectModel;
45
using System.Globalization;
56
using System.Linq;
67

@@ -30,7 +31,6 @@ public TextLines()
3031
{
3132
lines = new LinkedList<string>();
3233
Count = 0;
33-
IsReadOnly = false;
3434
InvalidateLastAccessed();
3535
}
3636

@@ -61,7 +61,7 @@ public TextLines(IEnumerable<string> inputLines) : this()
6161
/// <summary>
6262
/// If the object is ReadOnly or not.
6363
/// </summary>
64-
public bool IsReadOnly { get; private set; }
64+
public bool IsReadOnly => false;
6565

6666
/// <summary>
6767
/// Sets or gets the element at the given index.
@@ -82,16 +82,12 @@ public string this[int index]
8282
}
8383

8484
/// <summary>
85-
/// Creates a readonly shallow copy.
85+
/// Return a readonly collection of the current object.
8686
/// </summary>
87-
/// <returns>A readonly shallow copy of the current object.</returns>
88-
public IList<string> ReadOnly()
87+
/// <returns>A readonly collection of the current object.</returns>
88+
public ReadOnlyCollection<string> ReadOnly()
8989
{
90-
var ret = new TextLines();
91-
ret.IsReadOnly = true;
92-
ret.Count = this.Count;
93-
ret.lines = this.lines;
94-
return ret;
90+
return new ReadOnlyCollection<string>(this);
9591
}
9692

9793
/// <summary>
@@ -108,7 +104,6 @@ public void Add(string item)
108104
/// </summary>
109105
public void Clear()
110106
{
111-
ValidateReadOnly();
112107
lines.Clear();
113108
}
114109

@@ -169,7 +164,6 @@ public int IndexOf(string item)
169164
/// </summary>
170165
public void Insert(int index, string item)
171166
{
172-
ValidateReadOnly();
173167
ThrowIfNull(item, nameof(item));
174168
LinkedListNode<string> itemInserted;
175169
if (Count == 0 && index == 0)
@@ -196,7 +190,6 @@ public void Insert(int index, string item)
196190
/// <returns>true if removal is successful, otherwise false.</returns>
197191
public bool Remove(string item)
198192
{
199-
ValidateReadOnly();
200193
var itemIndex = IndexOf(item);
201194
if (itemIndex == -1)
202195
{
@@ -212,7 +205,6 @@ public bool Remove(string item)
212205
/// </summary>
213206
public void RemoveAt(int index)
214207
{
215-
ValidateReadOnly();
216208
ValidateIndex(index);
217209
var node = GetNodeAt(index);
218210
if (node.Next != null)
@@ -358,13 +350,5 @@ private static void ThrowIfNull<T>(T param, string paramName)
358350
throw new ArgumentNullException(paramName);
359351
}
360352
}
361-
362-
private void ValidateReadOnly()
363-
{
364-
if (IsReadOnly)
365-
{
366-
throw new NotSupportedException(Strings.TextLinesReadOnlyCollection);
367-
}
368-
}
369353
}
370354
}

0 commit comments

Comments
 (0)