All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.yield.ReferenceYieldAdapter Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.yield;

import java.util.ArrayList;
import java.util.Iterator;


/**
 * This reference adapter simply invokes the Collector<>, first gathering the results into a list.
 * It is provided to illustrate the simplicity of the function of the adapter, and to aid debugging
 * if threading issues are suspected as the cause of problems in the calling code.
 *
 * @author Jim Blackler ([email protected])
 */
public class ReferenceYieldAdapter implements YieldAdapter {

    /**
     * Convert a method that implements the Collector<> class with a standard Iterable<>, by
     * collecting the results in a list, and returning an iterator to that list.
     */
    public YieldAdapterIterable adapt(Collector client) {

        final ArrayList results = new ArrayList();

        try {
            client.collect(new ResultHandler() {
                public void handleResult(T value) {
                    results.add(value);
                }
            });
        } catch (CollectionAbortedException e) {
            // The process was aborted by calling code.
        }

        // Wrap container's iterator with yield adapter interface for compatibility
        return new YieldAdapterIterable() {
            public YieldAdapterIterator iterator() {
                final Iterator iterator = results.iterator();
                return new YieldAdapterIterator() {
                    public boolean hasNext() {
                        return iterator.hasNext();
                    }

                    public T next() {
                        return iterator.next();
                    }

                    public void remove() {
                        iterator.remove();
                    }

                    public void dispose() {
                        // Does nothing in this implementation
                    }
                };
            }
        };

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy