001package jmri.implementation;
002
003import jmri.Audio;
004import jmri.InstanceManager;
005
006/**
007 * Base implementation of the Audio class.
008 * <p>
009 * Specific implementations will extend this base class.
010 *
011 * @author Matthew Harris copyright (c) 2009
012 */
013public abstract class AbstractAudio extends AbstractNamedBean implements Audio {
014
015    private int _state = STATE_INITIAL;
016
017    private static final int INT_PRECISION = (int) Math.pow(10, DECIMAL_PLACES);
018
019    /**
020     * Abstract constructor for new Audio with system name
021     *
022     * @param systemName Audio object system name (e.g. IAS1, IAB4)
023     */
024    public AbstractAudio(String systemName) {
025        super(systemName);
026    }
027
028    /**
029     * Abstract constructor for new Audio with system name and user name
030     *
031     * @param systemName Audio object system name (e.g. IAS1, IAB4)
032     * @param userName   Audio object user name
033     */
034    public AbstractAudio(String systemName, String userName) {
035        super(systemName, userName);
036    }
037
038    @Override
039    public int getState() {
040        return this._state;
041    }
042
043    @Override
044    public void setState(int newState) {
045        Object _old = this._state;
046        this._state = newState;
047        stateChanged((Integer) _old);
048        firePropertyChange("State", _old, _state); // NOI18N
049    }
050
051    /**
052     * Abstract method that concrete classes will implement to perform necessary
053     * cleanup routines.
054     * <p>
055     * This method is now included in dispose(). The caller can
056     * call dispose() to cleanup and deregister an audio object.
057     */
058    abstract protected void cleanup();
059
060    @Override
061    public void dispose() {
062        InstanceManager.getDefault(jmri.AudioManager.class).deregister(this);
063        cleanup();
064        super.dispose();
065    }
066
067    /**
068     * Static method to round a float value to the specified number of decimal
069     * places
070     *
071     * @param value  float value to round
072     * @param places number of decimal places to round to
073     * @return float value rounded to specified number of decimal places
074     */
075    public static float roundDecimal(float value, double places) {
076        double multiplier = Math.pow(10, places);
077        value *= multiplier;
078        return (float) (Math.round(value) / multiplier);
079    }
080
081    /**
082     * Static method to round a float value to the number of decimal places
083     * defined by DECIMAL_PLACES.
084     *
085     * @param value float value to round
086     * @return float value rounded to DECIMAL_PLACES decimal places
087     */
088    public static float roundDecimal(float value) {
089        return roundDecimal(value, Math.log10(INT_PRECISION));
090    }
091
092    @Override
093    public String getBeanType() {
094        return Bundle.getMessage("BeanNameAudio");
095    }
096
097}