001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.ldap.model.schema; 021 022 023import java.util.HashSet; 024import java.util.Set; 025 026import org.apache.directory.api.util.Strings; 027 028 029/** 030 * An structure containing a couple of attributeType and options. A search request 031 * can contain a list of attribute to return, those attribute could be associated 032 * with options. 033 * 034 * Those options are stored into a Set. 035 * 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 */ 038public class AttributeTypeOptions 039{ 040 /** The attributeType */ 041 private AttributeType attributeType; 042 043 /** The options, if any */ 044 private Set<String> options; 045 046 047 /** 048 * Creates a new instance of AttributeTypeOptions, containing an attributeType, 049 * but no options. 050 * 051 * @param attributeType The associated AttributeType 052 */ 053 public AttributeTypeOptions( AttributeType attributeType ) 054 { 055 this.attributeType = attributeType; 056 } 057 058 059 /** 060 * Creates a new instance of AttributeTypeOptions, containing an attributeType, 061 * and options. 062 * 063 * @param attributeType the associated AttributeType 064 * @param options the associated options 065 */ 066 public AttributeTypeOptions( AttributeType attributeType, Set<String> options ) 067 { 068 this.attributeType = attributeType; 069 this.options = options; 070 } 071 072 073 /** 074 * @return the inner attributeType 075 */ 076 public AttributeType getAttributeType() 077 { 078 return attributeType; 079 } 080 081 082 /** 083 * @return the associated options 084 */ 085 public Set<String> getOptions() 086 { 087 return options; 088 } 089 090 091 /** 092 * @return <code>true</code> if the attributeType has at least one option 093 */ 094 public boolean hasOption() 095 { 096 return ( options != null ) && !options.isEmpty(); 097 } 098 099 100 /** 101 * @param option the option to check 102 * @return <code>true</code> if the attributeType has the given option 103 */ 104 public boolean hasOption( String option ) 105 { 106 if ( hasOption() ) 107 { 108 return options.contains( Strings.toLowerCaseAscii( Strings.trim( option ) ) ); 109 } 110 else 111 { 112 return false; 113 } 114 } 115 116 117 /** 118 * Add a new option to the option set for this attributeType. 119 * 120 * @param option the option to add 121 */ 122 public void addOption( String option ) 123 { 124 if ( options == null ) 125 { 126 options = new HashSet<>(); 127 } 128 129 options.add( Strings.toLowerCaseAscii( Strings.trim( option ) ) ); 130 } 131 132 133 /** 134 * Add a set of optionS to the option set for this attributeType. 135 * 136 * @param optionsToAdd the options to add 137 */ 138 public void addOptions( Set<String> optionsToAdd ) 139 { 140 if ( this.options == null ) 141 { 142 this.options = optionsToAdd; 143 } 144 else 145 { 146 this.options.addAll( optionsToAdd ); 147 } 148 } 149 150 151 /** 152 * @see Object#hashCode() 153 */ 154 @Override 155 public int hashCode() 156 { 157 return attributeType.hashCode(); 158 } 159 160 161 /** 162 * @see Object#equals(Object) 163 */ 164 @Override 165 public boolean equals( Object o ) 166 { 167 // Short circuit 168 if ( this == o ) 169 { 170 return true; 171 } 172 173 if ( !( o instanceof AttributeTypeOptions ) ) 174 { 175 return false; 176 } 177 178 AttributeTypeOptions that = ( AttributeTypeOptions ) o; 179 180 return attributeType.equals( that.attributeType ); 181 } 182 183 /** 184 * {@inheritDoc} 185 */ 186 @Override 187 public String toString() 188 { 189 StringBuilder sb = new StringBuilder(); 190 191 sb.append( "<" ).append( attributeType.getName() ); 192 193 if ( hasOption() ) 194 { 195 for ( String option : options ) 196 { 197 sb.append( ";" ).append( option ); 198 } 199 } 200 201 return sb.append( ">" ).toString(); 202 } 203}