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