View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema;
21  
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.apache.directory.api.util.Strings;
27  
28  
29  /**
30   * An structure containing a couple of attributeType and options. A search request
31   * can contain a list of attribute to return, those attribute could be associated
32   * with options.
33   * 
34   * Those options are stored into a Set.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class AttributeTypeOptions
39  {
40      /** The attributeType */
41      private AttributeType attributeType;
42  
43      /** The options, if any */
44      private Set<String> options;
45  
46  
47      /**
48       * Creates a new instance of AttributeTypeOptions, containing an attributeType, 
49       * but no options.
50       *
51       * @param attributeType The associated AttributeType
52       */
53      public AttributeTypeOptions( AttributeType attributeType )
54      {
55          this.attributeType = attributeType;
56      }
57  
58  
59      /**
60       * Creates a new instance of AttributeTypeOptions, containing an attributeType, 
61       * and options.
62       *
63       * @param attributeType the associated AttributeType
64       * @param options the associated options
65       */
66      public AttributeTypeOptions( AttributeType attributeType, Set<String> options )
67      {
68          this.attributeType = attributeType;
69          this.options = options;
70      }
71  
72  
73      /**
74       * @return the inner attributeType
75       */
76      public AttributeType getAttributeType()
77      {
78          return attributeType;
79      }
80  
81  
82      /**
83       * @return the associated options
84       */
85      public Set<String> getOptions()
86      {
87          return options;
88      }
89  
90  
91      /**
92       * @return <code>true</code> if the attributeType has at least one option
93       */
94      public boolean hasOption()
95      {
96          return ( options != null ) && !options.isEmpty();
97      }
98  
99  
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 }