Simplest way to automatically alter "const" value in Java during compile time
This is a question corresponds to my uni assignment so I am very sorry to say I cannot adopt any one of the following best practices in a short time -- you know -- assignment is due tomorrow :(
link to Best way to alter const in Java on StackOverflow
Basically the only task (I hope so) left for me is the performance tuning. I've got a bunch of predefined "const" values in my single-class agent source code like this:
// values used as "const"s
private static long FT_THRESHOLD = 400;
private static long FT_THRESHOLD_MARGIN = 50;
private static long FT_SMOOTH_PRICE_IDICATOR = 20;
private static long FT_BUY_PRICE_MODIFIER = 0;
private static long FT_LAST_ROUNDS_STARTTIME = 90;
private static long FT_AMOUNT_ADJUST_MODIFIER = 5;
private static long FT_HISTORY_PIRCES_LENGTH = 10;
private static long FT_TRACK_DURATION = 5;
private static int MAX_BED_BID_NUM_PER_AUC = 12;
I can definitely alter the values manu开发者_如何学JAVAally and then compile the code to give it another go around. But the execution time for a thorough "statistic analysis" usually requires over 2000 times of execution, which will typically lasts more than half an hour on my own laptop...
So I hope there is a way to alter values using other ways than dig into the source code to change the "const" values there, so I can automatically distributed compiled code to other people's PC and let them run the statistic analysis instead.
One other reason for a automatically value adjustment is that I can try using my own agent to defeat itself by choosing different "const" values. Although my values are derived from previous history and statistical results, they are far from the most optimized values.
I hope there is a easy way so I can quickly adopt that so to have a good sleep tonight while the computer does everything for me... :)
Any hints on this sort of stuff? Any suggestion is welcomed and much appreciated.
Forget about constants, they are not what you want here. Go with a Properties file you read from a file on the disk. That is what I would do if I had to do it by tomorrow. You will have to fiddle with classpaths to make that work, and I am assuming you already covered file IO in Java.
As others have suggested, reading in a Properties file would be a relatively quick and easy way to accomplish this.
If you are trying to run the program against different versions of itself in the same JVM, your variables CANNOT be static.
Assuming you reimplement this as properties files, here's a little reference material:
A properties file typically looks like this:
#This is a .properties file
FT_THRESHOLD=400
FT_THRESHOLD_MARGIN=50
etc...
Reading in Properties from a file is pretty simple, just make a fileInputStream out of the file and call the load() method on the java.util.Properties class.
public void readProperties(File propertiesFile)
{
Properties props = new Properties();
props.load(new FileInputStream(propertiesFile));
FT_THRESHOLD = Long.parseLong(props.getProperty("FT_THRESHOLD"));
FT_TRACK_DURATION = Long.parseLong(props.getProperty("FT_TRACK_DURATION"));
...etc
}
Hope that helps!
You could assign the return value of a method reading a Properties file to a static final variable.
精彩评论