On Sunday 11 April 2004 17:46, Erik Hatcher wrote:
> In other words, you need to invent your own "pattern" here?!  :)

I just experimented a bit and came up with the ValueListSupplier which 
replaces the ValueList in the VLH. Seems to work so far... :-) Comments are 
greatly appreciated!

Timo

public class ValueListSupplier implements IValueListIterator
{
        private final Log log = LogFactory.getLog(this.getClass());

        // TODO junit test case
        private Hits hits;
        protected BitSet fetched;
        protected List list;
        protected int index;
        
        public ValueListSupplier(Hits hits)
        {
                int size = hits.length();
                this.list = new ArrayList(size);
                // stupid idiots at SUN
                for (int i = 0; i < size; i++) list.add(null);
                this.fetched = new BitSet();
                this.hits = hits;
                this.index = 0;
        }

        public List getList()
        {
                return list;
        }

        public int size()
        {
                return list.size();
        }

        public boolean hasPrevious()
        {
                return index > 0;
        }

        public boolean hasNext()
        {
                return index < size();
        }

        /**
         * @param index
         */
        public synchronized void move(int index)
        {
                this.index = index;
        }

        public void reset()
        {
                move(0);
        }

        public Object current()
        {
                validate(index, index + 1);
                return list.get(index);
        }

        public List previous(int count)
        {
                int from = Math.max(0, index - count);
                int to = index;

                validate(from, to);
                move(from);
                return list.subList(from, to);
        }

        public List next(int count)
        {
                int from = index;
                int to = Math.min(Math.max(0, size() - 1), index + count);

                validate(from, to);
                move(to);
                return list.subList(from, to);
        }

        /**
         * @param from
         *                 starting index (inclusive)
         * @param to
         *                 ending index (exclusive)
         */
        private void validate(int from, int to)
        {
                while ((from = fetched.nextClearBit(from)) < to)
                {
                        log.debug("fetching #" + from);

                        try
                        {
                                list.set(from, 
SearchResultAdapter.wrap(hits.doc(from)));
                                fetched.set(from);
                        }
                        catch (IOException e)
                        {
                                // TODO potentially bug
                                e.printStackTrace();
                        }
                }
        }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to