Permalink
Browse files

make branches readonly

  • Loading branch information...
jediwhale
jediwhale committed Mar 17, 2009
1 parent 68af25b commit 3aa200c1b04588f12afab03b96ac54feefe04ad0
Showing with 22 additions and 8 deletions.
  1. +22 −8 source/mtee/Model/Tree.cs
View
@@ -4,6 +4,7 @@
// to be bound by the terms of this license. You must not remove this notice, or any other, from this software.
using System;
+using System.Collections;
using System.Collections.Generic;
namespace fitnesse.mtee.model {
@@ -13,11 +14,16 @@ public interface TreeWriter<T> {
void WriteSuffix(Tree<T> tree);
}
+ public interface ReadList<T>: IEnumerable<T> {
+ T this[int index] { get; }
+ int Count { get; }
+ }
+
public abstract class Tree<T> {
public abstract T Value { get; }
public abstract bool IsLeaf { get; }
- public abstract IList<Tree<T>> Branches { get; }
+ public abstract ReadList<Tree<T>> Branches { get; }
public IEnumerable<T> Leaves {
get {
@@ -39,7 +45,7 @@ public abstract class Tree<T> {
}
public class TreeList<T>: Tree<T> {
- private readonly List<Tree<T>> list = new List<Tree<T>>();
+ private readonly BranchList<T> list = new BranchList<T>();
private readonly T aValue;
public override T Value { get { return aValue; } }
@@ -49,26 +55,34 @@ public class TreeList<T>: Tree<T> {
public override bool IsLeaf { get { return false; } }
- public override IList<Tree<T>> Branches { get { return list; } }
+ public override ReadList<Tree<T>> Branches { get { return list; } }
public TreeList<T> AddBranchValue(object value) {
- Branches.Add(value as Tree<T> ?? new TreeLeaf<T>((T)value));
+ list.Add(value as Tree<T> ?? new TreeLeaf<T>((T)value));
return this;
}
public TreeList<T> AddBranch(Tree<T> value) {
- Branches.Add(value);
+ list.Add(value);
return this;
}
+
+ }
+
+ public class BranchList<T>: ReadList<Tree<T>> {
+ private readonly List<Tree<T>> branches = new List<Tree<T>>();
+ public void Add(Tree<T> item) { branches.Add(item); }
+ public IEnumerator<Tree<T>> GetEnumerator() { return branches.GetEnumerator(); }
+ IEnumerator IEnumerable.GetEnumerator() { return branches.GetEnumerator(); }
+ public Tree<T> this[int index] { get { return branches[index]; } }
+ public int Count { get { return branches.Count; } }
}
public class TreeLeaf<T>: Tree<T> {
private readonly T aValue;
public override T Value { get { return aValue; } }
public TreeLeaf(T value) { aValue = value; }
-
public override bool IsLeaf { get { return true; } }
-
- public override IList<Tree<T>> Branches { get { throw new InvalidOperationException(); } }
+ public override ReadList<Tree<T>> Branches { get { throw new InvalidOperationException(); } }
}
}

0 comments on commit 3aa200c

Please sign in to comment.