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 2015 ForgeRock AS. 016 */ 017package org.forgerock.opendj.config; 018 019import org.forgerock.util.Reject; 020 021import java.util.EnumSet; 022import java.util.HashMap; 023import java.util.Map; 024 025/** 026 * Boolean property definition. 027 */ 028public final class BooleanPropertyDefinition extends PropertyDefinition<Boolean> { 029 030 /** 031 * Mapping used for parsing boolean values. This mapping is more flexible 032 * than the standard boolean string parser and supports common true/false 033 * synonyms used in configuration. 034 */ 035 private static final Map<String, Boolean> VALUE_MAP = new HashMap<>(); 036 static { 037 // We could have more possibilities but decided against in issue 1960. 038 VALUE_MAP.put("false", Boolean.FALSE); 039 VALUE_MAP.put("true", Boolean.TRUE); 040 } 041 042 /** 043 * An interface for incrementally constructing boolean property definitions. 044 */ 045 public static final class Builder extends AbstractBuilder<Boolean, BooleanPropertyDefinition> { 046 047 /** Private constructor. */ 048 private Builder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) { 049 super(d, propertyName); 050 } 051 052 /** {@inheritDoc} */ 053 @Override 054 protected BooleanPropertyDefinition buildInstance(AbstractManagedObjectDefinition<?, ?> d, 055 String propertyName, EnumSet<PropertyOption> options, AdministratorAction adminAction, 056 DefaultBehaviorProvider<Boolean> defaultBehavior) { 057 return new BooleanPropertyDefinition(d, propertyName, options, adminAction, defaultBehavior); 058 } 059 060 } 061 062 /** 063 * Create a boolean property definition builder. 064 * 065 * @param d 066 * The managed object definition associated with this property 067 * definition. 068 * @param propertyName 069 * The property name. 070 * @return Returns the new boolean property definition builder. 071 */ 072 public static Builder createBuilder(AbstractManagedObjectDefinition<?, ?> d, String propertyName) { 073 return new Builder(d, propertyName); 074 } 075 076 /** Private constructor. */ 077 private BooleanPropertyDefinition(AbstractManagedObjectDefinition<?, ?> d, String propertyName, 078 EnumSet<PropertyOption> options, AdministratorAction adminAction, 079 DefaultBehaviorProvider<Boolean> defaultBehavior) { 080 super(d, Boolean.class, propertyName, options, adminAction, defaultBehavior); 081 } 082 083 /** {@inheritDoc} */ 084 @Override 085 public void validateValue(Boolean value) { 086 Reject.ifNull(value); 087 088 // No additional validation required. 089 } 090 091 /** {@inheritDoc} */ 092 @Override 093 public Boolean decodeValue(String value) { 094 Reject.ifNull(value); 095 096 String nvalue = value.trim().toLowerCase(); 097 Boolean b = VALUE_MAP.get(nvalue); 098 099 if (b == null) { 100 throw PropertyException.illegalPropertyValueException(this, value); 101 } else { 102 return b; 103 } 104 } 105 106 /** {@inheritDoc} */ 107 @Override 108 public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) { 109 return v.visitBoolean(this, p); 110 } 111 112 /** {@inheritDoc} */ 113 @Override 114 public <R, P> R accept(PropertyValueVisitor<R, P> v, Boolean value, P p) { 115 return v.visitBoolean(this, value, p); 116 } 117 118 /** {@inheritDoc} */ 119 @Override 120 public int compare(Boolean o1, Boolean o2) { 121 return o1.compareTo(o2); 122 } 123}