Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

So I have been looking through some code I wrote a few years ago for an economic simulation program. Each simulation has a large number of settings that can be saved to a file and later loaded back into the program to re-run the same/similar simulation. Some of the settings are optional or depend on what is being simulated.

The code to read back the parameters is basically one very large switch statement (with a few nested switch statements). I was wondering if there is a better way to handle this situation.

One line of the settings file might look like this:

#RA:1,MT:DiscriminatoryPriceKDoubleAuction,OF:Demo Output.csv,QM:100,NT:5000,KP:0.5 //continues...

And some of the code that would read that line:

switch( Character.toUpperCase( s.charAt(0) ) )
{
    case 'R':
         randSeed = Integer.valueOf( s.substring(3).trim() );
         break;
    case 'M':
          marketType = s.substring(3).trim();
          System.err.println("MarketType: " + marketType);
          break;
    case 'O':
          outputFileName = s.substring(3).trim() ;
          break;
    case 'Q':
          quantityOfMarkets = Integer.valueOf( s.substring(3).trim() );
          break;
    case 'N':
          maxTradesPerRound = Integer.valueOf( s.substring(3).trim() );
          break;
    case 'K':
          kParameter = Float.valueOf( s.substring(3).trim() );
          break;
 // continues...
}
share|improve this question
    
Have you thought about serializing the object to disk, and deserializing it later? –  blueberryfields Aug 14 '11 at 20:36
3  
Reinvent the wheel much? I suggest you use one of many existing formats for storing settings and then use a library. If you still decide against it, why not just parse a line into a dictionary first and then process it later? These lines are not very different from a complex combination of command line switches, if you remove commas, colons, and prefix options with a'-'. Ideally your program can take args on command line or read them from a settings file and work the same way. *nix tools do this. Check out stackoverflow.com/questions/491595/… –  Job Aug 15 '11 at 3:42
add comment

2 Answers

From a code perspective, the cleanest method would be to switch to the Properties class. It's made for handling this, although you would either need to define all of your settings (even the unused ones) in the file it reads from or have predefined constants in a source file that you can use if the properties file returns null for a particular named property.

If you can't change the scheme, you could follow the general strategy of having a Hashtable, which is really what the Properties class does - it reads a file into a Hashtable conveniently. It would pretty much be rolling your own properties. Again, you would have to have preset values defined somehow to handle cases where a setting that you need might not exist in the file.

The way you would use either is to simply query the data structure any time you needed a value. You can either enforce that it exists in the data structure (throw an Exception and terminate it it doesn't) or define default values somehow (probably in a source file).

share|improve this answer
add comment

An excellent library for this sort of work is Apache Commons Configuration

provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources. Commons Configuration provides typed access to single, and multi-valued configuration parameters as demonstrated by the following code:

    Double double = config.getDouble("number");
    Integer integer = config.getInteger("number");

Configuration parameters may be loaded from the following sources:

  • Properties files
  • XML documents
  • Windows INI files
  • Property list files (plist)
  • JNDI
  • JDBC Datasource
  • System properties
  • Applet parameters
  • Servlet parameters

Different configuration sources can be mixed using a ConfigurationFactory and a CompositeConfiguration. Additional sources of configuration parameters can be created by using custom configuration objects. This customization can be achieved by extending AbstractConfiguration or AbstractFileConfiguration.

The full Javadoc API documentation is available here...

share|improve this answer
    
yes properties is only string but this is numbers, dates and from different formats. neat –  tgkprog Apr 4 '13 at 18:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.