naveed naveed - 10 days ago 5
C# Question

converting some code snippet from c# to java

I am converting some code from c# to java. I am unable to figure out how to do following conversion

struct Point : IEnumerable<Point>{
public int X;
public int Y;

IEnumerator<Point> IEnumerable<Point>.GetEnumerator()
{
Point point = new Point();
for (point.Y = 0; point.Y < Y; ++point.Y)
for (point.X = 0; point.X < X; ++point.X)
yield return point;
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<Point>)this).GetEnumerator();
}}


here is my conversion:

public class Point implements Iterable<Point>{
int X;
int Y;

/*
* IEnumerator IEnumerable.GetEnumerator()
* {
* return ((IEnumerable<Point>)this).GetEnumerator();
* }
*/
@Override
public Iterator<Point> iterator() {
return ((Iterable<Point>)this).iterator();
}}


what will be the equivalent of 2nd function
IEnumerable<Point>.GetEnumerator()

Answer

It seems a little bit strange that the Point itself will implement and Iterable on a Point type. One instance of a Point (a particular Point object should usually not know much about other points...), you might rather have some kind of collection doing that.But assuming that you want to do just that...

You can use guava's PeekingIterator. If you are working with maven, the following dependency will neatly bring it to you, otherwise you need the jar.

 <!-- https://mvnrepository.com/artifact/com.google.guava/guava-jdk5 -->
 <dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava-jdk5</artifactId>
    <version>17.0</version>
 </dependency>

Once you have it, it is not very complicated to use Here is an illustration (and I am only implementing the part related to your second function):

public class Point implements Iterable<Point> {

    // Additional code here
    private Iterator<Point> pointIterator;

    public Point (Iterator<Point> pointIterator) {
        this.pointIterator = pointIterator;
    }
    @Override
    public Iterator<Point> iterator() {
        return pointIterator;
    }


    PeekingIterator<Point> getEnumerator() {
        if (pointIterator instanceof PeekingIterator) {
            return (PeekingIterator<Point>)pointIterator;
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        Point myPoint = new Point(new PeekingIteratorMock());
        PeekingIterator<Point> myIterator = myPoint.getEnumerator();
        Point p;
        while ((p = myIterator.next()) != null) {
            // Do something usefull here
        }
    }

}

The PeekingIterator is an interface so you need to pass it a concrete implementation if you want to have a practical test case. I lhave left it unimplemented for you to complete.

public class PeekingIteratorMock implements PeekingIterator<Point> {

    @Override
    public boolean hasNext() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Point next() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Point peek() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void remove() {
        // TODO Auto-generated method stub

    }

}
Comments