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