001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.server.admin;
029    
030    
031    
032    import static org.opends.server.util.Validator.ensureNotNull;
033    
034    import java.util.EnumSet;
035    import java.util.HashMap;
036    import java.util.Map;
037    
038    
039    
040    /**
041     * Boolean property definition.
042     */
043    public final class BooleanPropertyDefinition extends
044        PropertyDefinition<Boolean> {
045    
046      /**
047       * Mapping used for parsing boolean values. This mapping is more flexible than
048       * the standard boolean string parser and supports common true/false synonyms
049       * used in configuration.
050       */
051      private static final Map<String, Boolean> VALUE_MAP;
052      static {
053        VALUE_MAP = new HashMap<String, Boolean>();
054    
055        // We could have more possibilities but decided against in issue 1960.
056        VALUE_MAP.put("false", Boolean.FALSE);
057        VALUE_MAP.put("true", Boolean.TRUE);
058      }
059    
060    
061    
062      /**
063       * An interface for incrementally constructing boolean property definitions.
064       */
065      public static class Builder extends
066          AbstractBuilder<Boolean, BooleanPropertyDefinition> {
067    
068        // Private constructor
069        private Builder(
070            AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
071          super(d, propertyName);
072        }
073    
074    
075    
076        /**
077         * {@inheritDoc}
078         */
079        @Override
080        protected BooleanPropertyDefinition buildInstance(
081            AbstractManagedObjectDefinition<?, ?> d, String propertyName,
082            EnumSet<PropertyOption> options,
083            AdministratorAction adminAction,
084            DefaultBehaviorProvider<Boolean> defaultBehavior) {
085          return new BooleanPropertyDefinition(d, propertyName, options,
086              adminAction, defaultBehavior);
087        }
088    
089      }
090    
091    
092    
093      /**
094       * Create a boolean property definition builder.
095       *
096       * @param d
097       *          The managed object definition associated with this
098       *          property definition.
099       * @param propertyName
100       *          The property name.
101       * @return Returns the new boolean property definition builder.
102       */
103      public static Builder createBuilder(
104          AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
105        return new Builder(d, propertyName);
106      }
107    
108    
109    
110      // Private constructor.
111      private BooleanPropertyDefinition(
112          AbstractManagedObjectDefinition<?, ?> d, String propertyName,
113          EnumSet<PropertyOption> options,
114          AdministratorAction adminAction,
115          DefaultBehaviorProvider<Boolean> defaultBehavior) {
116        super(d, Boolean.class, propertyName, options, adminAction,
117            defaultBehavior);
118      }
119    
120    
121    
122      /**
123       * {@inheritDoc}
124       */
125      @Override
126      public void validateValue(Boolean value)
127          throws IllegalPropertyValueException {
128        ensureNotNull(value);
129    
130        // No additional validation required.
131      }
132    
133    
134    
135      /**
136       * {@inheritDoc}
137       */
138      @Override
139      public Boolean decodeValue(String value)
140          throws IllegalPropertyValueStringException {
141        ensureNotNull(value);
142    
143        String nvalue = value.trim().toLowerCase();
144        Boolean b = VALUE_MAP.get(nvalue);
145    
146        if (b == null) {
147          throw new IllegalPropertyValueStringException(this, value);
148        } else {
149          return b;
150        }
151      }
152    
153    
154    
155      /**
156       * {@inheritDoc}
157       */
158      @Override
159      public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
160        return v.visitBoolean(this, p);
161      }
162    
163    
164    
165      /**
166       * {@inheritDoc}
167       */
168      @Override
169      public <R, P> R accept(PropertyValueVisitor<R, P> v, Boolean value, P p) {
170        return v.visitBoolean(this, value, p);
171      }
172    
173    
174    
175      /**
176       * {@inheritDoc}
177       */
178      @Override
179      public int compare(Boolean o1, Boolean o2) {
180        return o1.compareTo(o2);
181      }
182    }