Reputation: 6625
I have a class, say Test; within that is a nested static class, say TestParams.
TestParams only contains some String variables referring to the Test class
The problem which I am facing is that, in the setters of Test class, I need to verify if the set parameter is one of the params declared in the Params class.
The scenario is shown in the code below:
public class Test {
protected String n;
protected int num;
public static class TestParams {
public static final String PARAM_N="n";
public static final String PARAM_NUM="num";
}
public void setParam(String key, Object value) {
// Need to check here the if key is defined in TestParams
// keep adding conditions to IF statement when more params added??
if(key.equals(TestParams.PARAM_N) || (key.equals(TestParams.PARAM_NUM))
// Do some stuff
}
}
Is there any method to replace the IF statement with multiple conditions? (something like if key in TestParams() or maybe some other design for the code structure?
Upvotes: 0
Views: 133
Reputation: 2476
Using reflection could be a solution:
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
public class Test {
protected String n;
protected int num;
private Map<String,Object> params = new HashMap<String,Object>();
public Test(){
Field[] fields = TestParams.class.getDeclaredFields();
for (Field field : fields) {
if ( Modifier.isStatic(field.getModifiers()) ) {
try {
params.put(field.getName(), field.get(null));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
public static class TestParams {
public static final String PARAM_N="n";
public static final String PARAM_NUM="num";
}
public void setParam(String key, Object value) {
if( params.containsValue(key)){
// do ....
}else{
// do ...
}
}
}
1 - you loop over static variables in TestParams
2 - save these variables in a map<paramName,ParamValue>
in your Test Class
3 - in your SetParam
method you ask if a value exists in the map
Upvotes: 0
Reputation: 109597
There is such a mechanism, BeanInfo or this, that associates a simple POJO (bean) class with meta information in a separate class. Its major usage nowadays is providing two-column property windows to edit field of some POJO class. Using PropertyDescriptor.
Now, BeanInfo needs some tooling, and that all depends on your use case. The documentation out there never was very appealing, touching on Serializable and such. But it provides functionality, you would do hard to reinvent.
Maybe try it out first for a JComponent, the possibilities like field validation still need some coding.
Upvotes: 0
Reputation: 110
You could put all this fields in a Map like
public class Test {
protected String n;
protected int num;
public static class TestParams {
public static Map<String, Object> PARAMS;
// This will initialize the map statically
static {
PARAMS.put("PARAM_N", "n");
PARAMS.put("PARAM_NUM", "num");
}
}
public void setParam(String key, Object value) {
for(Map.Entry<String, Object> entry : TestParams.PARAMS){
if(entry.getValue().equals(key)){
//do stuff
}
}
}
}
Upvotes: 0
Reputation: 421090
To solve the particular problem you're asking about, I see no other way than to use reflection:
private boolean isValidKey(String str) {
for (Field f : TestParams.class.getFields())
try {
if (f.get(null).equals(str))
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Though I would strongly encourage you to rethink the design.
Upvotes: 2