import lejos.nxt.*;

/**
 * An average filtered color sensor. This is implemented as thin wrapper around the ColorSensor class. This class is meant for random sampling.
 * <p/>
 * THREAD SAFE
 */
public class FilteredColorSensor {
    /**
     * Constructs a new filtered color sensor from the port to which it is connected.
     *
     * @param port The sensor port
     */
    public FilteredColorSensor(SensorPort port) {
    }

    /**
     * Sets the number of samples to use for filtering.
     *
     * @param sampleCount The number of samples to use for filtering
     */
    public void setSampleCount(int sampleCount) {
    }

    /**
     * Applies the filtering and returns the color data, a value of format 0xRRGGBB where R are the red component bits, G the green ones and B the blue ones. Performs the specified number of samples
     * per call.
     *
     * @return The color data
     */
    public int getColorData() {
        return 0;
    }
}

@@ -224,24 +224,101 @@
 ensor %7B%0A
+    private final ColorSensor handle;%0A    private volatile int sampleCount;%0A%0A
     /**%0A
      * C
@@ -305,24 +305,24 @@
 t;%0A%0A    /**%0A
-
      * Const
@@ -491,24 +491,91 @@
 ort port) %7B%0A
+        handle = new ColorSensor(port);%0A        setSampleCount(5);%0A
     %7D%0A%0A    /
@@ -763,24 +763,64 @@
 pleCount) %7B%0A
+        this.sampleCount = sampleCount;%0A
     %7D%0A%0A    /
@@ -1127,16 +1127,772 @@
     
-return 0
+// Thread safety precautions, make a local copy of sample count%0A        final int count = sampleCount;%0A        // No samples means no color%0A        if (count == 0) %7B%0A            return 0;%0A        %7D%0A        // Sample the sensoe and average the results per component%0A        int redSum = 0, greenSum = 0, blueSum = 0;%0A        for (int i = 0; i %3C count; i++) %7B%0A            final ColorSensor.Color sample = handle.getColor();%0A            redSum += sample.getRed();%0A            greenSum += sample.getGreen();%0A            blueSum += sample.getBlue();%0A        %7D%0A        redSum /= count;%0A        greenSum /= count;%0A        blueSum /= count;%0A        // Assemble components as 0RGB 32-bit color int%0A        return (redSum & 255) %3C%3C 16 %7C (greenSum & 255) %3C%3C 8 %7C blueSum & 255
 ;%0A  

Implement FilteredColorSensor