New property descriptor constructors for single-value types that accept string values to simplify external/factory usage

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6459 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brian Remedios
2008-09-08 03:17:15 +00:00
parent 1e2c6f2952
commit 5f95b5a799
7 changed files with 139 additions and 43 deletions

View File

@ -21,7 +21,24 @@ public class BooleanProperty extends AbstractScalarProperty<Boolean> {
public BooleanProperty(String theName, String theDescription, Boolean defaultValue, float theUIOrder) {
super(theName, theDescription, Boolean.valueOf(defaultValue), theUIOrder);
}
/**
* Constructor for BooleanProperty limited to a single value.
* Converts default argument string into a boolean.
*
* @param theName String
* @param theDescription String
* @param defaultStr String
* @param theUIOrder float
*/
public BooleanProperty(String theName, String theDescription, String defaultStr, float theUIOrder) {
this(theName, theDescription, boolFrom(defaultStr), theUIOrder);
}
private static Boolean boolFrom(String boolStr) {
return Boolean.valueOf(boolStr);
}
/**
* @return Class
* @see net.sourceforge.pmd.PropertyDescriptor#type()
@ -37,6 +54,6 @@ public class BooleanProperty extends AbstractScalarProperty<Boolean> {
* @return Object
*/
protected Object createFrom(String value) {
return Boolean.valueOf(value);
return boolFrom(value);
}
}

View File

@ -15,13 +15,28 @@ public class CharacterProperty extends AbstractProperty<Character> {
* Constructor for CharacterProperty.
* @param theName String
* @param theDescription String
* @param theDefault char
* @param theDefault Character
* @param theUIOrder float
*/
public CharacterProperty(String theName, String theDescription, Character theDefault, float theUIOrder) {
super(theName, theDescription, Character.valueOf(theDefault), theUIOrder);
super(theName, theDescription, theDefault, theUIOrder);
}
/**
* Constructor for CharacterProperty.
* @param theName String
* @param theDescription String
* @param defaultStr String
* @param theUIOrder float
*/
public CharacterProperty(String theName, String theDescription, String defaultStr, float theUIOrder) {
this(theName, theDescription, charFrom(defaultStr), theUIOrder);
}
public static Character charFrom(String charStr) {
return charStr.charAt(0);
}
/**
* Method type.
* @return Class

View File

@ -25,6 +25,25 @@ public class DoubleProperty extends AbstractNumericProperty<Double> {
super(theName, theDescription, min, max, theDefault, theUIOrder);
}
/**
* Constructor for DoubleProperty.
* @param theName String
* @param theDescription String
* @param minStr String
* @param maxStr String
* @param theDefault String
* @param theUIOrder float
* @throws IllegalArgumentException
*/
public DoubleProperty(String theName, String theDescription, String minStr, String maxStr, String defaultStr, float theUIOrder) {
this(theName, theDescription, doubleFrom(minStr), doubleFrom(maxStr), doubleFrom(defaultStr), theUIOrder);
}
public static Double doubleFrom(String numberString) {
return Double.valueOf(numberString);
}
/**
* Method type.
* @return Class
@ -41,6 +60,6 @@ public class DoubleProperty extends AbstractNumericProperty<Double> {
* @return Object
*/
protected Object createFrom(String value) {
return Double.valueOf(value);
return doubleFrom(value);
}
}

View File

@ -25,6 +25,26 @@ public class FloatProperty extends AbstractNumericProperty<Float> {
super(theName, theDescription, Float.valueOf(min), Float.valueOf(max), Float.valueOf(theDefault), theUIOrder);
}
/**
* Constructor for FloatProperty that limits itself to a single value within the specified limits.
* Converts string arguments into the Float values.
*
* @param theName String
* @param theDescription String
* @param minStr String
* @param maxStr String
* @param defaultStr String
* @param theUIOrder float
* @throws IllegalArgumentException
*/
public FloatProperty(String theName, String theDescription, String minStr, String maxStr, String defaultStr, float theUIOrder) {
this(theName, theDescription, floatFrom(minStr), floatFrom(maxStr), floatFrom(defaultStr), theUIOrder);
}
public static Float floatFrom(String numberString) {
return Float.valueOf(numberString);
}
/**
* @return Class
* @see net.sourceforge.pmd.PropertyDescriptor#type()
@ -40,6 +60,6 @@ public class FloatProperty extends AbstractNumericProperty<Float> {
* @return Object
*/
protected Object createFrom(String value) {
return Float.valueOf(value);
return floatFrom(value);
}
}

View File

@ -11,7 +11,8 @@ package net.sourceforge.pmd.lang.rule.properties;
public class IntegerProperty extends AbstractNumericProperty<Integer> {
/**
* Constructor for IntegerProperty.
* Constructor for IntegerProperty that limits itself to a single value within the specified limits.
*
* @param theName String
* @param theDescription String
* @param min Integer
@ -24,6 +25,26 @@ public class IntegerProperty extends AbstractNumericProperty<Integer> {
super(theName, theDescription, min, max, theDefault, theUIOrder);
}
/**
* Constructor for IntegerProperty that limits itself to a single value within the specified limits.
* Converts string arguments into the Float values.
*
* @param theName String
* @param theDescription String
* @param minStr String
* @param maxStr String
* @param defaultStr String
* @param theUIOrder
* @throws IllegalArgumentException
*/
public IntegerProperty(String theName, String theDescription, String minStr, String maxStr, String defaultStr, float theUIOrder) {
this(theName, theDescription, intFrom(minStr), intFrom(maxStr), intFrom(defaultStr), theUIOrder);
}
public static Integer intFrom(String numberString) {
return Integer.valueOf(numberString);
}
/**
* Method type.
* @return Class
@ -39,6 +60,6 @@ public class IntegerProperty extends AbstractNumericProperty<Integer> {
* @return Object
*/
protected Object createFrom(String value) {
return Integer.valueOf(value);
return intFrom(value);
}
}

View File

@ -18,11 +18,32 @@ public class LongProperty extends AbstractNumericProperty<Long> {
* @param max Long
* @param theDefault Long
* @param theUIOrder float
* @throws IllegalArgumentException
*/
public LongProperty(String theName, String theDescription, Long min, Long max, Long theDefault, float theUIOrder) {
super(theName, theDescription, min, max, theDefault, theUIOrder);
}
/**
* Constructor for LongProperty that limits itself to a single value within the specified limits.
* Converts string arguments into the Long values.
*
* @param theName String
* @param theDescription String
* @param minStr String
* @param maxStr String
* @param defaultStr String
* @param theUIOrder float
* @throws IllegalArgumentException
*/
public LongProperty(String theName, String theDescription, String minStr, String maxStr, String defaultStr, float theUIOrder) {
this(theName, theDescription, longFrom(minStr), longFrom(maxStr), longFrom(defaultStr), theUIOrder);
}
public static Long longFrom(String numberString) {
return Long.valueOf(numberString);
}
/**
* Method type.
* @return Class
@ -38,6 +59,6 @@ public class LongProperty extends AbstractNumericProperty<Long> {
* @return Object
*/
protected Object createFrom(String value) {
return Long.valueOf(value);
return longFrom(value);
}
}

View File

@ -4,6 +4,7 @@
package net.sourceforge.pmd.lang.rule.properties;
import net.sourceforge.pmd.PropertyDescriptor;
import net.sourceforge.pmd.util.StringUtil;
public class PropertyDescriptorFactory {
/**
@ -34,13 +35,12 @@ public class PropertyDescriptorFactory {
private static PropertyDescriptor<?> createRawPropertyDescriptor(String name, String description, String type,
String delimiter, String min, String max, String value) {
if ("Boolean".equals(type)) {
return new BooleanProperty(name, description, Boolean.valueOf(value), 0.0f);
return new BooleanProperty(name, description, value, 0.0f);
} else if ("Boolean[]".equals(type)) {
BooleanMultiProperty property = new BooleanMultiProperty(name, description, null, 0.0f);
return new BooleanMultiProperty(name, description, property.valueFrom(value), 0.0f);
} else if ("Character".equals(type)) {
CharacterProperty property = new CharacterProperty(name, description, null, 0.0f);
return new CharacterProperty(name, description, property.valueFrom(value), 0.0f);
return new CharacterProperty(name, description, CharacterProperty.charFrom(value), 0.0f);
} else if ("Character[]".equals(type)) {
checkDelimiter(name, type, delimiter);
char delim = delimiter.charAt(0);
@ -48,51 +48,34 @@ public class PropertyDescriptorFactory {
return new CharacterMultiProperty(name, description, property.valueFrom(value), 0.0f, delim);
} else if ("Double".equals(type)) {
checkMinMax(name, type, min, max);
DoubleProperty property = new DoubleProperty(name, description, 0d, 0d, null, 0.0f);
return new DoubleProperty(name, description, property.valueFrom(min), property.valueFrom(max), property
.valueFrom(value), 0.0f);
return new DoubleProperty(name, description, min, max, value, 0.0f);
} else if ("Double[]".equals(type)) {
checkMinMax(name, type, min, max);
DoubleProperty propertySingle = new DoubleProperty(name, description, 0d, 0d, null, 0.0f);
checkMinMax(name, type, min, max);
DoubleMultiProperty property = new DoubleMultiProperty(name, description, 0d, 0d, null, 0.0f);
return new DoubleMultiProperty(name, description, propertySingle.valueFrom(min), propertySingle
.valueFrom(max), property.valueFrom(value), 0.0f);
return new DoubleMultiProperty(name, description, DoubleProperty.doubleFrom(min), DoubleProperty.doubleFrom(max), property.valueFrom(value), 0.0f);
} else if ("Float".equals(type)) {
checkMinMax(name, type, min, max);
FloatProperty property = new FloatProperty(name, description, 0f, 0f, null, 0.0f);
return new FloatProperty(name, description, property.valueFrom(min), property.valueFrom(max), property
.valueFrom(value), 0.0f);
return new FloatProperty(name, description, min, max, value, 0.0f);
} else if ("Float[]".equals(type)) {
checkMinMax(name, type, min, max);
FloatProperty propertySingle = new FloatProperty(name, description, 0f, 0f, null, 0.0f);
FloatMultiProperty property = new FloatMultiProperty(name, description, 0f, 0f, null, 0.0f);
return new FloatMultiProperty(name, description, propertySingle.valueFrom(min), propertySingle
.valueFrom(max), property.valueFrom(value), 0.0f);
return new FloatMultiProperty(name, description, FloatProperty.floatFrom(min), FloatProperty.floatFrom(max), property.valueFrom(value), 0.0f);
} else if ("Integer".equals(type)) {
checkMinMax(name, type, min, max);
IntegerProperty property = new IntegerProperty(name, description, 0, 0, null, 0.0f);
return new IntegerProperty(name, description, property.valueFrom(min), property.valueFrom(max), property
.valueFrom(value), 0.0f);
checkMinMax(name, type, min, max);
return new IntegerProperty(name, description, min, max, value, 0.0f);
} else if ("Integer[]".equals(type)) {
checkMinMax(name, type, min, max);
IntegerProperty propertySingle = new IntegerProperty(name, description, 0, 0, null, 0.0f);
IntegerMultiProperty property = new IntegerMultiProperty(name, description, 0, 0, null, 0.0f);
return new IntegerMultiProperty(name, description, propertySingle.valueFrom(min), propertySingle
.valueFrom(max), property.valueFrom(value), 0.0f);
return new IntegerMultiProperty(name, description, IntegerProperty.intFrom(min), IntegerProperty.intFrom(max), property.valueFrom(value), 0.0f);
} else if ("Long".equals(type)) {
checkMinMax(name, type, min, max);
LongProperty property = new LongProperty(name, description, 0l, 0l, null, 0.0f);
return new LongProperty(name, description, property.valueFrom(min), property.valueFrom(max), property
.valueFrom(value), 0.0f);
return new LongProperty(name, description, min, max, value, 0.0f);
} else if ("Long[]".equals(type)) {
checkMinMax(name, type, min, max);
LongProperty propertySingle = new LongProperty(name, description, 0l, 0l, null, 0.0f);
checkMinMax(name, type, min, max);
LongMultiProperty property = new LongMultiProperty(name, description, 0l, 0l, null, 0.0f);
return new LongMultiProperty(name, description, propertySingle.valueFrom(min), propertySingle
.valueFrom(max), property.valueFrom(value), 0.0f);
} else if ("String".equals(type)) {
StringProperty property = new StringProperty(name, description, null, 0.0f);
return new StringProperty(name, description, property.valueFrom(value), 0.0f);
return new LongMultiProperty(name, description, LongProperty.longFrom(min), LongProperty.longFrom(max), property.valueFrom(value), 0.0f);
} else if ("String".equals(type)) {
return new StringProperty(name, description, value, 0.0f);
} else if ("String[]".equals(type)) {
checkDelimiter(name, type, delimiter);
char delim = delimiter.charAt(0);
@ -111,11 +94,11 @@ public class PropertyDescriptorFactory {
}
private static void checkMinMax(String name, String type, String min, String max) {
if (min == null || min.length() == 0) {
if (StringUtil.isEmpty(min)) {
throw new IllegalArgumentException("Min must be provided to create PropertyDescriptor for " + name
+ " of type " + type + ".");
}
if (max == null || max.length() == 0) {
if (StringUtil.isEmpty(max)) {
throw new IllegalArgumentException("Max must be provided to create PropertyDescriptor for " + name
+ " of type " + type + ".");
}