001package jmri.jmrix.loconet.ds64;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * Provides a simple object to track a turnout number and the position of that
008 * turnout.
009 * <p>
010 * Turnout numbering is the same as seen on a Digitrax throttle display; Tools
011 * using values from objects of this type must provide the appropriate transform
012 * to create turnout numbering which is suitable for use within LocoNet messaging.
013 *
014 * @author B. Milhaupt Copyright (C) 2011, 2012, 2013, 2014, 2015
015 */
016public class SimpleTurnout {
017    private Integer address;
018    private boolean isClosed;
019    private boolean isUnused;
020
021    /**
022     * Constructor used if address is unknown.  The constructor creates an object
023     * which is marked as "unused".
024     */
025    SimpleTurnout() {
026        address = -1;
027        isClosed = false;
028        isUnused = true;
029    }
030
031    /**
032     * Constructor used when the turnout address and position are known.
033     * <p>
034     * Validity state is assumed to be "valid".
035     *
036     * @param addr turnout address
037     * @param closed true if turnout is closed, else false
038     */
039    public SimpleTurnout(Integer addr, boolean closed) {
040        address = addr;
041        isClosed = closed;
042        isUnused = false;
043    }
044
045    /**
046     * Constructor used when the turnout address, position, and validity state
047     * are known by the instantiating method.
048     *
049     * @param addr turnout address
050     * @param closed true if turnout is closed, else false
051     * @param unused trud if turnout is unused, else false
052     */
053    public SimpleTurnout(Integer addr, boolean closed, boolean unused) {
054        address = addr;
055        isClosed = closed;
056        isUnused = unused;
057    }
058
059    /**
060     * Get the "validity" state of the simpleTurnout object.  This "validity"
061     * state does not necessarily reflect the "validity" state of a physical turnout.
062     *
063     * @return true if the address is valid
064     */
065    public boolean isValid() {
066        if ((address >=1) && (address <=2048)) {
067            return true;
068        }
069        else
070            return false;
071    }
072
073    /**
074     * Sets the turnout address of the simpleTurnout object.
075     *
076     * @param addr  address value
077     */
078    public void setAddress(Integer addr) {
079        address = addr;
080        if (isValid() == false) {
081            log.debug("simpleTurnout says {} is invalid therefore unused", addr);
082            isUnused = true;
083            isClosed = true;
084        }
085        else {
086            isUnused = false;
087        }
088    }
089
090    /**
091     * Returns the address field of the simpleTurnout object.
092     *
093     * @return the address from the GUI element
094     */
095    public Integer getAddress() {return address;}
096
097    /**
098     * Sets an object field to show the current position of a simpleTurnout object.
099     * This position does not necessarily reflect the actual position of an associated
100     * physical turnout.
101     *
102     * @param isclosed  true if the object is to be marked as closed.
103     */
104    public void setIsClosed(boolean isclosed) {
105        isClosed = isclosed;
106        isUnused = false;
107    }
108
109    /**
110     * Returns position of the turnout as represented in an object field.  This
111     * position does not necessarily reflect the actual position of an associated
112     * physical turnout.
113     *
114     * @return true if turnout position is closed, else false
115     */
116    public boolean getIsClosed() {return isClosed;}
117
118    /**
119     * Returns the "used" state of the object, as represented in an object field.
120     * This does not necessarily reflect the actual "used" state of the physical
121     * device.
122     *
123     * @return true if turnout is "unused", else false
124     */
125    public boolean getIsUnused() {
126        log.debug("simple turnout isunused returns {}", isUnused);
127        return isUnused;
128    }
129
130    /**
131     * Define the turnout as "unused" within a field in the object.
132     */
133    public void setIsUnused() {
134        address = -1;
135        isClosed = true;
136        isUnused = true;
137    }
138
139    private final static Logger log = LoggerFactory.getLogger(SimpleTurnout.class);
140
141}