/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only * (the "License"). You may not use this file except in compliance * with the License. * * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt * or http://forgerock.org/license/CDDLv1.0.html. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at legal-notices/CDDLv1_0.txt. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: * Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END * * * Copyright 2008 Sun Microsystems, Inc. */ package org.opends.server.admin; import static com.forgerock.opendj.util.Validator.*; import java.util.EnumSet; import java.util.Locale; import java.util.MissingResourceException; import org.forgerock.i18n.LocalizableMessage; /** * Integer property definition. *
* All values must be zero or positive and within the lower/upper limit
* constraints. Support is provided for "unlimited" values. These are
* represented using a negative value or using the string "unlimited".
*/
public final class IntegerPropertyDefinition extends PropertyDefinitionnull if there is no
* upper limit.
*/
public final void setUpperLimit(Integer upperLimit) {
if (upperLimit != null) {
if (upperLimit < 0) {
throw new IllegalArgumentException("Negative lower limit");
}
if (lowerLimit > upperLimit) {
throw new IllegalArgumentException("Lower limit greater than upper limit");
}
}
this.upperLimit = upperLimit;
}
/**
* Specify whether or not this property definition will allow unlimited
* values (default is false).
*
* @param allowUnlimited
* true if the property will allow unlimited
* values, or false otherwise.
*/
public final void setAllowUnlimited(boolean allowUnlimited) {
this.allowUnlimited = allowUnlimited;
}
/**
* {@inheritDoc}
*/
@Override
protected IntegerPropertyDefinition buildInstance(AbstractManagedObjectDefinition, ?> d, String propertyName,
EnumSetnull if there is no upper
* limit.
*/
public Integer getUpperLimit() {
return upperLimit;
}
/**
* Gets the optional unit synopsis of this integer property definition in
* the default locale.
*
* @return Returns the unit synopsis of this integer property definition in
* the default locale, or null if there is no unit
* synopsis.
*/
public LocalizableMessage getUnitSynopsis() {
return getUnitSynopsis(Locale.getDefault());
}
/**
* Gets the optional unit synopsis of this integer property definition in
* the specified locale.
*
* @param locale
* The locale.
* @return Returns the unit synopsis of this integer property definition in
* the specified locale, or null if there is no unit
* synopsis.
*/
public LocalizableMessage getUnitSynopsis(Locale locale) {
ManagedObjectDefinitionI18NResource resource = ManagedObjectDefinitionI18NResource.getInstance();
String property = "property." + getName() + ".syntax.integer.unit-synopsis";
try {
return resource.getMessage(getManagedObjectDefinition(), property, locale);
} catch (MissingResourceException e) {
return null;
}
}
/**
* Determine whether this property allows unlimited values.
*
* @return Returns true if this this property allows unlimited
* values.
*/
public boolean isAllowUnlimited() {
return allowUnlimited;
}
/**
* {@inheritDoc}
*/
@Override
public void validateValue(Integer value) throws IllegalPropertyValueException {
ensureNotNull(value);
if (!allowUnlimited && value < lowerLimit) {
throw new IllegalPropertyValueException(this, value);
// unlimited allowed
} else if (value >= 0 && value < lowerLimit) {
throw new IllegalPropertyValueException(this, value);
}
if ((upperLimit != null) && (value > upperLimit)) {
throw new IllegalPropertyValueException(this, value);
}
}
/**
* {@inheritDoc}
*/
@Override
public String encodeValue(Integer value) throws IllegalPropertyValueException {
ensureNotNull(value);
// Make sure that we correctly encode negative values as "unlimited".
if (allowUnlimited) {
if (value < 0) {
return UNLIMITED;
}
}
return value.toString();
}
/**
* {@inheritDoc}
*/
@Override
public Integer decodeValue(String value) throws IllegalPropertyValueStringException {
ensureNotNull(value);
if (allowUnlimited) {
if (value.trim().equalsIgnoreCase(UNLIMITED)) {
return -1;
}
}
Integer i;
try {
i = Integer.valueOf(value);
} catch (NumberFormatException e) {
throw new IllegalPropertyValueStringException(this, value);
}
try {
validateValue(i);
} catch (IllegalPropertyValueException e) {
throw new IllegalPropertyValueStringException(this, value);
}
return i;
}
/**
* {@inheritDoc}
*/
@Override
public