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 2014-2015 ForgeRock AS 026 */ 027 028package org.opends.server.admin; 029 030 031 032import static org.forgerock.util.Reject.ifNull; 033 034import java.util.EnumSet; 035import java.util.HashMap; 036import java.util.Map; 037 038 039 040/** 041 * Boolean property definition. 042 */ 043public 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 = new HashMap<>(); 052 static { 053 // We could have more possibilities but decided against in issue 1960. 054 VALUE_MAP.put("false", Boolean.FALSE); 055 VALUE_MAP.put("true", Boolean.TRUE); 056 } 057 058 059 060 /** 061 * An interface for incrementally constructing boolean property definitions. 062 */ 063 public static class Builder extends 064 AbstractBuilder<Boolean, BooleanPropertyDefinition> { 065 066 /** Private constructor. */ 067 private Builder( 068 AbstractManagedObjectDefinition<?, ?> d, String propertyName) { 069 super(d, propertyName); 070 } 071 072 073 074 /** {@inheritDoc} */ 075 @Override 076 protected BooleanPropertyDefinition buildInstance( 077 AbstractManagedObjectDefinition<?, ?> d, String propertyName, 078 EnumSet<PropertyOption> options, 079 AdministratorAction adminAction, 080 DefaultBehaviorProvider<Boolean> defaultBehavior) { 081 return new BooleanPropertyDefinition(d, propertyName, options, 082 adminAction, defaultBehavior); 083 } 084 085 } 086 087 088 089 /** 090 * Create a boolean property definition builder. 091 * 092 * @param d 093 * The managed object definition associated with this 094 * property definition. 095 * @param propertyName 096 * The property name. 097 * @return Returns the new boolean property definition builder. 098 */ 099 public static Builder createBuilder( 100 AbstractManagedObjectDefinition<?, ?> d, String propertyName) { 101 return new Builder(d, propertyName); 102 } 103 104 105 106 /** Private constructor. */ 107 private BooleanPropertyDefinition( 108 AbstractManagedObjectDefinition<?, ?> d, String propertyName, 109 EnumSet<PropertyOption> options, 110 AdministratorAction adminAction, 111 DefaultBehaviorProvider<Boolean> defaultBehavior) { 112 super(d, Boolean.class, propertyName, options, adminAction, 113 defaultBehavior); 114 } 115 116 117 118 /** {@inheritDoc} */ 119 @Override 120 public void validateValue(Boolean value) 121 throws PropertyException { 122 ifNull(value); 123 124 // No additional validation required. 125 } 126 127 128 129 /** {@inheritDoc} */ 130 @Override 131 public Boolean decodeValue(String value) 132 throws PropertyException { 133 ifNull(value); 134 135 String nvalue = value.trim().toLowerCase(); 136 Boolean b = VALUE_MAP.get(nvalue); 137 138 if (b == null) { 139 throw PropertyException.illegalPropertyValueException(this, value); 140 } else { 141 return b; 142 } 143 } 144 145 146 147 /** {@inheritDoc} */ 148 @Override 149 public <R, P> R accept(PropertyDefinitionVisitor<R, P> v, P p) { 150 return v.visitBoolean(this, p); 151 } 152 153 154 155 /** {@inheritDoc} */ 156 @Override 157 public <R, P> R accept(PropertyValueVisitor<R, P> v, Boolean value, P p) { 158 return v.visitBoolean(this, value, p); 159 } 160 161 162 163 /** {@inheritDoc} */ 164 @Override 165 public int compare(Boolean o1, Boolean o2) { 166 return o1.compareTo(o2); 167 } 168}