Output.java

  1. /*
  2.  * Copyright © 2016 Greg Chabala
  3.  *
  4.  * This file is part of brick-control-lab.
  5.  *
  6.  * brick-control-lab is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU Lesser General Public License as
  8.  * published by the Free Software Foundation, either version 3 of the
  9.  * License, or (at your option) any later version.
  10.  *
  11.  * brick-control-lab is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public License
  17.  * along with brick-control-lab.  If not, see http://www.gnu.org/licenses/.
  18.  */
  19. package org.chabala.brick.controllab;

  20. import java.io.IOException;
  21. import java.util.Collections;
  22. import java.util.EnumSet;
  23. import java.util.Set;

  24. /**
  25.  * Handle for an output port (or group of ports) on a specific control lab instance.
  26.  * Obtain via {@link ControlLab#getOutput(OutputId)} or {@link ControlLab#getOutput(Set)}.
  27.  */
  28. public class Output {

  29.     private final ControlLab controlLab;
  30.     private final Set<OutputId> outputIdSet;

  31.     /**
  32.      * Called internally by {@link ControlLab#getOutput(OutputId)}.
  33.      * @param controlLab the control lab this output is a handle to
  34.      * @param outputId the output port ID on that control lab
  35.      */
  36.     Output(ControlLab controlLab, OutputId outputId) {
  37.         this(controlLab, EnumSet.of(outputId));
  38.     }

  39.     /**
  40.      * Called internally by {@link ControlLab#getOutput(Set)}.
  41.      * @param controlLab the control lab this output is a handle to
  42.      * @param outputIdSet the output IDs this output should reference
  43.      */
  44.     Output(ControlLab controlLab, Set<OutputId> outputIdSet) {
  45.         this.controlLab = controlLab;
  46.         this.outputIdSet = outputIdSet;
  47.     }

  48.     /**
  49.      * Returns set of {@link OutputId}s this {@link Output} relates to. In normal
  50.      * usage, this is unlikely to be called as one would obtain the {@link Output}
  51.      * from {@link ControlLab#getOutput(OutputId)} and immediately chain one of the
  52.      * fluent method calls. But it can useful to inspect the {@link OutputId}s if the
  53.      * {@link Output} reference is retained and seperated from the creation site.
  54.      * @return set of {@link OutputId}s this {@link Output} relates to
  55.      */
  56.     public Set<OutputId> getOutputIdSet() {
  57.         return Collections.unmodifiableSet(outputIdSet);
  58.     }

  59.     /**
  60.      * Stops sending power to this output.
  61.      * @return self reference to the output for chaining
  62.      * @throws IOException if any number of possible communication issues occurs
  63.      */
  64.     public Output turnOff() throws IOException {
  65.         controlLab.turnOutputOff(outputIdSet);
  66.         return this;
  67.     }

  68.     /**
  69.      * Starts sending power to this output.
  70.      * @return self reference to the output for chaining
  71.      * @throws IOException if any number of possible communication issues occurs
  72.      */
  73.     public Output turnOn() throws IOException {
  74.         controlLab.turnOutputOn(outputIdSet);
  75.         return this;
  76.     }

  77.     /**
  78.      * Sets the {@link Direction} of this output. Direction may be changed
  79.      * while the output is powered or unpowered.
  80.      * @param direction desired direction
  81.      * @return self reference to the output for chaining
  82.      * @throws IOException if any number of possible communication issues occurs
  83.      */
  84.     public Output setDirection(Direction direction) throws IOException {
  85.         controlLab.setOutputDirection(direction, outputIdSet);
  86.         return this;
  87.     }

  88.     /**
  89.      * Reverses the {@link Direction} of this output. This is a convenience method
  90.      * that is the same as
  91.      * {@link Output#setDirection(Direction) setDirection(}{@link Direction#REVERSE Direction.REVERSE)}.
  92.      * @return self reference to the output for chaining
  93.      * @throws IOException if any number of possible communication issues occurs
  94.      */
  95.     public Output reverseDirection() throws IOException {
  96.         return setDirection(Direction.REVERSE);
  97.     }

  98.     /**
  99.      * Sets the {@link PowerLevel} of this output. Power level may be changed
  100.      * while the output is powered or unpowered.
  101.      * @param powerLevel desired power level
  102.      * @return self reference to the output for chaining
  103.      * @throws IOException if any number of possible communication issues occurs
  104.      */
  105.     public Output setPowerLevel(PowerLevel powerLevel) throws IOException {
  106.         controlLab.setOutputPowerLevel(powerLevel, outputIdSet);
  107.         return this;
  108.     }

  109.     /** {@inheritDoc} */
  110.     @Override
  111.     public String toString() {
  112.         return "Output{" +
  113.                 "controlLab=" + controlLab +
  114.                 ", outputIdSet=" + outputIdSet +
  115.                 '}';
  116.     }
  117. }