import java.util.*;
import java.io.PrintWriter;

    // Collect an arbitrary number of double values.
    // Provide access to individual items and the mean value,
    // for instance.
    // Allow selection of values from the collection on different criteria.
public class DataSelection {
        // Add a value to the collection.
    public void add(double d){
        getNumbers().add(new Double(d));
        
    }

        // Get a value from the collection.
    public double get(int index) throws IndexOutOfBoundsException {
        Double d = (Double) getNumbers().get(index);
        return d.doubleValue();
    }

        // Calculate the mean of the values in the collection.
    public double mean(){
        final int howMany = numItems();
        if(howMany > 0){
            double sum = 0.0;
            for(int i = 0; i < howMany; i++){
                sum += get(i);
            }
            return sum / howMany;
        }
        else{
            return 0.0;
        }
    }

        // Print the collection.
    public void print(PrintWriter writer){
        final int howMany = numItems();
        for(int i = 0; i < howMany; i++){
            writer.print(get(i)+" ");
        }
        writer.println();
        writer.flush();
    }

        // Return how many items in the collection.
    public int numItems(){
        return getNumbers().size();
    }
    
        // Obtain an iterator over the values above limit in the collection.
    public Iterator selectAbove(final double limit){
        return new Selector(){
            // Provide the selection criteria.
            public boolean select(double d){
                return d > limit;
            }
        };
    }

        // Obtain an iterator over the values below limit in the collection.
    public Iterator selectBelow(final double limit){
        return new Selector(){
            // Provide the selection criteria.
            public boolean select(double d){
                return d < limit;
            }
        };
    }

            // Obtain an iterator over the values in the range
        // [lower .. upper] in the collection.
    public Iterator selectRange(final double lower, final double upper){
        return new Selector(){
            // Provide the selection criteria.
            public boolean select(double d){
                return (d >= lower) && (d <= upper);
            }
        };
    }

        // A local inner super class to allow selection of
        // values in the collection.
    protected class Selector implements Iterator {
        public boolean hasNext(){
            return nextPosition() >= 0;
        }

            // Return the next positive data item, if there is one.
        public Object next() throws NoSuchElementException {
            // Find out where the next (if any) occurs.
            int i = nextPosition();
            if(i >= 0){
                Object o = getNumbers().get(i);
                // Next time, start looking from the following position.
                setIndex(i+1);
                return o;
            }
            else{
                throw new NoSuchElementException();
            }
        }

            // Disallow removal.
        public void remove() throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

            // Decide whether to select this particular value.
            // This method would normally be overridden in
            // an inner sub class.
        public boolean select(double d){
            return true;
        }

            // Return the index of the next positive number in the collection.
            // If there is none, return NOITEM.
        protected int nextPosition(){
            int i = getIndex();
            final int howMany = numItems();
            while(i < howMany){
                // Test against the selection criteria.
                if(select(get(i))){
                    return i;
                }
                i++;
            }
            return NOITEM;
        }

        protected int getIndex(){
            return index;
        }

        protected void setIndex(int i){
            index = i;
        }

        // The index of the next item to be examined.
        private int index = 0;
        // An illegal index to indicate no item is available.
        private final int NOITEM = -1;
    }
    
    protected LinkedList getNumbers(){
        return numbers;
    }
    
    // A collection of Double objects.
    private final LinkedList numbers = new LinkedList();
}
