首页 > 解决方案 > 使用 next() 为可迭代查找下一个唯一元素

问题描述

我需要next()在我的包装类中重写迭代器的函数,这样每次我调用时next(),它都会遍历ScrollableResults,添加细节,并最终返回唯一的记录。

由于事情已经就位,除了详细名称和详细值之外,具有相同序列号的每条记录都应该完全相同。我需要将这些详细信息添加到同一记录中(就是这样record.addDetail(name, value)做的)并最终返回该唯一记录。以下是我的自定义需要发生的事情的细分next()

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

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

  3. 一旦返回,下一次调用next()将重新开始该过程,除了光标会从它停止的地方开始,previousSequence也应该从它离开的地方开始。

包装器.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;
        }
    }
}

我一直在尝试实现这一点,这就是我所得到的。我目前的方法可以遍历并详细说明,但我的问题是返回正确的行。由于当前序列需要与前一个序列进行检查,因此我无法返回新序列记录之前的最后一条记录,因为光标已经过了它。

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

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

标签: javaiteratornext

解决方案


推荐阅读