Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add better iterator over java iterable
- Loading branch information
1 parent
5b9d471
commit 1ebd04b
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |