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 */
020 package org.apache.directory.shared.ldap.schema;
021
022
023 import java.util.HashSet;
024 import java.util.Set;
025
026 import org.apache.directory.shared.ldap.util.StringTools;
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 * @version $Rev: 655151 $
038 */
039 public class AttributeTypeOptions
040 {
041 /** The attributeType */
042 private AttributeType attributeType;
043
044 /** The options, if any */
045 private Set<String> options;
046
047
048 /**
049 * Creates a new instance of AttributeTypeOptions, containing an attributeType,
050 * but no options.
051 *
052 * @param attributeType The associated AttributeType
053 */
054 public AttributeTypeOptions( AttributeType attributeType )
055 {
056 this.attributeType = attributeType;
057 }
058
059
060 /**
061 * Creates a new instance of AttributeTypeOptions, containing an attributeType,
062 * and options.
063 *
064 * @param attributeType the associated AttributeType
065 * @param options the associated options
066 */
067 public AttributeTypeOptions( AttributeType attributeType, Set<String> options )
068 {
069 this.attributeType = attributeType;
070 this.options = options;
071 }
072
073
074 /**
075 * @return the inner attributeType
076 */
077 public AttributeType getAttributeType()
078 {
079 return attributeType;
080 }
081
082
083 /**
084 * @return the associated options
085 */
086 public Set<String> getOptions()
087 {
088 return options;
089 }
090
091
092 /**
093 * @return <code>true</code> if the attributeType has at least one option
094 */
095 public boolean hasOption()
096 {
097 return ( options != null ) && ( options.size() != 0 );
098 }
099
100
101 /**
102 * @param option the option to check
103 * @return <code>true</code> if the attributeType has the given option
104 */
105 public boolean hasOption( String option )
106 {
107 if ( hasOption() )
108 {
109 return options.contains( StringTools.toLowerCase( StringTools.trim( option ) ) );
110 }
111 else
112 {
113 return false;
114 }
115 }
116
117
118 /**
119 * Add a new option to the option set for this attributeType.
120 *
121 * @param option the option to add
122 */
123 public void addOption( String option )
124 {
125 if ( options == null )
126 {
127 options = new HashSet<String>();
128 }
129
130 options.add( StringTools.toLowerCase( StringTools.trim( option ) ) );
131 }
132
133
134 /**
135 * Add a set of optionS to the option set for this attributeType.
136 *
137 * @param options the options to add
138 */
139 public void addOptions( Set<String> options )
140 {
141 if ( this.options == null )
142 {
143 this.options = options;
144 }
145 else
146 {
147 this.options.addAll( options );
148 }
149 }
150
151
152 public String toString()
153 {
154 StringBuilder sb = new StringBuilder();
155
156 sb.append( "<" ).append( attributeType.getName() );
157
158 if ( hasOption() )
159 {
160 for ( String option:options )
161 {
162 sb.append( ";" ).append( option );
163 }
164 }
165
166 return sb.append( ">" ).toString();
167 }
168 }