-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Beginnings of better Parameter handling
Added ParamHandlerSingleton class Added ParameterGroup class Added Parameter class interface Added DoubleParameter class Added IntegerParameter class Added PTMShepherd.initParameters
- Loading branch information
1 parent
e6fdf10
commit c59a658
Showing
6 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/edu/umich/andykong/ptmshepherd/paramhandling/DoubleParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package edu.umich.andykong.ptmshepherd.paramhandling; | ||
|
||
|
||
public class DoubleParameter implements Parameter<Double> { | ||
private String key; | ||
private double value; | ||
private double min; | ||
private double max; | ||
|
||
public DoubleParameter(String key, double min, double max) { | ||
this.key = key; | ||
this.min = min; | ||
this.max = max; | ||
} | ||
|
||
@Override | ||
public Double getValue() { | ||
return this.value; | ||
} | ||
|
||
@Override | ||
public void setValue(Double value) { | ||
if (!isValid(value)) { | ||
throw new IllegalArgumentException("Value out of bounds"); | ||
} | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean isValid(Double value) { | ||
return ((value >= this.min) && (value <= this.max)); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/edu/umich/andykong/ptmshepherd/paramhandling/IntegerParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package edu.umich.andykong.ptmshepherd.paramhandling; | ||
|
||
|
||
public class IntegerParameter implements Parameter<Integer> { | ||
private String key; | ||
private int level; | ||
private int order; | ||
private int value; | ||
private int min; | ||
private int max; | ||
private int defaultValue; | ||
private String description; | ||
|
||
public IntegerParameter(String key, int level, int order, int min, int max, int value, String description) { | ||
this.key = key; | ||
this.level = level; | ||
this.order = order; | ||
this.min = min; | ||
this.max = max; | ||
this.value = value; | ||
this.defaultValue = value; | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public Integer getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void setValue(Integer value) throws IllegalArgumentException { | ||
if (!isValid(value)) { | ||
throw new IllegalArgumentException(String.format(this.key + " received an invalid argument.")); | ||
} | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean isValid(Integer value) { | ||
return ((value >= min) && (value <= max)); | ||
} | ||
|
||
|
||
} | ||
|
52 changes: 52 additions & 0 deletions
52
src/edu/umich/andykong/ptmshepherd/paramhandling/ParamHandlerSingleton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package edu.umich.andykong.ptmshepherd.paramhandling; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ParamHandlerSingleton { | ||
private static ParamHandlerSingleton instance; | ||
private Map<String, ParameterGroup> groups; | ||
//todo make a map here that links to parameter keys to group parameters | ||
|
||
|
||
public static synchronized ParamHandlerSingleton getInstance() { | ||
if (instance == null) { | ||
instance = new ParamHandlerSingleton(); | ||
} | ||
return instance; | ||
} | ||
|
||
|
||
public void addParamGroup(String key) { | ||
try { | ||
if (groups.containsKey(key)) { | ||
throw new IllegalArgumentException("Parameter group already exists: " + key); | ||
} | ||
groups.put(key, new ParameterGroup(key)); | ||
} catch (IllegalArgumentException e) { | ||
return; | ||
} | ||
} | ||
|
||
|
||
@SuppressWarnings("unchecked") | ||
public ParameterGroup getParamGroup(String key) { | ||
ParameterGroup parameterGroup = groups.get(key); | ||
if (parameterGroup == null) { | ||
throw new IllegalArgumentException("Parameter group not found: " + key); | ||
} | ||
return parameterGroup; | ||
} | ||
|
||
public <T> void addParam(String group, String key, Parameter<T> param) { | ||
ParameterGroup parameterGroup = groups.get(group); | ||
parameterGroup.setParamValue(key, param); | ||
} | ||
|
||
public <T> void setParamValue(String group, String key, T value) { | ||
ParameterGroup parameterGroup = groups.get(group); | ||
parameterGroup.setParamValue(key, value); | ||
} | ||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/edu/umich/andykong/ptmshepherd/paramhandling/Parameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package edu.umich.andykong.ptmshepherd.paramhandling; | ||
|
||
public interface Parameter<T> { | ||
T getValue(); | ||
void setValue(T value) throws IllegalArgumentException; | ||
boolean isValid(T value); | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/edu/umich/andykong/ptmshepherd/paramhandling/ParameterGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package edu.umich.andykong.ptmshepherd.paramhandling; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ParameterGroup { | ||
private String name; | ||
private Map<String, Parameter<?>> parameters; | ||
|
||
|
||
public ParameterGroup(String name) { | ||
this.name = name; | ||
this.parameters = new HashMap<>(); | ||
} | ||
|
||
public void addParam(String key, Parameter<?> parameter) { | ||
parameters.put(key, parameter); | ||
} | ||
|
||
|
||
public Parameter<?> getParam(String key) { | ||
return parameters.get(key); | ||
} | ||
|
||
|
||
public <T> void setParamValue(String key, T value) { | ||
Parameter<T> parameter = (Parameter<T>) parameters.get(key); | ||
if (parameter == null) | ||
throw new IllegalArgumentException("Parameter not found: " + key); | ||
parameter.setValue(value); | ||
} | ||
|
||
|
||
|
||
public void printParameters() { | ||
System.out.println("Group: " + name); | ||
parameters.forEach((key, parameter) -> System.out.println(key + ": " + parameter.getValue())); | ||
} | ||
} |