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