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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027package org.forgerock.opendj.config;
028
029import org.forgerock.util.Reject;
030
031import java.util.EnumSet;
032import java.util.HashMap;
033import java.util.Map;
034
035/**
036 * Boolean property definition.
037 */
038public final class BooleanPropertyDefinition extends PropertyDefinition<Boolean> {
039
040    /**
041     * Mapping used for parsing boolean values. This mapping is more flexible
042     * than the standard boolean string parser and supports common true/false
043     * synonyms used in configuration.
044     */
045    private static final Map<String, Boolean> VALUE_MAP = new HashMap<>();
046    static {
047        // We could have more possibilities but decided against in issue 1960.
048        VALUE_MAP.put("false", Boolean.FALSE);
049        VALUE_MAP.put("true", Boolean.TRUE);
050    }
051
052    /**
053     * An interface for incrementally constructing boolean property definitions.
054     */
055    public static final class Builder extends AbstractBuilder<Boolean, BooleanPropertyDefinition> {
056
057        /** Private constructor. */
058        private Builder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
059            super(d, propertyName);
060        }
061
062        /** {@inheritDoc} */
063        @Override
064        protected BooleanPropertyDefinition buildInstance(AbstractManagedObjectDefinition<?, ?> d,
065            String propertyName, EnumSet<PropertyOption> options, AdministratorAction adminAction,
066            DefaultBehaviorProvider<Boolean> defaultBehavior) {
067            return new BooleanPropertyDefinition(d, propertyName, options, adminAction, defaultBehavior);
068        }
069
070    }
071
072    /**
073     * Create a boolean property definition builder.
074     *
075     * @param d
076     *            The managed object definition associated with this property
077     *            definition.
078     * @param propertyName
079     *            The property name.
080     * @return Returns the new boolean property definition builder.
081     */
082    public static Builder createBuilder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) {
083        return new Builder(d, propertyName);
084    }
085
086    /** Private constructor. */
087    private BooleanPropertyDefinition(AbstractManagedObjectDefinition<?, ?> d, String propertyName,
088        EnumSet<PropertyOption> options, AdministratorAction adminAction,
089        DefaultBehaviorProvider<Boolean> defaultBehavior) {
090        super(d, Boolean.class, propertyName, options, adminAction, defaultBehavior);
091    }
092
093    /** {@inheritDoc} */
094    @Override
095    public void validateValue(Boolean value) {
096        Reject.ifNull(value);
097
098        // No additional validation required.
099    }
100
101    /** {@inheritDoc} */
102    @Override
103    public Boolean decodeValue(String value) {
104        Reject.ifNull(value);
105
106        String nvalue = value.trim().toLowerCase();
107        Boolean b = VALUE_MAP.get(nvalue);
108
109        if (b == null) {
110            throw PropertyException.illegalPropertyValueException(this, value);
111        } else {
112            return b;
113        }
114    }
115
116    /** {@inheritDoc} */
117    @Override
118    public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) {
119        return v.visitBoolean(this, p);
120    }
121
122    /** {@inheritDoc} */
123    @Override
124    public <R, P> R accept(PropertyValueVisitor<R, P> v, Boolean value, P p) {
125        return v.visitBoolean(this, value, p);
126    }
127
128    /** {@inheritDoc} */
129    @Override
130    public int compare(Boolean o1, Boolean o2) {
131        return o1.compareTo(o2);
132    }
133}