001package jmri.jmrit.ctc.ctcserialdata;
002
003import java.io.IOException;
004import java.io.StringReader;
005import java.util.ArrayList;
006import javax.swing.ButtonGroup;
007import jmri.*;
008import jmri.jmrit.ctc.*;
009
010import org.apache.commons.csv.CSVFormat;
011import org.apache.commons.csv.CSVParser;
012
013/**
014 *
015 * @author Gregory J. Bedlek Copyright (C) 2018, 2019
016 */
017
018public class ProjectsCommonSubs {
019    static final public String SSV_SEPARATOR = ";";
020
021    static public ArrayList<String> getArrayListFromCSV(String csvString) { return helper1(csvString, CSVFormat.DEFAULT.getDelimiterString());}
022    static public ArrayList<String> getArrayListFromSSV(String ssvString) { return helper1(ssvString, SSV_SEPARATOR); }
023    static private ArrayList<String> helper1(String ssvString, String separator) {
024        ArrayList<String> list = new ArrayList<>();
025        try (CSVParser parser = new CSVParser(new StringReader(ssvString),
026                CSVFormat.Builder.create(CSVFormat.DEFAULT)
027                        .setQuote(null).setDelimiter(separator).setRecordSeparator(null).build())) {
028            parser.getRecords().forEach(record -> record.forEach(item -> list.add(item)));
029        } catch (IOException ex) {
030            log.error("Unable to parse {}", ssvString, ex);
031        }
032        return list;
033    }
034
035    static public ArrayList<String> getArrayListOfSignalNames(ArrayList<NBHSignal> array) {
036        ArrayList<String> stringList = new ArrayList<>();
037        array.forEach(row -> {
038            var handle = (NamedBeanHandle<?>) row.getBeanHandle();
039            stringList.add(handle.getName());
040        });
041        return stringList;
042    }
043
044    static public ArrayList<NBHSignal> getArrayListOfSignals(ArrayList<String> signalNames) {
045        CtcManager cm = InstanceManager.getDefault(CtcManager.class);
046        ArrayList<NBHSignal> newList = new ArrayList<>();
047        signalNames.forEach(name -> {
048            NBHSignal newSignal = cm.getNBHSignal(name);
049            if (newSignal == null) {
050                newSignal = new NBHSignal(name);
051            }
052            if (newSignal.valid()) {
053                newList.add(newSignal);
054            }
055        });
056        return newList;
057    }
058
059//  Returns an ArrayList guaranteed to have exactly "returnArrayListSize" entries,
060//  and if the passed "csvString" has too few entries, then those missing end values are set to "":
061    static public ArrayList<String> getFixedArrayListSizeFromCSV(String csvString, int returnArrayListSize) {
062        ArrayList<String> returnArray = getArrayListFromCSV(csvString);
063        while (returnArray.size() < returnArrayListSize) returnArray.add("");
064        return returnArray;
065    }
066
067    static public int getIntFromStringNoThrow(String aString, int defaultValueIfProblem) {
068        int returnValue = defaultValueIfProblem;    // Default if error
069        try { returnValue = Integer.parseInt(aString); } catch (NumberFormatException e) {}
070        return returnValue;
071    }
072
073    public static boolean isNullOrEmptyString(String aString) {
074        return aString == null || aString.trim().length() == 0;
075    }
076
077//  If you used "CommonSubs.numberButtonGroup" above to setup the button group, then
078//  you can call this routine to get the switch value as an int value,
079//  since exception "NumberFormatException" should NEVER be thrown!
080//  If it does throw because you screwed something up, it will return -1.
081    public static int getButtonSelectedInt(ButtonGroup buttonGroup) {
082        try { return Integer.parseInt(getButtonSelectedString(buttonGroup)); } catch (NumberFormatException e) { return -1; }
083    }
084
085    public static String getButtonSelectedString(ButtonGroup buttonGroup) {
086        return buttonGroup.getSelection().getActionCommand();
087    }
088
089    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ProjectsCommonSubs.class);
090}