* \r
* @author pvtroshin\r
* \r
- * Date November 2009\r
+ * @version 1.0 November 2009\r
*/\r
@XmlAccessorType(XmlAccessType.FIELD)\r
public class ValueConstrain {\r
\r
- public static enum Type {\r
- Integer, Float\r
- };\r
-\r
- @XmlElement(required = true)\r
- Type type;\r
- // These can contain different numeric types values, thus they are strings\r
- String max;\r
- String min;\r
-\r
- public Type getType() {\r
- return type;\r
- }\r
-\r
- public void setType(Type type) {\r
- this.type = type;\r
- }\r
-\r
- public Number getMax() {\r
- if (Util.isEmpty(max)) {\r
- return null;\r
- }\r
- switch (type) {\r
- case Float:\r
- return Double.parseDouble(max);\r
- case Integer:\r
- return Integer.parseInt(max);\r
- }\r
- // should not happened\r
- throw new RuntimeException("Type is undefined! ");\r
- }\r
-\r
- public void setMax(String max) {\r
- this.max = max;\r
- }\r
-\r
- public Number getMin() {\r
- if (Util.isEmpty(min)) {\r
- return null;\r
- }\r
- switch (type) {\r
- case Float:\r
- return Double.parseDouble(min);\r
- case Integer:\r
- return Integer.parseInt(min);\r
- }\r
- // should not happened\r
- throw new RuntimeException("Type is undefined! ");\r
- }\r
-\r
- public void setMin(String min) {\r
- this.min = min;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (obj == null) {\r
- return false;\r
- }\r
- ValueConstrain constr = null;\r
- if (obj instanceof ValueConstrain) {\r
- constr = (ValueConstrain) obj;\r
- } else {\r
- return false;\r
- }\r
- if (this.type != constr.type) {\r
- return false;\r
- }\r
- if (this.max != null && constr.max != null) {\r
- if (!this.max.equals(constr.max)) {\r
- return false;\r
- }\r
- } else {\r
- return false;\r
- }\r
-\r
- if (this.min != null && constr.min != null) {\r
- if (!this.min.equals(constr.min)) {\r
- return false;\r
- }\r
- } else {\r
- return false;\r
- }\r
-\r
- return true;\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- String value = "Type: " + this.type + "\n";\r
- if (this.min != null) {\r
- value += "Min: " + this.min + "\n";\r
- }\r
- if (this.max != null) {\r
- value += "Max: " + this.max + "\n";\r
- }\r
- return value;\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- int code = type.hashCode();\r
- if (this.max != null) {\r
- code *= this.max.hashCode();\r
- }\r
- if (this.min != null) {\r
- code *= this.min.hashCode();\r
- }\r
- return code;\r
- }\r
-\r
- boolean hasMinValue() {\r
- return !Util.isEmpty(min);\r
- }\r
-\r
- boolean hasMaxValue() {\r
- return !Util.isEmpty(max);\r
- }\r
-\r
- /**\r
- * Validate that the value is within the constrain boundaries\r
- * \r
- * @param value\r
- * @throws IndexOutOfBoundsException\r
- * when the value is outside the defined boundaries\r
- */\r
- void checkValue(String value) {\r
- switch (type) {\r
- case Float:\r
- float val = Float.parseFloat(value);\r
- if (getMin() != null\r
- && new Float(this.getMin().floatValue()).compareTo(val) == 1) {\r
- throw new IndexOutOfBoundsException("Value '" + value\r
- + "' is lower that minumim value '" + getMin() + "'");\r
- }\r
- if (getMax() != null\r
- && new Float(this.getMax().floatValue()).compareTo(val) == -1) {\r
- throw new IndexOutOfBoundsException("Value '" + value\r
- + "' is greater that maximum value '" + getMax() + "'");\r
- }\r
- break;\r
- case Integer:\r
- int ival = Integer.parseInt(value);\r
- if (getMin() != null\r
- && new Integer(this.getMin().intValue()).compareTo(ival) == 1) {\r
- throw new IndexOutOfBoundsException("Value '" + value\r
- + "' is lower that minumim value '" + getMin() + "'");\r
- }\r
- if (getMax() != null\r
- && new Integer(this.getMax().intValue()).compareTo(ival) == -1) {\r
- throw new IndexOutOfBoundsException("Value '" + value\r
- + "' is greater that maximum value '" + getMax() + "'");\r
- }\r
- break;\r
- }\r
- }\r
-\r
- /**\r
- * Validate the ValueConstrain object. For the ValueConstrain object the\r
- * type and at least one boundary has to be defined\r
- * \r
- * @throws ValidationException\r
- * - if the type or no boundaries are defined\r
- */\r
- void validate() throws ValidationException {\r
- if (this.type == null) {\r
- throw new ValidationException(\r
- "Type is not defined for ValueConstrain: " + this);\r
- }\r
- if (Util.isEmpty(min) && Util.isEmpty(max)) {\r
- throw new ValidationException(\r
- "Both boundaries (min and max) is undefined for ValueConstrain: "\r
- + this);\r
+ public static enum Type {\r
+ Integer, Float\r
+ };\r
+\r
+ @XmlElement(required = true)\r
+ Type type;\r
+ // These can contain different numeric types values, thus they are strings\r
+ String max;\r
+ String min;\r
+\r
+ public Type getType() {\r
+ return type;\r
+ }\r
+\r
+ public void setType(Type type) {\r
+ this.type = type;\r
+ }\r
+\r
+ public Number getMax() {\r
+ if (Util.isEmpty(max)) {\r
+ return null;\r
+ }\r
+ switch (type) {\r
+ case Float:\r
+ return Double.parseDouble(max);\r
+ case Integer:\r
+ return Integer.parseInt(max);\r
+ }\r
+ // should not happened\r
+ throw new RuntimeException("Type is undefined! ");\r
+ }\r
+\r
+ public void setMax(String max) {\r
+ this.max = max;\r
+ }\r
+\r
+ public Number getMin() {\r
+ if (Util.isEmpty(min)) {\r
+ return null;\r
+ }\r
+ switch (type) {\r
+ case Float:\r
+ return Double.parseDouble(min);\r
+ case Integer:\r
+ return Integer.parseInt(min);\r
+ }\r
+ // should not happened\r
+ throw new RuntimeException("Type is undefined! ");\r
+ }\r
+\r
+ public void setMin(String min) {\r
+ this.min = min;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (obj == null) {\r
+ return false;\r
+ }\r
+ ValueConstrain constr = null;\r
+ if (obj instanceof ValueConstrain) {\r
+ constr = (ValueConstrain) obj;\r
+ } else {\r
+ return false;\r
+ }\r
+ if (this.type != constr.type) {\r
+ return false;\r
+ }\r
+ if (this.max != null && constr.max != null) {\r
+ if (!this.max.equals(constr.max)) {\r
+ return false;\r
+ }\r
+ } else {\r
+ return false;\r
+ }\r
+\r
+ if (this.min != null && constr.min != null) {\r
+ if (!this.min.equals(constr.min)) {\r
+ return false;\r
+ }\r
+ } else {\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ String value = "Type: " + this.type + "\n";\r
+ if (this.min != null) {\r
+ value += "Min: " + this.min + "\n";\r
+ }\r
+ if (this.max != null) {\r
+ value += "Max: " + this.max + "\n";\r
+ }\r
+ return value;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ int code = type.hashCode();\r
+ if (this.max != null) {\r
+ code *= this.max.hashCode();\r
+ }\r
+ if (this.min != null) {\r
+ code *= this.min.hashCode();\r
+ }\r
+ return code;\r
+ }\r
+\r
+ boolean hasMinValue() {\r
+ return !Util.isEmpty(min);\r
+ }\r
+\r
+ boolean hasMaxValue() {\r
+ return !Util.isEmpty(max);\r
+ }\r
+\r
+ /**\r
+ * Validate that the value is within the constrain boundaries\r
+ * \r
+ * @param value\r
+ * @throws IndexOutOfBoundsException\r
+ * when the value is outside the defined boundaries\r
+ */\r
+ void checkValue(String value) {\r
+ switch (type) {\r
+ case Float:\r
+ float val = Float.parseFloat(value);\r
+ if (getMin() != null\r
+ && new Float(this.getMin().floatValue()).compareTo(val) == 1) {\r
+ throw new IndexOutOfBoundsException("Value '" + value\r
+ + "' is lower that minumim value '" + getMin() + "'");\r
+ }\r
+ if (getMax() != null\r
+ && new Float(this.getMax().floatValue()).compareTo(val) == -1) {\r
+ throw new IndexOutOfBoundsException("Value '" + value\r
+ + "' is greater that maximum value '" + getMax() + "'");\r
+ }\r
+ break;\r
+ case Integer:\r
+ int ival = Integer.parseInt(value);\r
+ if (getMin() != null\r
+ && new Integer(this.getMin().intValue()).compareTo(ival) == 1) {\r
+ throw new IndexOutOfBoundsException("Value '" + value\r
+ + "' is lower that minumim value '" + getMin() + "'");\r
+ }\r
+ if (getMax() != null\r
+ && new Integer(this.getMax().intValue()).compareTo(ival) == -1) {\r
+ throw new IndexOutOfBoundsException("Value '" + value\r
+ + "' is greater that maximum value '" + getMax() + "'");\r
+ }\r
+ break;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Validate the ValueConstrain object. For the ValueConstrain object the\r
+ * type and at least one boundary has to be defined\r
+ * \r
+ * @throws ValidationException\r
+ * - if the type or no boundaries are defined\r
+ */\r
+ void validate() throws ValidationException {\r
+ if (this.type == null) {\r
+ throw new ValidationException(\r
+ "Type is not defined for ValueConstrain: " + this);\r
+ }\r
+ if (Util.isEmpty(min) && Util.isEmpty(max)) {\r
+ throw new ValidationException(\r
+ "Both boundaries (min and max) is undefined for ValueConstrain: "\r
+ + this);\r
+ }\r
}\r
- }\r
\r
}\r