001package jmri.jmrit.logixng.actions;
002
003import java.util.Locale;
004import java.util.Map;
005import java.util.concurrent.atomic.AtomicBoolean;
006import java.util.concurrent.atomic.AtomicLong;
007
008import jmri.InstanceManager;
009import jmri.jmrit.logixng.*;
010
011/**
012 * This action sets the value of an AtomicBoolean. It is mostly used for tests.
013 * 
014 * @author Daniel Bergqvist Copyright 2018
015 */
016public class ActionAtomicBoolean extends AbstractDigitalAction {
017
018    private AtomicBoolean _atomicBoolean;
019    private boolean _newValue;
020    private AtomicLong _counter;
021    
022    public ActionAtomicBoolean(AtomicBoolean atomicBoolean, boolean newValue)
023            throws BadUserNameException {
024        super(InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName(), null);
025        _atomicBoolean = atomicBoolean;
026        _newValue = newValue;
027    }
028    
029    public ActionAtomicBoolean(AtomicBoolean atomicBoolean, boolean newValue, AtomicLong counter)
030            throws BadUserNameException {
031        super(InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName(), null);
032        _atomicBoolean = atomicBoolean;
033        _newValue = newValue;
034        _counter = counter;
035    }
036
037    public ActionAtomicBoolean(String sys, String user)
038            throws BadUserNameException, BadSystemNameException {
039        super(sys, user);
040        _atomicBoolean = new AtomicBoolean();
041    }
042    
043    public ActionAtomicBoolean(String sys, String user, AtomicBoolean atomicBoolean, boolean newValue)
044            throws BadUserNameException, BadSystemNameException {
045        super(sys, user);
046        _atomicBoolean = atomicBoolean;
047        _newValue = newValue;
048    }
049    
050    @Override
051    public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) {
052        DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class);
053        String sysName = systemNames.get(getSystemName());
054        String userName = userNames.get(getSystemName());
055        if (sysName == null) sysName = manager.getAutoSystemName();
056        DigitalActionBean copy = new ActionAtomicBoolean(sysName, userName, new AtomicBoolean(), _newValue);
057        copy.setComment(getComment());
058        return manager.registerAction(copy);
059    }
060    
061    public void setAtomicBoolean(AtomicBoolean atomicBoolean) {
062        _atomicBoolean = atomicBoolean;
063    }
064    
065    public AtomicBoolean getAtomicBoolean() {
066        return _atomicBoolean;
067    }
068    
069    public void setNewValue(boolean newValue) {
070        _newValue = newValue;
071    }
072    
073    public boolean getNewValue() {
074        return _newValue;
075    }
076    
077    /** {@inheritDoc} */
078    @Override
079    public Category getCategory() {
080        return Category.OTHER;
081    }
082
083    /** {@inheritDoc} */
084    @Override
085    public void execute() {
086        _atomicBoolean.set(_newValue);
087        if (_counter != null) _counter.addAndGet(1);
088    }
089
090    @Override
091    public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException {
092        throw new UnsupportedOperationException("Not supported.");
093    }
094
095    @Override
096    public int getChildCount() {
097        return 0;
098    }
099
100    @Override
101    public String getShortDescription(Locale locale) {
102        return Bundle.getMessage(locale, "ActionAtomicBoolean_Short");
103    }
104
105    @Override
106    public String getLongDescription(Locale locale) {
107        return Bundle.getMessage(locale, "ActionAtomicBoolean_Long", _newValue);
108    }
109    
110    /** {@inheritDoc} */
111    @Override
112    public void setup() {
113    }
114    
115    /** {@inheritDoc} */
116    @Override
117    public void registerListenersForThisClass() {
118    }
119    
120    /** {@inheritDoc} */
121    @Override
122    public void unregisterListenersForThisClass() {
123    }
124    
125    /** {@inheritDoc} */
126    @Override
127    public void disposeMe() {
128    }
129
130}