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 org.apache.directory.shared.ldap.util.ArrayUtils;
024
025
026 /**
027 * The abstract base class for all schema object types.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 * @version $Rev: 664290 $
031 */
032 public abstract class AbstractSchemaObject implements SchemaObject
033 {
034 /** a numeric object identifier */
035 protected final String oid;
036
037 /** The object hash code compiled only once to avoid doing it at every call */
038 private int hash;
039
040 /** whether or not this SchemaObject is active */
041 protected boolean isObsolete;
042
043 /** a human readable identifiers for this SchemaObject */
044 protected String[] names = ArrayUtils.EMPTY_STRING_ARRAY;
045
046 /** a short description of this SchemaObject */
047 protected String description;
048
049 /** the name of the schema this object is associated with */
050 protected String schema;
051
052 // ------------------------------------------------------------------------
053 // C O N S T R U C T O R S
054 // ------------------------------------------------------------------------
055
056 /**
057 * Creates an abstract SchemaObject.
058 *
059 * @param oid
060 * the numeric object identifier (OID)
061 * @see SchemaObject#getOid()
062 * @see MatchingRuleUse
063 * @throws NullPointerException
064 * if oid is null
065 */
066 protected AbstractSchemaObject( String oid )
067 {
068 this( oid, ArrayUtils.EMPTY_STRING_ARRAY, false, null );
069
070 hash = oid.hashCode();
071 }
072
073
074 /**
075 * Creates an abstract SchemaObject.
076 *
077 * @param oid the numeric object identifier (OID)
078 * @param names the human readable names for this SchemaObject
079 * @throws NullPointerException if oid is null
080 */
081 protected AbstractSchemaObject( String oid, String[] names )
082 {
083 this( oid, names, false, null );
084
085 hash = oid.hashCode();
086 }
087
088
089 /**
090 * Creates an abstract SchemaObject.
091 *
092 * @param oid the numeric object identifier (OID)
093 * @param names the human readable names for this SchemaObject
094 * @param isObsolete true if this object is inactive, false if active
095 * @throws NullPointerException if oid is null
096 */
097 protected AbstractSchemaObject( String oid, String[] names, boolean isObsolete )
098 {
099 this( oid, names, isObsolete, null );
100
101 hash = oid.hashCode();
102 }
103
104
105 /**
106 * Creates an abstract SchemaObject.
107 *
108 * @param oid the numeric object identifier (OID)
109 * @param name the first human readable name for this SchemaObject
110 * @param isObsolete true if this object is inactive, false if active
111 * @throws NullPointerException if oid is null
112 */
113 protected AbstractSchemaObject( String oid, String name, boolean isObsolete )
114 {
115 this( oid, new String[]
116 { name }, isObsolete, null );
117
118 hash = oid.hashCode();
119 }
120
121
122 /**
123 * Creates an abstract SchemaObject.
124 *
125 * @param oid the numeric object identifier (OID)
126 * @param isObsolete true if this object is inactive, false if active
127 * @throws NullPointerException if oid is null
128 */
129 protected AbstractSchemaObject( String oid, boolean isObsolete )
130 {
131 this( oid, null, isObsolete, null );
132
133 hash = oid.hashCode();
134 }
135
136
137 /**
138 * Creates an abstract SchemaObject.
139 *
140 * @param oid the numeric object identifier (OID)
141 * @param description a brief description for the SchemaObject
142 * @throws NullPointerException if oid is null
143 */
144 protected AbstractSchemaObject( String oid, String description )
145 {
146 this( oid, null, false, description );
147
148 hash = oid.hashCode();
149 }
150
151
152 /**
153 * Creates an abstract SchemaObject.
154 *
155 * @param oid the numeric object identifier (OID)
156 * @param names the human readable names for this SchemaObject
157 * @param isObsolete true if this object is inactive, false if active
158 * @param description a brief description for the SchemaObject
159 * @throws NullPointerException if oid is null
160 */
161 protected AbstractSchemaObject( String oid, String[] names, boolean isObsolete, String description )
162 {
163 if ( oid == null )
164 {
165 throw new NullPointerException( "oid cannot be null" );
166 }
167
168 this.oid = oid;
169 this.isObsolete = isObsolete;
170 this.description = description;
171
172 if ( names != null )
173 {
174 this.names = new String[names.length];
175 System.arraycopy( names, 0, this.names, 0, names.length );
176 }
177
178 hash = oid.hashCode();
179 }
180
181
182 // ------------------------------------------------------------------------
183 // P U B L I C A C C E S S O R S
184 // ------------------------------------------------------------------------
185
186 /**
187 * @see SchemaObject#getOid()
188 * @return an OID for this SchemaObject or its MatchingRule if this
189 * SchemaObject is a MatchingRuleUse object
190 */
191 public String getOid()
192 {
193 return oid;
194 }
195
196
197 /**
198 * @see SchemaObject#isObsolete()
199 * @return true if inactive, false if active
200 */
201 public boolean isObsolete()
202 {
203 return isObsolete;
204 }
205
206
207 /**
208 * @see SchemaObject#getNames()
209 * @return the names for this SchemaObject
210 */
211 public String[] getNamesRef()
212 {
213 return names;
214 }
215
216
217 /**
218 * @see SchemaObject#getSchema()
219 * @return the name of the schema associated with this schemaObject
220 */
221 public String getSchema()
222 {
223 return schema;
224 }
225
226
227 /**
228 * @see SchemaObject#getName()
229 * @return the first of the names for this SchemaObject or null if one does
230 * not exist
231 */
232 public String getName()
233 {
234 return ( names == null || names.length == 0 ) ? null : names[0];
235 }
236
237
238 /**
239 * @see SchemaObject#getDescription()
240 * @return a short description about this SchemaObject
241 */
242 public String getDescription()
243 {
244 return description;
245 }
246
247
248 // ------------------------------------------------------------------------
249 // P R O T E C T E D M U T A T O R S
250 // ------------------------------------------------------------------------
251
252
253 /**
254 * Sets whether or not this SchemaObject is inactived.
255 *
256 * @param obsolete
257 * true if this object is inactive, false if it is in use
258 */
259 protected void setObsolete( boolean obsolete )
260 {
261 isObsolete = obsolete;
262 }
263
264
265 /**
266 * Sets schema name this schema object is associated with.
267 *
268 * @param schema the name of the schema this object is associated with
269 */
270 public void setSchema( String schema )
271 {
272 this.schema = schema;
273 }
274
275
276 /**
277 * Sets the human readable names for this SchemaObject.
278 *
279 * @param names
280 * the human readable names for this SchemaObject
281 */
282 protected void setNames( String[] names )
283 {
284 this.names = new String[names.length];
285 System.arraycopy( names, 0, this.names, 0, names.length );
286 }
287
288
289 /**
290 * Sets the brief description for this SchemaObject.
291 *
292 * @param description
293 * the brief description for this SchemaObject
294 */
295 protected void setDescription( String description )
296 {
297 this.description = description;
298 }
299
300
301 // ------------------------------------------------------------------------
302 // Object overloads
303 // ------------------------------------------------------------------------
304
305
306 /**
307 * Based on the hashCode of the oid property. It's bre-computed, as this
308 * value won't change once the instance is created.
309 *
310 * @return the hashCode of the oid String
311 */
312 public int hashCode()
313 {
314 return hash;
315 }
316
317
318 /**
319 * If the object implements SchemaObject and has the same OID as this
320 * SchemaObject then they are considered equal.
321 *
322 * @param obj
323 * the object to test for equality
324 * @return true if obj is a SchemaObject and the OID's match
325 */
326 public boolean equals( Object obj )
327 {
328 if ( obj == this )
329 {
330 return true;
331 }
332
333 if ( obj instanceof SchemaObject )
334 {
335 return oid.equals( ( ( SchemaObject ) obj ).getOid() );
336 }
337
338 return false;
339 }
340
341
342 /**
343 * Gets the String for the OID of this SchmeaObject.
344 *
345 * @return the OID of this SchmeaObject
346 */
347 public String toString()
348 {
349 return "<" + oid + ", " + ( ( names == null ) || ( names.length == 0 ) ? "null" : names[0] ) + ">";
350 }
351 }