使用next()查找下一个唯一元素进行迭代

时间:2018-07-28 09:38:28

标签: java iterator next

我需要在包装器类中重写迭代器的next()函数,以便每次我调用next()时,它将遍历ScrollableResults,添加详细信息并最终返回唯一记录。

由于事物的放置方式,具有相同序号的每个记录的详细名称和详细值都应完全相同。 。我需要将这些详细信息添加到同一记录中(record.addDetail(name, value)就是这样做的),并最终返回该唯一记录。以下是我的自定义next()所需要执行的操作的详细信息:

  1. 遍历scrollableResults,将详细信息名称和详细信息值添加到唯一记录(它可以添加到任一唯一记录中)。

  2. 一旦光标遇到不同的序列号,它就会跳出循环并返回唯一的记录/对象。

  3. 返回后,对next()的下一次调用将重新开始该过程,除了光标将从其中断处拾起而previousSequence也应从中断处拾起。

Wrapper.java:

public class Wrapper<T> implements Iterable<T>, AutoCloseable {

    private ScrollableResults scrollableResults;
    private Session session;

    public Wrapper(ScrollableResults scrollableResults, Session session) {
        this.scrollableResults = scrollableResults;
        this.session = session;
    }

    public Iterator<T> iterator() { return new WrapperIterator(); }

    @Override
    public void close() {
        scrollableResults.close();
    }

    public class WrapperIterator implements Iterator<T> {

        private Integer previousSequence;

        @Override
        public boolean hasNext() {
            boolean hasNext = scrollableResults.next();
            return hasNext;
        }

        /**
         * This next() method should return the next UNIQUE record.
         * But before returning the unique record, it should add all the appropriate details to the respective record.
         */
        @Override
        public T next() {
            boolean hasNext = scrollableResults.next();
            if (!hasNext) {
                throw new NoSuchElementException();
            }
            // For previousSequence, first result needs to be retrieved explicitly
            Object[] row = (Object[]) scrollableResults.get();
            Record record = (Record) row[0];
            Integer currentSequence = previousSequence = record.getSequence();
            String detailName = (String) row[1];
            String detailValue = (String) row[2];
            record = setDetails(record, detailName, detailValue);

            Object[] mainRow = row;

            // First one should always be unique since it's starting a new set of records of same sequence.
            while(scrollableResults.next()) {
                 // Get record
                row = (Object[]) scrollableResults.get();
                record = (Record) row[0];
                // Update current sequence
                currentSequence = record.getSequence();
                // If current sequence is different from previous sequence, break
                // First result needs to be accounted for
                if(!currentSequence.equals(previousSequence)) {
                    previousSequence = currentSequence;
                    session.evict(row);
                    return (T) row;
                }
                // Add detail
                detailName = (String) row[1];
                detailValue = (String) row[2];
                record = setDetails(record, detailName, detailValue);
            }
            previousSequence = currentSequence;
            return (T) mainRow;
        }

        private Record setDetails(Record record, String detailName, String detailValue) {
            record.addDetail(detailName, detailValue);
            return record;
        }
    }
}

我一直在尝试实现这一点,这是我所知道的。我当前的方法可以遍历所有细节,但我的问题是返回正确的行。由于当前序列需要与先前的序列进行检查,因此我无法返回新序列记录之前的最后一条记录,因为光标已经经过它。

关于如何解决此问题的任何建议?任何事情将不胜感激!

注意: 迭代器不能向后移动。由于滚动模式的PSQL错误需要为FORWARD_ONLY,因此滚动模式已设置为FORWARD_ONLY。

0 个答案:

没有答案
相关问题