001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj.event;
006
007import java.util.Collection;
008import java.util.LinkedHashSet;
009
010/**
011 * A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
012 */
013public final class EventLoop {
014  private final Collection<Runnable> m_bindings = new LinkedHashSet<>();
015
016  /**
017   * Bind a new action to run when the loop is polled.
018   *
019   * @param action the action to run.
020   */
021  public void bind(Runnable action) {
022    m_bindings.add(action);
023  }
024
025  /** Poll all bindings. */
026  public void poll() {
027    m_bindings.forEach(Runnable::run);
028  }
029
030  /** Clear all bindings. */
031  public void clear() {
032    m_bindings.clear();
033  }
034}