/* * The contents of this file are subject to the terms of the Common Development and * Distribution License (the License). You may not use this file except in compliance with the * License. * * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the * specific language governing permission and limitations under the License. * * When distributing Covered Software, include this CDDL Header Notice in each file and include * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL * Header, with the fields enclosed by brackets [] replaced by your own identifying * information: "Portions Copyright [year] [name of copyright owner]". * * Copyright 2008 Sun Microsystems, Inc. * Portions Copyright 2016 ForgeRock AS. */ package org.forgerock.opendj.config; import org.forgerock.util.Reject; import java.util.EnumSet; /** * Memory size property definition. *
* All memory size property values are represented in bytes using longs. *
* All values must be zero or positive and within the lower/upper limit
* constraints. Support is provided for "unlimited" memory sizes. These are
* represented using a negative memory size value or using the string
* "unlimited".
*/
public final class SizePropertyDefinition extends PropertyDefinitionnull if there
* is no upper limit.
* @throws IllegalArgumentException
* If the lower limit is greater than the upper limit.
*/
public final void setUpperLimit(Long upperLimit) {
if (upperLimit != null) {
if (upperLimit < 0) {
throw new IllegalArgumentException("Negative upper limit");
}
if (lowerLimit > upperLimit) {
throw new IllegalArgumentException("Lower limit greater than upper limit");
}
}
this.upperLimit = upperLimit;
}
/**
* Set the upper limit using a string representation of the limit.
*
* @param upperLimit
* The string representation of the new upper limit, or
* null if there is no upper limit.
* @throws IllegalArgumentException
* If the upper limit could not be parsed, or if the lower
* limit is greater than the upper limit.
*/
public final void setUpperLimit(String upperLimit) {
setUpperLimit(upperLimit != null ? SizeUnit.parseValue(upperLimit, SizeUnit.BYTES) : null);
}
/**
* Specify whether 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;
}
@Override
protected SizePropertyDefinition buildInstance(AbstractManagedObjectDefinition, ?> d, String propertyName,
EnumSetnull if there is
* no upper limit.
*/
public Long getUpperLimit() {
return upperLimit;
}
/**
* Determine whether this property allows unlimited memory sizes.
*
* @return Returns true if this this property allows unlimited
* memory sizes.
*/
public boolean isAllowUnlimited() {
return allowUnlimited;
}
@Override
public void validateValue(Long value) {
Reject.ifNull(value);
if (!allowUnlimited && value < lowerLimit) {
throw PropertyException.illegalPropertyValueException(this, value);
// unlimited allowed
} else if (value >= 0 && value < lowerLimit) {
throw PropertyException.illegalPropertyValueException(this, value);
}
if (upperLimit != null && value > upperLimit) {
throw PropertyException.illegalPropertyValueException(this, value);
}
}
@Override
public String encodeValue(Long value) {
Reject.ifNull(value);
// Make sure that we correctly encode negative values as "unlimited".
if (allowUnlimited && value < 0) {
return UNLIMITED;
}
// Encode the size value using the best-fit unit.
StringBuilder builder = new StringBuilder();
SizeUnit unit = SizeUnit.getBestFitUnitExact(value);
// Cast to a long to remove fractional part (which should not be there
// anyway as the best-fit unit should result in an exact conversion).
builder.append((long) unit.fromBytes(value));
builder.append(' ');
builder.append(unit);
return builder.toString();
}
@Override
public Long decodeValue(String value) {
Reject.ifNull(value);
// First check for the special "unlimited" value when necessary.
if (allowUnlimited && UNLIMITED.equalsIgnoreCase(value.trim())) {
return -1L;
}
// Decode the value.
Long i;
try {
i = SizeUnit.parseValue(value, SizeUnit.BYTES);
} catch (NumberFormatException e) {
throw PropertyException.illegalPropertyValueException(this, value);
}
try {
validateValue(i);
} catch (PropertyException e) {
throw PropertyException.illegalPropertyValueException(this, value);
}
return i;
}
@Override
public