| | |
| | | public final void setBaseUnit(DurationUnit unit) { |
| | | Reject.ifNull(unit); |
| | | |
| | | // Make sure that the base unit is not bigger than the maximum |
| | | // unit. |
| | | if (maximumUnit != null) { |
| | | if (unit.getDuration() > maximumUnit.getDuration()) { |
| | | throw new IllegalArgumentException("Base unit greater than maximum unit"); |
| | | } |
| | | // Make sure that the base unit is not bigger than the maximum unit. |
| | | if (maximumUnit != null && unit.getDuration() > maximumUnit.getDuration()) { |
| | | throw new IllegalArgumentException("Base unit greater than maximum unit"); |
| | | } |
| | | |
| | | this.baseUnit = unit; |
| | |
| | | * base unit. |
| | | */ |
| | | public final void setMaximumUnit(String unit) { |
| | | if (unit == null) { |
| | | setMaximumUnit((DurationUnit) null); |
| | | } else { |
| | | setMaximumUnit(DurationUnit.getUnit(unit)); |
| | | } |
| | | setMaximumUnit(unit != null ? DurationUnit.getUnit(unit) : null); |
| | | } |
| | | |
| | | /** |
| | |
| | | * unit. |
| | | */ |
| | | public final void setMaximumUnit(DurationUnit unit) { |
| | | if (unit != null) { |
| | | // Make sure that the maximum unit is not smaller than the |
| | | // base unit. |
| | | if (unit.getDuration() < baseUnit.getDuration()) { |
| | | throw new IllegalArgumentException("Maximum unit smaller than base unit"); |
| | | } |
| | | // Make sure that the maximum unit is not smaller than the base unit. |
| | | if (unit != null && unit.getDuration() < baseUnit.getDuration()) { |
| | | throw new IllegalArgumentException("Maximum unit smaller than base unit"); |
| | | } |
| | | |
| | | this.maximumUnit = unit; |
| | |
| | | * limit is greater than the upper limit. |
| | | */ |
| | | public final void setUpperLimit(String upperLimit) { |
| | | if (upperLimit == null) { |
| | | setUpperLimit((Long) null); |
| | | } else { |
| | | setUpperLimit(DurationUnit.parseValue(upperLimit, baseUnit)); |
| | | } |
| | | setUpperLimit(upperLimit != null ? DurationUnit.parseValue(upperLimit, baseUnit) : null); |
| | | } |
| | | |
| | | /** |
| | |
| | | public String encodeValue(Long value) { |
| | | Reject.ifNull(value); |
| | | |
| | | // Make sure that we correctly encode negative values as |
| | | // "unlimited". |
| | | if (allowUnlimited) { |
| | | if (value < 0) { |
| | | return UNLIMITED; |
| | | } |
| | | // Make sure that we correctly encode negative values as "unlimited". |
| | | if (allowUnlimited && value < 0) { |
| | | return UNLIMITED; |
| | | } |
| | | |
| | | // Encode the size value using the base unit. |
| | |
| | | Reject.ifNull(value); |
| | | |
| | | // First check for the special "unlimited" value when necessary. |
| | | if (allowUnlimited) { |
| | | if (value.trim().equalsIgnoreCase(UNLIMITED)) { |
| | | return -1L; |
| | | } |
| | | if (allowUnlimited && UNLIMITED.equalsIgnoreCase(value.trim())) { |
| | | return -1L; |
| | | } |
| | | |
| | | // Parse the string representation. |
| | |
| | | Long i = (long) baseUnit.fromMilliSeconds(ms); |
| | | try { |
| | | validateValue(i); |
| | | return i; |
| | | } catch (PropertyException e) { |
| | | throw PropertyException.illegalPropertyValueException(this, value); |
| | | } |
| | | return i; |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |