001package votorola.s.gwt.stage.light; // Copyright 2012, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import java.util.*;
004
005
006/** An implementation of a basic sensor.
007  */
008public class Sensor1 implements Sensor
009{
010
011
012    /** Responds to the addition of a light to the bank.
013      *
014      *     @see #removingLight(Light)
015      */
016    @SuppressWarnings("unchecked") final <S extends Sensor> void addingLight( final Light<S> light )
017    {
018        final S s = light.tryCast( Sensor1.this );
019        if( s == null ) return;
020
021        final Actuator<S> actuator = light.assignActuator( s );
022        if( actuator != null ) actuators.add( (Actuator<Sensor1>)actuator );
023    }
024
025
026
027    /** Responds to the removal of this sensor from the stage, and includes a call to
028      * {@linkplain #out() out}.
029      */
030    final void removed()
031    {
032        out();
033        actuators.clear();
034    }
035
036
037
038    /** Responds to the removal of a light from the bank.
039      *
040      *     @see #addingLight(Light)
041      */
042    final void removingLight( final Light<?> light )
043    {
044        final Iterator<Actuator<Sensor1>> a = actuators.iterator();
045        while( a.hasNext() )
046        {
047            final Actuator<Sensor1> actuator = a.next();
048            if( actuator.light() == light ) a.remove();
049        }
050    }
051
052
053
054   // - S e n s o r ----------------------------------------------------------------------
055
056
057    public final void changed()
058    {
059        for( Actuator<Sensor1> actuator: actuators ) actuator.changed( Sensor1.this );
060    }
061
062
063
064    public final void out()
065    {
066        for( Actuator<Sensor1> actuator: actuators ) actuator.out( Sensor1.this );
067    }
068
069
070
071    public final void over()
072    {
073        for( Actuator<Sensor1> actuator: actuators ) actuator.over( Sensor1.this );
074    }
075
076
077
078//// P r i v a t e ///////////////////////////////////////////////////////////////////////
079
080
081    private final LinkedList<Actuator<Sensor1>> actuators = new LinkedList<Actuator<Sensor1>>();
082
083
084}