001: package org.conform.validator;
002:
003: import org.conform.Validator;
004: import org.conform.ValidationException;
005:
006: import java.lang.annotation.Annotation;
007:
008: public class NumberRangeValidator implements Validator {
009: private Number from;
010: private Number until;
011: private boolean fromInclusive;
012: private boolean untilInclusive;
013:
014: public NumberRangeValidator() {
015: }
016:
017: public NumberRangeValidator(Annotation annotation) {
018: NumberRange numberRange = (NumberRange) annotation;
019: String from = numberRange.from();
020: if (from.length() > 0) {
021: if (from.indexOf('.') != -1)
022: this .from = new Double(from);
023: else
024: this .from = new Long(from);
025: }
026: fromInclusive = numberRange.fromInclusive();
027:
028: String until = numberRange.until();
029: if (until.length() > 0) {
030: if (until.indexOf('.') != -1)
031: this .until = new Double(until);
032: else
033: this .until = new Long(until);
034: }
035: untilInclusive = numberRange.untilInclusive();
036: }
037:
038: public NumberRangeValidator(Number from, Number until) {
039: this .from = from;
040: this .until = until;
041: }
042:
043: public NumberRangeValidator(Number from, boolean fromInclusive,
044: Number until, boolean untilInclusive) {
045: this .from = from;
046: this .fromInclusive = fromInclusive;
047: this .until = until;
048: this .untilInclusive = untilInclusive;
049: }
050:
051: public Number getFrom() {
052: return from;
053: }
054:
055: public void setFrom(Number from) {
056: this .from = from;
057: }
058:
059: public Number getUntil() {
060: return until;
061: }
062:
063: public void setUntil(Number until) {
064: this .until = until;
065: }
066:
067: public boolean isFromInclusive() {
068: return fromInclusive;
069: }
070:
071: public void setFromInclusive(boolean fromInclusive) {
072: this .fromInclusive = fromInclusive;
073: }
074:
075: public boolean isUntilInclusive() {
076: return untilInclusive;
077: }
078:
079: public void setUntilInclusive(boolean untilInclusive) {
080: this .untilInclusive = untilInclusive;
081: }
082:
083: /**
084: * ...[...]...
085: * *****
086: * ...]...[...
087: * ***
088: */
089: public Object validate(Object value) {
090: if (value == null)
091: return null;
092:
093: Number number = (Number) value;
094: double n = number.doubleValue();
095:
096: if (from != null) {
097: double f = from.doubleValue();
098: if ((fromInclusive && n < f) || (!fromInclusive && n <= f))
099: throw new ValidationException(
100: new ValidationException.Message(
101: getFromMessage(fromInclusive),
102: new Object[] { value, from, until }));
103: }
104: if (until != null) {
105: double u = until.doubleValue();
106: if ((untilInclusive && n > u)
107: || (!untilInclusive && n >= u))
108: throw new ValidationException(
109: new ValidationException.Message(
110: getUntilMessage(untilInclusive),
111: new Object[] { value, from, until }));
112: }
113:
114: return value;
115: }
116:
117: private String getFromMessage(boolean fromInclusive) {
118: return fromInclusive ? "validation.numberFromInclusive"
119: : "validation.numberFrom";
120: }
121:
122: private String getUntilMessage(boolean untilInclusive) {
123: return untilInclusive ? "validation.numberUntilInclusive"
124: : "validation.numberUntil";
125: }
126: }
|