001package jmri.progdebugger;
002
003import java.util.HashMap;
004import jmri.AddressedProgrammer;
005import jmri.managers.DefaultProgrammerManager;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Provides an implementation of ProgrammerManager for the debug programmer. It
011 * will consistently return the same ProgDebugger instance for a given request.
012 * <p>
013 * It uses the DefaultProgrammerManager to handle the service mode operations.
014 *
015 * @see jmri.managers.DefaultProgrammerManager
016 * @author Bob Jacobsen Copyright (C) 2002
017 */
018public class DebugProgrammerManager extends DefaultProgrammerManager {
019
020    public DebugProgrammerManager() {
021        super(new ProgDebugger());
022    }
023
024    public DebugProgrammerManager(jmri.SystemConnectionMemo memo) {
025        super(new ProgDebugger(), memo);
026    }
027
028    /**
029     * Save the mapping from addresses to Programmer objects. Short addresses
030     * are saved as negative numbers.
031     */
032    HashMap<Integer, ProgDebugger> opsProgrammers = new HashMap<>();
033
034    @Override
035    public AddressedProgrammer getAddressedProgrammer(boolean pLongAddress, int pAddress) {
036        int address = pAddress;
037        if (!pLongAddress) {
038            address = -address;
039        }
040        // look for an existing entry by getting something from hash table
041        ProgDebugger saw = opsProgrammers.get(address);
042        if (saw != null) {
043            log.debug("return existing ops-mode programmer {} {}", pAddress, pLongAddress);
044            return saw;
045        }
046        // if not, save a new one & return it
047        opsProgrammers.put(address, saw = new ProgDebugger(pLongAddress, pAddress));
048        log.debug("return new ops-mode programmer {} {}", pAddress, pLongAddress);
049        return saw;
050    }
051
052    @Override
053    public AddressedProgrammer reserveAddressedProgrammer(boolean pLongAddress, int pAddress) {
054        return null;
055    }
056
057    /**
058     * Debug programmer does provide Ops Mode
059     *
060     * @return true
061     */
062    @Override
063    public boolean isAddressedModePossible() {
064        return true;
065    }
066
067    private final static Logger log = LoggerFactory.getLogger(DebugProgrammerManager.class);
068}