1008N/A/*
1008N/A * CDDL HEADER START
1008N/A *
1008N/A * The contents of this file are subject to the terms of the
1008N/A * Common Development and Distribution License, Version 1.0 only
1008N/A * (the "License"). You may not use this file except in compliance
1008N/A * with the License.
1008N/A *
1008N/A * You can obtain a copy of the license at
1008N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE
1008N/A * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
1008N/A * See the License for the specific language governing permissions
1008N/A * and limitations under the License.
1008N/A *
1008N/A * When distributing Covered Code, include this CDDL HEADER in each
1008N/A * file and include the License file at
1008N/A * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
1008N/A * add the following below this CDDL HEADER, with the fields enclosed
1008N/A * by brackets "[]" replaced with your own identifying information:
1008N/A * Portions Copyright [yyyy] [name of copyright owner]
1008N/A *
1008N/A * CDDL HEADER END
1008N/A *
1008N/A *
3215N/A * Copyright 2008 Sun Microsystems, Inc.
1008N/A */
1008N/A
1008N/Apackage org.opends.server.admin;
2086N/Aimport org.opends.messages.Message;
1008N/A
1008N/A
1008N/A
1008N/Aimport static org.opends.server.util.Validator.ensureNotNull;
1008N/A
1008N/Aimport java.util.EnumSet;
1008N/Aimport java.util.HashMap;
1140N/Aimport java.util.Locale;
1008N/Aimport java.util.Map;
1140N/Aimport java.util.MissingResourceException;
1008N/A
1008N/A
1008N/A
1008N/A/**
1008N/A * Enumeration property definition.
1008N/A *
1008N/A * @param <E>
1008N/A * The enumeration that should be used for values of this
1008N/A * property definition.
1008N/A */
1008N/Apublic final class EnumPropertyDefinition<E extends Enum<E>> extends
1470N/A PropertyDefinition<E> {
1008N/A
1008N/A /**
1008N/A * An interface for incrementally constructing enumeration property
1008N/A * definitions.
1008N/A *
1008N/A * @param <E>
1008N/A * The enumeration that should be used for values of this
1008N/A * property definition.
1008N/A */
1008N/A public static class Builder<E extends Enum<E>> extends
1008N/A AbstractBuilder<E, EnumPropertyDefinition<E>> {
1008N/A
1008N/A // The enumeration class.
1008N/A private Class<E> enumClass;
1008N/A
1008N/A
1008N/A
1008N/A // Private constructor
1140N/A private Builder(
1140N/A AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
1140N/A super(d, propertyName);
1008N/A this.enumClass = null;
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * Set the enumeration class which should be used for values of
1008N/A * this property definition.
1008N/A *
1008N/A * @param enumClass
1008N/A * The enumeration class which should be used for values
1008N/A * of this property definition.
1008N/A */
1008N/A public final void setEnumClass(Class<E> enumClass) {
1008N/A this.enumClass = enumClass;
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1008N/A protected EnumPropertyDefinition<E> buildInstance(
1140N/A AbstractManagedObjectDefinition<?, ?> d, String propertyName,
1140N/A EnumSet<PropertyOption> options,
1565N/A AdministratorAction adminAction,
1008N/A DefaultBehaviorProvider<E> defaultBehavior) {
1008N/A // Make sure that the enumeration class has been defined.
1008N/A if (enumClass == null) {
1008N/A throw new IllegalStateException("Enumeration class undefined");
1008N/A }
1008N/A
1140N/A return new EnumPropertyDefinition<E>(d, propertyName, options,
1565N/A adminAction, defaultBehavior, enumClass);
1008N/A }
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * Create an enumeration property definition builder.
1008N/A *
1008N/A * @param <E>
1008N/A * The enumeration that should be used for values of this
1008N/A * property definition.
1140N/A * @param d
1140N/A * The managed object definition associated with this
1140N/A * property definition.
1008N/A * @param propertyName
1008N/A * The property name.
1008N/A * @return Returns the new enumeration property definition builder.
1008N/A */
1008N/A public static <E extends Enum<E>> Builder<E> createBuilder(
1140N/A AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
1140N/A return new Builder<E>(d, propertyName);
1008N/A }
1008N/A
1008N/A // The enumeration class.
1008N/A private final Class<E> enumClass;
1008N/A
1008N/A // Map used for decoding values.
1008N/A private final Map<String, E> decodeMap;
1008N/A
1008N/A
1008N/A
1008N/A // Private constructor.
1565N/A private EnumPropertyDefinition(AbstractManagedObjectDefinition<?, ?> d,
1565N/A String propertyName, EnumSet<PropertyOption> options,
1565N/A AdministratorAction adminAction,
1008N/A DefaultBehaviorProvider<E> defaultBehavior, Class<E> enumClass) {
1565N/A super(d, enumClass, propertyName, options, adminAction, defaultBehavior);
1008N/A this.enumClass = enumClass;
1008N/A
1008N/A // Initialize the decoding map.
1008N/A this.decodeMap = new HashMap<String, E>();
1008N/A for (E value : EnumSet.<E> allOf(enumClass)) {
1008N/A String s = value.toString().trim().toLowerCase();
1008N/A this.decodeMap.put(s, value);
1008N/A }
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1008N/A public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
1008N/A return v.visitEnum(this, p);
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1470N/A public <R, P> R accept(PropertyValueVisitor<R, P> v, E value, P p) {
1470N/A return v.visitEnum(this, value, p);
1470N/A }
1470N/A
1470N/A
1470N/A
1470N/A /**
1470N/A * {@inheritDoc}
1470N/A */
1470N/A @Override
1008N/A public E decodeValue(String value)
1008N/A throws IllegalPropertyValueStringException {
1008N/A ensureNotNull(value);
1008N/A
1008N/A String nvalue = value.trim().toLowerCase();
1008N/A E eValue = decodeMap.get(nvalue);
1008N/A if (eValue == null) {
1008N/A throw new IllegalPropertyValueStringException(this, value);
1008N/A } else {
1008N/A return eValue;
1008N/A }
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * Get the enumeration class used for values of this property.
1008N/A *
1008N/A * @return Returns the enumeration class used for values of this
1008N/A * property.
1008N/A */
1008N/A public Class<E> getEnumClass() {
1008N/A return enumClass;
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1140N/A * Gets the synopsis of the specified enumeration value of this
1140N/A * enumeration property definition in the default locale.
1140N/A *
1140N/A * @param value
1140N/A * The enumeration value.
1140N/A * @return Returns the synopsis of the specified enumeration value
1140N/A * of this enumeration property definition in the default
1140N/A * locale.
1140N/A */
2086N/A public final Message getValueSynopsis(E value) {
1140N/A return getValueSynopsis(Locale.getDefault(), value);
1140N/A }
1140N/A
1140N/A
1140N/A
1140N/A /**
1140N/A * Gets the synopsis of the specified enumeration value of this
1140N/A * enumeration property definition in the specified locale.
1140N/A *
1140N/A * @param value
1140N/A * The enumeration value.
1140N/A * @param locale
1140N/A * The locale.
1140N/A * @return Returns the synopsis of the specified enumeration value
1140N/A * of this enumeration property definition in the specified
1140N/A * locale.
1140N/A */
2086N/A public final Message getValueSynopsis(Locale locale, E value) {
1140N/A ManagedObjectDefinitionI18NResource resource =
1140N/A ManagedObjectDefinitionI18NResource.getInstance();
1140N/A String property = "property." + getName()
1140N/A + ".syntax.enumeration.value." + value.toString()
1140N/A + ".synopsis";
1140N/A try {
1140N/A return resource.getMessage(getManagedObjectDefinition(),
1140N/A property, locale);
1140N/A } catch (MissingResourceException e) {
1140N/A return null;
1140N/A }
1140N/A }
1140N/A
1140N/A
1140N/A
1140N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1008N/A public String normalizeValue(E value)
1008N/A throws IllegalPropertyValueException {
1008N/A ensureNotNull(value);
1008N/A
1008N/A return value.toString().trim().toLowerCase();
1008N/A }
1008N/A
1008N/A
1008N/A
1008N/A /**
1008N/A * {@inheritDoc}
1008N/A */
1008N/A @Override
1008N/A public void validateValue(E value)
1008N/A throws IllegalPropertyValueException {
1008N/A ensureNotNull(value);
1008N/A
1008N/A // No additional validation required.
1008N/A }
1008N/A}