Java: Read from Config File & Efficiently Perform

2019-08-29 05:21发布

问题:

Okay, I'm sure that I'm not going about this in the most efficient way and I'm looking for some help regarding how to do this more efficiently...

  • config.txt file contains key/value pairs, where key = name of test and value = whether to execute test
  • parse through config file and create a list of tests to run
  • run those tests

Here is how I'm currently going about this

  • create an ArrayList by passing to a helper function, parseConfig, a BufferedReader over my config file. parseConfig returns a TreeSet , which I use in the constructor method for my ArrayList
  • parseConfig iterates over lines of text in config file. If value indicates to perform test, add name of test to TreeSet. Return TreeSet.
  • Iterate over ArrayList with enhanced for loop. Body of enhanced for loop is basically a long if/else statement...if key.equals ("thisTest"), perform thisTest, else if key.equals (thatTest), perform thatTest...etc

It's that last part that I really don't like. It works well enough, but it seems clumsy and inefficient. Since my ArrayList is constructed using a TreeSet, it is in sorted order. I would like to use a more elegant and deterministic method for mapping my keys to tests to perform. Can anyone help me?

回答1:

I would do something else since all you need to do with this list is to test it's entries or not.

I would take line by line and apply a regular expression on it, from what I see it is going to be really simple with only two groups and a positive lookahead, this way I could extract all the matching lines only and create an ArrayList out of those, then iterate the ArrayList and test every method. If you can give some input of how the file looks I can help you put with the code.

UPDATE

For example here is the code I come up (in 5 min could be improved) that would do the parsing:

 /**
 * 
 * @param inputFile location of inputFile
 * @return {@link ImmutableSet} of tests to run
 */
public static ImmutableSet<String> parseConfigFile(File inputFile){
    HashSet<String> innerSet = Sets.newHashSet();
    BufferedReader bufferedReader = null;
    try {
        bufferedReader = new BufferedReader(new FileReader(inputFile));
        String newLine = "";
        while( (newLine = bufferedReader.readLine()) != null){
            Pattern p = Pattern.compile("(.+)=(?=yes|1|true)(.+)");
            Matcher m = p.matcher(newLine);

            while(m.find()){
                //System.out.println(m.group(1));
                innerSet.add(m.group(1));
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(bufferedReader != null)
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return ImmutableSet.copyOf(innerSet);
}

I testes it for a file that looks like this for example:

  SomeTest=true
  SomeOtherTest=false
  YetAnotherTest=1
  LastTest=yes
  GogoTest=no
  OneMore=0


回答2:

The answer was to create a HashMap <String, Method> object.