001package jmri.jmrit.symbolicprog;
002
003import java.util.HashMap;
004import javax.swing.JLabel;
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008/**
009 * Like {@link SplitVariableValue}, except that the string representation is split out
010 * into 100's in each CV (up to two in this first implementation)
011 * <br><br>
012 * All the attributes of {@link SplitVariableValue} are inherited.
013 *
014 *
015 * @author Bob Jacobsen Copyright (C) 2001, 2002, 2003, 2004, 2013, 2014
016 * @author Dave Heap Copyright (C) 2016
017 */
018public class SplitHundredsVariableValue extends SplitVariableValue {
019
020    public SplitHundredsVariableValue(String name, String comment, String cvName,
021            boolean readOnly, boolean infoOnly, boolean writeOnly, boolean opsOnly,
022            String cvNum, String mask, int minVal, int maxVal,
023            HashMap<String, CvValue> v, JLabel status, String stdname,
024            String pSecondCV, int pFactor, int pOffset, String uppermask, String extra1, String extra2, String extra3, String extra4) {
025        super(name, comment, cvName, readOnly, infoOnly, writeOnly, opsOnly, cvNum, mask, minVal, maxVal, v, status, stdname, pSecondCV, pFactor, pOffset, uppermask, extra1, extra2, extra3, extra4);
026    }
027
028    @Override
029    public void stepOneActions(String name, String comment, String cvName,
030            boolean readOnly, boolean infoOnly, boolean writeOnly, boolean opsOnly,
031            String cvNum, String mask, int minVal, int maxVal,
032            HashMap<String, CvValue> v, JLabel status, String stdname,
033            String pSecondCV, int pFactor, int pOffset, String uppermask, String extra1, String extra2, String extra3, String extra4) {
034
035        if (extra3 != null) {
036            _minVal = getValueFromText(extra3);
037        }
038        if (extra4 != null) {
039            _maxVal = getValueFromText(extra4);
040        }
041    }
042
043    @Override
044    long getValueFromText(String s) {
045        if (s.isEmpty()) return 0L;
046        long val = Long.parseUnsignedLong(s);
047        long result = 0;
048        long multiplier = 1L;
049        final long INRADIX = 100L;
050        final long OUTRADIX = 0x100;
051        while (val > 0) {
052            long digits = val % INRADIX;
053            val = val/INRADIX;
054            result = result + multiplier*digits;
055            multiplier = multiplier * OUTRADIX;
056        }
057        return result;
058    }
059
060    @Override
061    String getTextFromValue(long val) {
062        String result = "";
063
064        final long INRADIX = 0x100;
065        while (val > 0) {
066            long digits = val % INRADIX;
067            val = val/INRADIX;
068            result = "" + String.format("%02d", digits) + result;// that's a String prepend operation
069        }
070        result = "0"+result;  // never blank, even if the input is zero
071        // remove leading zeros for appearance sake
072        while (result.startsWith("0")) {
073            if (result.length() <= 1) break;  // leave one zero if thats all there is
074            result = result.substring(1);
075        }
076        return result;
077    }
078
079    /**
080     * Set value from a String value.
081     *
082     * @param value a string representing the unsigned hundreds-based value to be set
083     */
084    @Override
085    public void setValue(String value) {
086        try {
087            var result = getValueFromText(value);
088            setLongValue(result);
089        } catch (NumberFormatException e) {
090            log.warn("handling non-numeric value \"{}\"", value);
091            return;
092        }
093
094    }
095
096    // initialize logging
097    private final static Logger log = LoggerFactory.getLogger(SplitHundredsVariableValue.class);
098}