>();
}
};
// Object class attribute description.
private static final ZeroOptionImpl ZERO_OPTION_IMPL = new ZeroOptionImpl();
private static final AttributeDescription OBJECT_CLASS;
static {
final AttributeType attributeType = Schema.getCoreSchema().getAttributeType("2.5.4.0");
OBJECT_CLASS =
new AttributeDescription(attributeType.getNameOrOID(), attributeType,
ZERO_OPTION_IMPL);
}
// This is the size of the per-thread per-schema attribute description
// cache. We should be conservative here in case there are many
// threads.
private static final int ATTRIBUTE_DESCRIPTION_CACHE_SIZE = 512;
/**
* Returns an attribute description having the same attribute type and
* options as this attribute description as well as the provided option.
*
* @param option
* The attribute option.
* @return The new attribute description containing {@code option}.
* @throws NullPointerException
* If {@code attributeDescription} or {@code option} was
* {@code null}.
*/
public AttributeDescription withOption(final String option) {
Validator.ensureNotNull(option);
final String normalizedOption = toLowerCase(option);
if (pimpl.containsOption(normalizedOption)) {
return this;
}
final String oldAttributeDescription = attributeDescription;
final StringBuilder builder =
new StringBuilder(oldAttributeDescription.length() + option.length() + 1);
builder.append(oldAttributeDescription);
builder.append(';');
builder.append(option);
final String newAttributeDescription = builder.toString();
final Impl impl = pimpl;
if (impl instanceof ZeroOptionImpl) {
return new AttributeDescription(newAttributeDescription, attributeType,
new SingleOptionImpl(option, normalizedOption));
} else if (impl instanceof SingleOptionImpl) {
final SingleOptionImpl simpl = (SingleOptionImpl) impl;
final String[] newOptions = new String[2];
newOptions[0] = simpl.option;
newOptions[1] = option;
final String[] newNormalizedOptions = new String[2];
if (normalizedOption.compareTo(simpl.normalizedOption) < 0) {
newNormalizedOptions[0] = normalizedOption;
newNormalizedOptions[1] = simpl.normalizedOption;
} else {
newNormalizedOptions[0] = simpl.normalizedOption;
newNormalizedOptions[1] = normalizedOption;
}
return new AttributeDescription(newAttributeDescription, attributeType,
new MultiOptionImpl(newOptions, newNormalizedOptions));
} else {
final MultiOptionImpl mimpl = (MultiOptionImpl) impl;
final int sz1 = mimpl.options.length;
final String[] newOptions = new String[sz1 + 1];
for (int i = 0; i < sz1; i++) {
newOptions[i] = mimpl.options[i];
}
newOptions[sz1] = option;
final int sz2 = mimpl.normalizedOptions.length;
final String[] newNormalizedOptions = new String[sz2 + 1];
boolean inserted = false;
for (int i = 0; i < sz2; i++) {
if (!inserted) {
final String s = mimpl.normalizedOptions[i];
if (normalizedOption.compareTo(s) < 0) {
newNormalizedOptions[i] = normalizedOption;
newNormalizedOptions[i + 1] = s;
inserted = true;
} else {
newNormalizedOptions[i] = s;
}
} else {
newNormalizedOptions[i + 1] = mimpl.normalizedOptions[i];
}
}
if (!inserted) {
newNormalizedOptions[sz2] = normalizedOption;
}
return new AttributeDescription(newAttributeDescription, attributeType,
new MultiOptionImpl(newOptions, newNormalizedOptions));
}
}
/**
* Returns an attribute description having the same attribute type and
* options as this attribute description except for the provided option.
*
* This method is idempotent: if this attribute description does not contain
* the provided option then this attribute description will be returned.
*
* @param option
* The attribute option.
* @return The new attribute description excluding {@code option}.
* @throws NullPointerException
* If {@code attributeDescription} or {@code option} was
* {@code null}.
*/
public AttributeDescription withoutOption(final String option) {
Validator.ensureNotNull(option);
final String normalizedOption = toLowerCase(option);
if (!pimpl.containsOption(normalizedOption)) {
return this;
}
final String oldAttributeDescription = attributeDescription;
final StringBuilder builder =
new StringBuilder(oldAttributeDescription.length() - option.length() - 1);
final String normalizedOldAttributeDescription = toLowerCase(oldAttributeDescription);
final int index =
normalizedOldAttributeDescription.indexOf(normalizedOption);
builder.append(oldAttributeDescription, 0, index - 1 /* to semi-colon */);
builder.append(oldAttributeDescription, index + option.length(), oldAttributeDescription
.length());
final String newAttributeDescription = builder.toString();
final Impl impl = pimpl;
if (impl instanceof ZeroOptionImpl) {
throw new IllegalStateException("ZeroOptionImpl unexpected");
} else if (impl instanceof SingleOptionImpl) {
return new AttributeDescription(newAttributeDescription, attributeType,
ZERO_OPTION_IMPL);
} else {
final MultiOptionImpl mimpl = (MultiOptionImpl) impl;
if (mimpl.options.length == 2) {
final String remainingOption;
final String remainingNormalizedOption;
if (toLowerCase(mimpl.options[0]).equals(normalizedOption)) {
remainingOption = mimpl.options[1];
} else {
remainingOption = mimpl.options[0];
}
if (mimpl.normalizedOptions[0].equals(normalizedOption)) {
remainingNormalizedOption = mimpl.normalizedOptions[1];
} else {
remainingNormalizedOption = mimpl.normalizedOptions[0];
}
return new AttributeDescription(newAttributeDescription, attributeType,
new SingleOptionImpl(remainingOption, remainingNormalizedOption));
} else {
final String[] newOptions = new String[mimpl.options.length - 1];
final String[] newNormalizedOptions =
new String[mimpl.normalizedOptions.length - 1];
for (int i = 0, j = 0; i < mimpl.options.length; i++) {
if (!toLowerCase(mimpl.options[i]).equals(normalizedOption)) {
newOptions[j++] = mimpl.options[i];
}
}
for (int i = 0, j = 0; i < mimpl.normalizedOptions.length; i++) {
if (!mimpl.normalizedOptions[i].equals(normalizedOption)) {
newNormalizedOptions[j++] = mimpl.normalizedOptions[i];
}
}
return new AttributeDescription(newAttributeDescription, attributeType,
new MultiOptionImpl(newOptions, newNormalizedOptions));
}
}
}
/**
* Creates an attribute description having the provided attribute type and
* no options.
*
* @param attributeType
* The attribute type.
* @return The attribute description.
* @throws NullPointerException
* If {@code attributeType} was {@code null}.
*/
public static AttributeDescription create(final AttributeType attributeType) {
Validator.ensureNotNull(attributeType);
// Use object identity in case attribute type does not come from
// core schema.
if (attributeType == OBJECT_CLASS.getAttributeType()) {
return OBJECT_CLASS;
} else {
return new AttributeDescription(attributeType.getNameOrOID(), attributeType,
ZERO_OPTION_IMPL);
}
}
/**
* Creates an attribute description having the provided attribute type and
* single option.
*
* @param attributeType
* The attribute type.
* @param option
* The attribute option.
* @return The attribute description.
* @throws NullPointerException
* If {@code attributeType} or {@code option} was {@code null}.
*/
public static AttributeDescription create(final AttributeType attributeType, final String option) {
Validator.ensureNotNull(attributeType, option);
final String oid = attributeType.getNameOrOID();
final StringBuilder builder = new StringBuilder(oid.length() + option.length() + 1);
builder.append(oid);
builder.append(';');
builder.append(option);
final String attributeDescription = builder.toString();
final String normalizedOption = toLowerCase(option);
return new AttributeDescription(attributeDescription, attributeType, new SingleOptionImpl(
option, normalizedOption));
}
/**
* Creates an attribute description having the provided attribute type and
* options.
*
* @param attributeType
* The attribute type.
* @param options
* The attribute options.
* @return The attribute description.
* @throws NullPointerException
* If {@code attributeType} or {@code options} was {@code null}.
*/
public static AttributeDescription create(final AttributeType attributeType,
final String... options) {
Validator.ensureNotNull(attributeType, options);
switch (options.length) {
case 0:
return create(attributeType);
case 1:
return create(attributeType, options[0]);
default:
final String[] optionsList = new String[options.length];
final String[] normalizedOptions = new String[options.length];
final String oid = attributeType.getNameOrOID();
final StringBuilder builder =
new StringBuilder(oid.length() + options[0].length() + options[1].length() + 2);
builder.append(oid);
int i = 0;
for (final String option : options) {
builder.append(';');
builder.append(option);
optionsList[i] = option;
final String normalizedOption = toLowerCase(option);
normalizedOptions[i++] = normalizedOption;
}
Arrays.sort(normalizedOptions);
final String attributeDescription = builder.toString();
return new AttributeDescription(attributeDescription, attributeType,
new MultiOptionImpl(optionsList, normalizedOptions));
}
}
/**
* Returns an attribute description representing the object class attribute
* type with no options.
*
* @return The object class attribute description.
*/
public static AttributeDescription objectClass() {
return OBJECT_CLASS;
}
/**
* Parses the provided LDAP string representation of an attribute
* description using the default schema.
*
* @param attributeDescription
* The LDAP string representation of an attribute description.
* @return The parsed attribute description.
* @throws UnknownSchemaElementException
* If {@code attributeDescription} contains an attribute type
* which is not contained in the default schema and the schema
* is strict.
* @throws LocalizedIllegalArgumentException
* If {@code attributeDescription} is not a valid LDAP string
* representation of an attribute description.
* @throws NullPointerException
* If {@code attributeDescription} was {@code null}.
*/
public static AttributeDescription valueOf(final String attributeDescription) {
return valueOf(attributeDescription, Schema.getDefaultSchema());
}
/**
* Parses the provided LDAP string representation of an attribute
* description using the provided schema.
*
* @param attributeDescription
* The LDAP string representation of an attribute description.
* @param schema
* The schema to use when parsing the attribute description.
* @return The parsed attribute description.
* @throws UnknownSchemaElementException
* If {@code attributeDescription} contains an attribute type
* which is not contained in the provided schema and the schema
* is strict.
* @throws LocalizedIllegalArgumentException
* If {@code attributeDescription} is not a valid LDAP string
* representation of an attribute description.
* @throws NullPointerException
* If {@code attributeDescription} or {@code schema} was
* {@code null}.
*/
@SuppressWarnings("serial")
public static AttributeDescription valueOf(final String attributeDescription,
final Schema schema) {
Validator.ensureNotNull(attributeDescription, schema);
// First look up the attribute description in the cache.
final WeakHashMap> threadLocalMap = CACHE.get();
Map schemaLocalMap = threadLocalMap.get(schema);
AttributeDescription ad = null;
if (schemaLocalMap == null) {
schemaLocalMap =
new LinkedHashMap(
ATTRIBUTE_DESCRIPTION_CACHE_SIZE, 0.75f, true) {
@Override
protected boolean removeEldestEntry(
final Map.Entry eldest) {
return size() > ATTRIBUTE_DESCRIPTION_CACHE_SIZE;
}
};
threadLocalMap.put(schema, schemaLocalMap);
} else {
ad = schemaLocalMap.get(attributeDescription);
}
// Cache miss: decode and cache.
if (ad == null) {
ad = valueOf0(attributeDescription, schema);
schemaLocalMap.put(attributeDescription, ad);
}
return ad;
}
private static int skipTrailingWhiteSpace(final String attributeDescription, int i,
final int length) {
char c;
while (i < length) {
c = attributeDescription.charAt(i);
if (c != ' ') {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_INTERNAL_WHITESPACE.get(attributeDescription);
throw new LocalizedIllegalArgumentException(message);
}
i++;
}
return i;
}
// Uncached valueOf implementation.
private static AttributeDescription valueOf0(final String attributeDescription,
final Schema schema) {
final boolean allowMalformedNamesAndOptions = schema.allowMalformedNamesAndOptions();
int i = 0;
final int length = attributeDescription.length();
char c = 0;
// Skip leading white space.
while (i < length) {
c = attributeDescription.charAt(i);
if (c != ' ') {
break;
}
i++;
}
// If we're already at the end then the attribute description only
// contained whitespace.
if (i == length) {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_EMPTY.get(attributeDescription);
throw new LocalizedIllegalArgumentException(message);
}
// Validate the first non-whitespace character.
ASCIICharProp cp = ASCIICharProp.valueOf(c);
if (cp == null) {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_ILLEGAL_CHARACTER.get(attributeDescription, c, i);
throw new LocalizedIllegalArgumentException(message);
}
// Mark the attribute type start position.
final int attributeTypeStart = i;
if (cp.isLetter()) {
// Non-numeric OID: letter + zero or more keychars.
i++;
while (i < length) {
c = attributeDescription.charAt(i);
if (c == ';' || c == ' ') {
break;
}
cp = ASCIICharProp.valueOf(c);
if (!cp.isKeyChar(allowMalformedNamesAndOptions)) {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_ILLEGAL_CHARACTER.get(attributeDescription,
c, i);
throw new LocalizedIllegalArgumentException(message);
}
i++;
}
// (charAt(i) == ';' || c == ' ' || i == length)
} else if (cp.isDigit()) {
// Numeric OID: decimal digit + zero or more dots or decimals.
i++;
while (i < length) {
c = attributeDescription.charAt(i);
if (c == ';' || c == ' ') {
break;
}
cp = ASCIICharProp.valueOf(c);
if (c != '.' && !cp.isDigit()) {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_ILLEGAL_CHARACTER.get(attributeDescription,
c, i);
throw new LocalizedIllegalArgumentException(message);
}
i++;
}
// (charAt(i) == ';' || charAt(i) == ' ' || i == length)
} else {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_ILLEGAL_CHARACTER.get(attributeDescription, c, i);
throw new LocalizedIllegalArgumentException(message);
}
// Skip trailing white space.
final int attributeTypeEnd = i;
if (c == ' ') {
i = skipTrailingWhiteSpace(attributeDescription, i + 1, length);
}
// Determine the portion of the string containing the attribute type
// name.
String oid;
if (attributeTypeStart == 0 && attributeTypeEnd == length) {
oid = attributeDescription;
} else {
oid = attributeDescription.substring(attributeTypeStart, attributeTypeEnd);
}
if (oid.length() == 0) {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_NO_TYPE.get(attributeDescription);
throw new LocalizedIllegalArgumentException(message);
}
// Get the attribute type from the schema.
final AttributeType attributeType = schema.getAttributeType(oid);
// If we're already at the end of the attribute description then it
// does not contain any options.
if (i == length) {
// Use object identity in case attribute type does not come from
// core schema.
if (attributeType == OBJECT_CLASS.getAttributeType()
&& attributeDescription.equals(OBJECT_CLASS.toString())) {
return OBJECT_CLASS;
} else {
return new AttributeDescription(attributeDescription, attributeType,
ZERO_OPTION_IMPL);
}
}
// At this point 'i' must point at a semi-colon.
i++;
StringBuilder builder = null;
int optionStart = i;
while (i < length) {
c = attributeDescription.charAt(i);
if (c == ' ' || c == ';') {
break;
}
cp = ASCIICharProp.valueOf(c);
if (!cp.isKeyChar(allowMalformedNamesAndOptions)) {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_ILLEGAL_CHARACTER.get(attributeDescription, c, i);
throw new LocalizedIllegalArgumentException(message);
}
if (builder == null) {
if (cp.isUpperCase()) {
// Need to normalize the option.
builder = new StringBuilder(length - optionStart);
builder.append(attributeDescription, optionStart, i);
builder.append(cp.toLowerCase());
}
} else {
builder.append(cp.toLowerCase());
}
i++;
}
String option = attributeDescription.substring(optionStart, i);
String normalizedOption;
if (builder != null) {
normalizedOption = builder.toString();
} else {
normalizedOption = option;
}
if (option.length() == 0) {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_EMPTY_OPTION.get(attributeDescription);
throw new LocalizedIllegalArgumentException(message);
}
// Skip trailing white space.
if (c == ' ') {
i = skipTrailingWhiteSpace(attributeDescription, i + 1, length);
}
// If we're already at the end of the attribute description then it
// only contains a single option.
if (i == length) {
return new AttributeDescription(attributeDescription, attributeType,
new SingleOptionImpl(option, normalizedOption));
}
// Multiple options need sorting and duplicates removed - we could
// optimize a bit further here for 2 option attribute descriptions.
final List options = new LinkedList();
options.add(option);
final SortedSet normalizedOptions = new TreeSet();
normalizedOptions.add(normalizedOption);
while (i < length) {
// At this point 'i' must point at a semi-colon.
i++;
builder = null;
optionStart = i;
while (i < length) {
c = attributeDescription.charAt(i);
if (c == ' ' || c == ';') {
break;
}
cp = ASCIICharProp.valueOf(c);
if (!cp.isKeyChar(allowMalformedNamesAndOptions)) {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_ILLEGAL_CHARACTER.get(attributeDescription,
c, i);
throw new LocalizedIllegalArgumentException(message);
}
if (builder == null) {
if (cp.isUpperCase()) {
// Need to normalize the option.
builder = new StringBuilder(length - optionStart);
builder.append(attributeDescription, optionStart, i);
builder.append(cp.toLowerCase());
}
} else {
builder.append(cp.toLowerCase());
}
i++;
}
option = attributeDescription.substring(optionStart, i);
if (builder != null) {
normalizedOption = builder.toString();
} else {
normalizedOption = option;
}
if (option.length() == 0) {
final LocalizableMessage message =
ERR_ATTRIBUTE_DESCRIPTION_EMPTY_OPTION.get(attributeDescription);
throw new LocalizedIllegalArgumentException(message);
}
// Skip trailing white space.
if (c == ' ') {
i = skipTrailingWhiteSpace(attributeDescription, i + 1, length);
}
options.add(option);
normalizedOptions.add(normalizedOption);
}
return new AttributeDescription(attributeDescription, attributeType, new MultiOptionImpl(
options.toArray(new String[options.size()]), normalizedOptions
.toArray(new String[normalizedOptions.size()])));
}
private final String attributeDescription;
private final AttributeType attributeType;
private final Impl pimpl;
// Private constructor.
private AttributeDescription(final String attributeDescription,
final AttributeType attributeType, final Impl pimpl) {
this.attributeDescription = attributeDescription;
this.attributeType = attributeType;
this.pimpl = pimpl;
}
/**
* Compares this attribute description to the provided attribute
* description. The attribute types are compared first and then, if equal,
* the options are normalized, sorted, and compared.
*
* @param other
* The attribute description to be compared.
* @return A negative integer, zero, or a positive integer as this attribute
* description is less than, equal to, or greater than the specified
* attribute description.
* @throws NullPointerException
* If {@code name} was {@code null}.
*/
public int compareTo(final AttributeDescription other) {
final int result = attributeType.compareTo(other.attributeType);
if (result != 0) {
return result;
} else {
// Attribute type is the same, so compare options.
return pimpl.compareTo(other.pimpl);
}
}
/**
* Indicates whether or not this attribute description contains the provided
* option.
*
* @param option
* The option for which to make the determination.
* @return {@code true} if this attribute description has the provided
* option, or {@code false} if not.
* @throws NullPointerException
* If {@code option} was {@code null}.
*/
public boolean containsOption(final String option) {
final String normalizedOption = toLowerCase(option);
return pimpl.containsOption(normalizedOption);
}
/**
* Indicates whether the provided object is an attribute description which
* is equal to this attribute description. It will be considered equal if
* the attribute type and normalized sorted list of options are identical.
*
* @param o
* The object for which to make the determination.
* @return {@code true} if the provided object is an attribute description
* that is equal to this attribute description, or {@code false} if
* not.
*/
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof AttributeDescription)) {
return false;
}
final AttributeDescription other = (AttributeDescription) o;
if (!attributeType.equals(other.attributeType)) {
return false;
}
// Attribute type is the same, compare options.
return pimpl.equals(other.pimpl);
}
/**
* Returns the attribute type associated with this attribute description.
*
* @return The attribute type associated with this attribute description.
*/
public AttributeType getAttributeType() {
return attributeType;
}
/**
* Returns an {@code Iterable} containing the options contained in this
* attribute description. Attempts to remove options using an iterator's
* {@code remove()} method are not permitted and will result in an
* {@code UnsupportedOperationException} being thrown.
*
* @return An {@code Iterable} containing the options.
*/
public Iterable getOptions() {
return pimpl;
}
/**
* Returns the hash code for this attribute description. It will be
* calculated as the sum of the hash codes of the attribute type and
* normalized sorted list of options.
*
* @return The hash code for this attribute description.
*/
@Override
public int hashCode() {
// FIXME: should we cache this?
return attributeType.hashCode() * 31 + pimpl.hashCode();
}
/**
* Indicates whether or not this attribute description has any options.
*
* @return {@code true} if this attribute description has any options, or
* {@code false} if not.
*/
public boolean hasOptions() {
return pimpl.hasOptions();
}
/**
* Indicates whether or not this attribute description is the
* {@code objectClass} attribute description with no options.
*
* @return {@code true} if this attribute description is the
* {@code objectClass} attribute description with no options, or
* {@code false} if not.
*/
public boolean isObjectClass() {
return attributeType.isObjectClass() && !hasOptions();
}
/**
* Indicates whether or not this attribute description is a sub-type of the
* provided attribute description as defined in RFC 4512 section 2.5.
* Specifically, this method will return {@code true} if and only if the
* following conditions are both {@code true}:
*
* - This attribute description has an attribute type which is equal to,
* or is a sub-type of, the attribute type in the provided attribute
* description.
*
- This attribute description contains all of the options contained in
* the provided attribute description.
*
* Note that this method will return {@code true} if this attribute
* description is equal to the provided attribute description.
*
* @param other
* The attribute description for which to make the determination.
* @return {@code true} if this attribute description is a sub-type of the
* provided attribute description, or {@code false} if not.
* @throws NullPointerException
* If {@code name} was {@code null}.
*/
public boolean isSubTypeOf(final AttributeDescription other) {
if (!attributeType.isSubTypeOf(other.attributeType)) {
return false;
} else {
return pimpl.isSubTypeOf(other.pimpl);
}
}
/**
* Indicates whether or not this attribute description is a super-type of
* the provided attribute description as defined in RFC 4512 section 2.5.
* Specifically, this method will return {@code true} if and only if the
* following conditions are both {@code true}:
*
* - This attribute description has an attribute type which is equal to,
* or is a super-type of, the attribute type in the provided attribute
* description.
*
- This attribute description contains a sub-set of the options
* contained in the provided attribute description.
*
* Note that this method will return {@code true} if this attribute
* description is equal to the provided attribute description.
*
* @param other
* The attribute description for which to make the determination.
* @return {@code true} if this attribute description is a super-type of the
* provided attribute description, or {@code false} if not.
* @throws NullPointerException
* If {@code name} was {@code null}.
*/
public boolean isSuperTypeOf(final AttributeDescription other) {
if (!other.attributeType.isSubTypeOf(attributeType)) {
return false;
} else {
return pimpl.isSuperTypeOf(other.pimpl);
}
}
/**
* Returns the string representation of this attribute description as
* defined in RFC4512 section 2.5.
*
* @return The string representation of this attribute description.
*/
@Override
public String toString() {
return attributeDescription;
}
}