001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2014. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007package edu.wpi.first.wpilibj;
008
009import edu.wpi.first.wpilibj.hal.InterruptJNI.InterruptJNIHandlerFunction;
010
011
012/**
013 * It is recommended that you use this class in conjunction with classes from 
014 * {@link java.util.concurrent.atomic} as these objects are all thread safe.
015 * 
016 * @author Jonathan Leitschuh
017 *
018 * @param <T> The type of the parameter that should be returned to the the
019 *  method {@link #interruptFired(int, Object)}
020 */
021public abstract class InterruptHandlerFunction<T>{
022        /**
023         * The entry point for the interrupt. When the interrupt fires the
024         * {@link #apply(int, Object)} method is called.
025         * The outer class is provided as an interface to allow the implementer to
026         * pass a generic object to the interrupt fired method.
027         * @author Jonathan Leitschuh
028         */
029        private class Function implements InterruptJNIHandlerFunction{
030                @SuppressWarnings("unchecked")
031                @Override
032                public void apply(int interruptAssertedMask, Object param) {
033                        interruptFired(interruptAssertedMask, (T)param);
034                }
035        }
036        final Function function = new Function();
037        
038        /**
039         * This method is run every time an interrupt is fired.
040         * @param interruptAssertedMask
041         * @param param The parameter provided by overriding the {@link #overridableParamater()}
042         * method. 
043         */
044        abstract void interruptFired(int interruptAssertedMask, T param);
045        
046        
047        /**
048         * Override this method if you would like to pass a specific
049         * parameter to the {@link #interruptFired(int, Object)} when it is fired by the interrupt.
050         * This method is called once when {@link InterruptableSensorBase#requestInterrupts(InterruptHandlerFunction)}
051         * is run.
052         * @return The object that should be passed to the interrupt when it runs
053         */
054        public T overridableParamater(){
055                return null;
056        }
057}