Skip to content

Commit

Permalink
Use IReadOnlyCollection over IEnumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Jul 4, 2016
2 parents 23befe7 + cc1bef9 commit 944cc78
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ private void ImplementRecursiveParentInterface()
// this.Children;
var thisDotChildren = Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name.ToPascalCase()));

// System.Collections.Generic.IEnumerable<IRecursiveType> IRecursiveParent.Children
// System.Collections.Generic.IReadOnlyCollection<IRecursiveType> IRecursiveParent.Children
this.innerMembers.Add(
SyntaxFactory.PropertyDeclaration(
Syntax.GetTypeSyntax(typeof(IEnumerable<IRecursiveType>)),
Syntax.GetTypeSyntax(typeof(IReadOnlyCollection<IRecursiveType>)),
nameof(IRecursiveParent.Children))
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveParent))))
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(thisDotChildren))
Expand Down Expand Up @@ -164,11 +164,11 @@ private void ImplementRecursiveParentInterface()
SyntaxFactory.Argument(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parentedVar, SyntaxFactory.IdentifierName(nameof(ParentedRecursiveTypeNonGeneric.Value)))),
SyntaxFactory.Argument(SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, parentedVar, SyntaxFactory.IdentifierName(nameof(ParentedRecursiveTypeNonGeneric.Parent)))))))));

////System.Collections.Generic.IEnumerable<<#= templateType.RecursiveType.TypeName #>> IRecursiveParent<<#= templateType.RecursiveType.TypeName #>>.Children
////System.Collections.Generic.IReadOnlyCollection<<#= templateType.RecursiveType.TypeName #>> IRecursiveParent<<#= templateType.RecursiveType.TypeName #>>.Children
//// => return this.Children;
this.innerMembers.Add(
SyntaxFactory.PropertyDeclaration(
Syntax.IEnumerableOf(this.generator.applyToMetaType.RecursiveType.TypeSyntax),
Syntax.IReadOnlyCollectionOf(this.generator.applyToMetaType.RecursiveType.TypeSyntax),
nameof(IRecursiveParent<IRecursiveType>.Children))
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(irecursiveParentOfT))
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(thisDotChildren))
Expand All @@ -185,6 +185,15 @@ private void ImplementOrderedChildrenInterface()
this.baseTypes.Add(SyntaxFactory.SimpleBaseType(Syntax.GetTypeSyntax(typeof(IRecursiveParentWithOrderedChildren))));
}

// IReadOnlyList<IRecursiveType> IRecursiveParentWithOrderedChildren.Children => this.children;
this.innerMembers.Add(SyntaxFactory.PropertyDeclaration(
Syntax.IReadOnlyListOf(Syntax.GetTypeSyntax(typeof(IRecursiveType))),
nameof(IRecursiveParentWithOrderedChildren.Children))
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(SyntaxFactory.IdentifierName(nameof(IRecursiveParentWithOrderedChildren))))
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(Syntax.ThisDot(SyntaxFactory.IdentifierName(this.generator.applyToMetaType.RecursiveField.Name))))
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
.AddAttributeLists(SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(DebuggerBrowsableNeverAttribute))));

// int IRecursiveParentWithOrderedChildren.IndexOf(IRecursiveType value)
var valueParameterName = SyntaxFactory.IdentifierName("value");
this.innerMembers.Add(SyntaxFactory.MethodDeclaration(
Expand Down
24 changes: 10 additions & 14 deletions src/ImmutableObjectGraph.Generation/CodeGen+RootedStructGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,21 +743,17 @@ protected MethodDeclarationSyntax CreateParentedNodeMethod()
protected PropertyDeclarationSyntax[] CreateChildrenProperties()
{
return new PropertyDeclarationSyntax[] {
// IEnumerable<IRecursiveType> IRecursiveParent.Children { get; }
SyntaxFactory.PropertyDeclaration(Syntax.IEnumerableOf(Syntax.GetTypeSyntax(typeof(IRecursiveType))), nameof(IRecursiveParent.Children))
// IReadOnlyCollection<IRecursiveType> IRecursiveParent.Children => this.greenNode.Children;
SyntaxFactory.PropertyDeclaration(Syntax.IReadOnlyCollectionOf(Syntax.GetTypeSyntax(typeof(IRecursiveType))), nameof(IRecursiveParent.Children))
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveParent))))
.AddAccessorListAccessors(SyntaxFactory.AccessorDeclaration(
SyntaxKind.GetAccessorDeclaration,
SyntaxFactory.Block(
// return System.Linq.Enumerable.Cast<IRecursiveType>(this.Children);
SyntaxFactory.ReturnStatement(
Syntax.EnumerableExtension(
SyntaxFactory.GenericName(nameof(Enumerable.Cast))
.AddTypeArgumentListArguments(Syntax.GetTypeSyntax(typeof(IRecursiveType))),
Syntax.ThisDot(this.applyTo.RecursiveParent.RecursiveField.NameAsProperty),
SyntaxFactory.ArgumentList()))))),
// IEnumerable<TRootedRecursiveType> IRecursiveParent<TRootedRecursiveType>.Children { get; }
SyntaxFactory.PropertyDeclaration(Syntax.IEnumerableOf(this.rootedRecursiveType), nameof(IRecursiveParent<IRecursiveType>.Children))
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
Syntax.ThisDot(GreenNodeFieldName),
this.applyTo.RecursiveParent.RecursiveField.NameAsProperty)))
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
// IReadOnlyCollection<TRootedRecursiveType> IRecursiveParent<TRootedRecursiveType>.Children { get; }
SyntaxFactory.PropertyDeclaration(Syntax.IReadOnlyCollectionOf(this.rootedRecursiveType), nameof(IRecursiveParent<IRecursiveType>.Children))
.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(CreateIRecursiveParentOfTSyntax(this.rootedRecursiveType)))
// => this.Children;
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(Syntax.ThisDot(this.applyTo.RecursiveParent.RecursiveField.NameAsProperty)))
Expand Down
10 changes: 6 additions & 4 deletions src/ImmutableObjectGraph/IRecursiveCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public interface IRecursiveType {
}

public interface IRecursiveParent : IRecursiveType {
IEnumerable<IRecursiveType> Children { get; }
IReadOnlyCollection<IRecursiveType> Children { get; }

ParentedRecursiveType<IRecursiveParent<IRecursiveType>, IRecursiveType> GetParentedNode(IdentityFieldType identity);
}

public interface IRecursiveParent<out TRecursiveType> : IRecursiveParent
where TRecursiveType : IRecursiveType {
new IEnumerable<TRecursiveType> Children { get; }
new IReadOnlyCollection<TRecursiveType> Children { get; }
}

public interface IRecursiveParentWithChildReplacement<TRecursiveType> : IRecursiveParent
Expand All @@ -30,11 +30,13 @@ public interface IRecursiveParentWithChildReplacement<TRecursiveType> : IRecursi
}

public interface IRecursiveParentWithOrderedChildren : IRecursiveParent {
int IndexOf(IRecursiveType value);
new IReadOnlyList<IRecursiveType> Children { get; }

int IndexOf(IRecursiveType value);
}

public interface IRecursiveParentWithSortedChildren : IRecursiveParentWithOrderedChildren {
int Compare(IRecursiveType first, IRecursiveType second);
int Compare(IRecursiveType first, IRecursiveType second);
}

public interface IRecursiveParentWithLookupTable<TRecursiveType> : IRecursiveParent<TRecursiveType>
Expand Down
4 changes: 2 additions & 2 deletions src/ImmutableObjectGraph/RecursiveTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ public static IEnumerable<TRecursiveType> GetSelfAndDescendentsBreadthFirst<TRec
var visiting = nodesToVisit.Dequeue();
yield return visiting;

var visitingAsParent = visiting as IRecursiveParent;
var visitingAsParent = visiting as IRecursiveParent<TRecursiveType>;
if (visitingAsParent != null && visitingAsParent.Children != null)
{
foreach (TRecursiveType child in visitingAsParent.Children)
foreach (var child in visitingAsParent.Children)
{
nodesToVisit.Enqueue(child);
}
Expand Down

0 comments on commit 944cc78

Please sign in to comment.