Skip to content

Commit

Permalink
Add better iterator over java iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
AliveDevil authored and pull[bot] committed Jan 25, 2023
1 parent 5b9d471 commit 1ebd04b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
37 changes: 37 additions & 0 deletions core/src/main/csharp/java/lang/IterableExtensions.cs
@@ -0,0 +1,37 @@
using java.util;
using System;
using System.Collections;

namespace java.lang
{
public static class IterableExtensions
{
public static IterableEnumerator GetEnumerator(this Iterable iterable) => new(iterable);

public struct IterableEnumerator : IEnumerator
{
private object current;
private Iterator iterator;

public IterableEnumerator(Iterable list)
{
iterator = list.iterator();
current = default;
}

public object Current => current;

public bool MoveNext()
{
if (iterator.hasNext())
{
current = iterator.next();
return true;
}
return false;
}

public void Reset() => throw new NotImplementedException();
}
}
}
9 changes: 9 additions & 0 deletions core/src/main/csharp/java/util/CollectionExtensions.cs
@@ -0,0 +1,9 @@
using java.lang;

namespace java.util
{
public static class CollectionExtensions
{
public static IterableExtensions.IterableEnumerator GetEnumerator(this Collection collection) => ((Iterable)collection).GetEnumerator();
}
}

0 comments on commit 1ebd04b

Please sign in to comment.