简体   繁体   English

我需要使用什么方法将生产者转换为生成者

[英]What method do I need to use to convert a Producer into a Generator

Assume I have a loop that produces items: 假设我有一个产生项目的循环:

for (int i = 0; true; i++) {
    ComplexObject co = new ComplexObject(i);
    System.out.println(co);
}

The standard behaviour would be to print a description about the objects, but that does not make that much sense for a real application. 标准行为是打印有关对象的描述,但这对于实际的应用程序没有多大意义。

I wish to write a custom iterator (generator) now via which I can obtain the elements. 我现在想编写一个自定义迭代器(生成器),通过它可以获取元素。

So I need to write the next() method in such a way that it will return one ComplexObject on every next() call, so it is like one step in a loop. 因此,我需要编写next()方法,使其在每次next()调用时都返回一个ComplexObject ,因此就像循环中的一个步骤。

How would I go about doing that? 我将如何去做? What general mechanism would you advise me to use? 您会建议我使用什么通用机制?

Keep in mind that I dumbed down the real issue to make it explainable, in reality it is of course not as easy as I have stated here and that is why I need to achieve the exact same mechanism that I have asked. 请记住,我愚弄了真实的问题以使其可解释,实际上,这当然不像我在这里所说的那么容易,这就是为什么我需要实现与我所要求的完全相同的机制。

Regards. 问候。

ps. ps。 (small rant) What is this bleep about only being allowed to post 6 questions per 24-hour period? (小毛病)每24小时只允许发布6个问题,这会带来什么麻烦

For the example you've given above, you'd need to save some state each time an element was retrieved in order to know what sort of state the next element should have. 对于上面给出的示例,每次获取元素时都需要保存一些状态,以便知道下一个元素应具有的状态。

Here's an example which does precisely that. 这是一个精确地做到这一点的例子。 I have not tested this, but it should clearly demonstrate what I'm suggesting. 我没有对此进行测试,但是它应该清楚地表明我的建议。

public class ComplexObjectIterator implements Iterator<ComplexObject>{
    //track start, so you can tell how many elements were retrieved if you want.
    private final int start;
    private final int end;
    private int position;

    public ComplexObjectIterator(int start, int end){
        if(start>end) throw new Exception("Start must be less "+
            "than or equal to end.");
        this.start = start;
        this.end = end;
        this.position = start;
    }

    public boolean hasNext(){
        return position < end;
    }

    public ComplexObject next(){
        if(position >= end) 
            throw new Exception("No more elements");
        ComplexObject obj = new Complexobject(this.position);
        position+=1;
        return obj;
    }

    public void remove(){
        if(position >= end)
            throw new Exception("No more elements");
        position+=1;
    }
}

If you truly want an endless iterator, this code can easily be changed to provide that kind of behavior. 如果您确实想要一个无限的迭代器,则可以轻松更改此代码以提供这种行为。

Note that iterators are typically used to return elements from an underlying data structure. 请注意,迭代器通常用于从基础数据结构返回元素。 Because here you have no underlying structure, I'd create a new interface called Generator for your purposes to prevent confusing anyone looking at your code. 因为这里没有底层结构,所以我将创建一个名为Generator的新接口,以防止混淆任何看您代码的人。

Far as the limit on questions, its probably there to prevent malicious users from attacking the site with an overload of questions. 作为问题的限制,它可以防止恶意用户以过多的问题攻击站点。 Not that you're doing that, just offering a possible explanation. 并不是说您正在这样做,只是提供了可能的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM