1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.model.schema;
21
22
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31
32 import org.apache.directory.api.i18n.I18n;
33 import org.apache.directory.api.util.Strings;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public abstract class AbstractSchemaObject implements SchemaObject, Serializable
73 {
74
75 private static final long serialVersionUID = 2L;
76
77
78 protected String oid;
79
80
81 protected List<String> names;
82
83
84 protected boolean isEnabled = true;
85
86
87 protected boolean isReadOnly = false;
88
89
90 protected boolean isObsolete = false;
91
92
93 protected String description;
94
95
96 protected String specification;
97
98
99 protected String schemaName;
100
101
102 protected SchemaObjectType objectType;
103
104
105 protected Map<String, List<String>> extensions;
106
107
108 protected volatile boolean locked;
109
110
111 private int h;
112
113
114
115
116
117
118
119
120
121 protected AbstractSchemaObject( SchemaObjectType objectType, String oid )
122 {
123 this.objectType = objectType;
124 this.oid = oid;
125 extensions = new HashMap<String, List<String>>();
126 names = new ArrayList<String>();
127 }
128
129
130
131
132
133
134
135
136 protected AbstractSchemaObject( SchemaObjectType objectType )
137 {
138 this.objectType = objectType;
139 extensions = new HashMap<String, List<String>>();
140 names = new ArrayList<String>();
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154 public String getOid()
155 {
156 return oid;
157 }
158
159
160
161
162
163
164
165 public void setOid( String oid )
166 {
167 if ( locked )
168 {
169 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
170 }
171
172 this.oid = oid;
173 }
174
175
176
177
178
179
180
181
182 public List<String> getNames()
183 {
184 if ( names != null )
185 {
186 return Collections.unmodifiableList( names );
187 }
188 else
189 {
190 return Collections.emptyList();
191 }
192 }
193
194
195
196
197
198
199
200
201
202 public String getName()
203 {
204 if ( ( names != null ) && ( names.size() != 0 ) )
205 {
206 return names.get( 0 );
207 }
208 else
209 {
210 return oid;
211 }
212 }
213
214
215
216
217
218
219
220
221 public void addName( String... namesToAdd )
222 {
223 if ( locked )
224 {
225 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
226 }
227
228 if ( !isReadOnly )
229 {
230
231 Set<String> lowerNames = new HashSet<String>();
232
233
234 for ( String name : this.names )
235 {
236 lowerNames.add( Strings.toLowerCase( name ) );
237 }
238
239 for ( String name : namesToAdd )
240 {
241 if ( name != null )
242 {
243 String lowerName = Strings.toLowerCase( name );
244
245 if ( !lowerNames.contains( lowerName ) )
246 {
247 this.names.add( name );
248 lowerNames.add( lowerName );
249 }
250 }
251 }
252 }
253 }
254
255
256
257
258
259
260
261
262 public void setNames( List<String> names )
263 {
264 if ( locked )
265 {
266 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
267 }
268
269 if ( names == null )
270 {
271 return;
272 }
273
274 if ( !isReadOnly )
275 {
276 this.names = new ArrayList<String>( names.size() );
277
278 for ( String name : names )
279 {
280 if ( name != null )
281 {
282 this.names.add( name );
283 }
284 }
285 }
286 }
287
288
289
290
291
292
293
294
295 public void setNames( String... names )
296 {
297 if ( locked )
298 {
299 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
300 }
301
302 if ( names == null )
303 {
304 return;
305 }
306
307 if ( !isReadOnly )
308 {
309 this.names.clear();
310
311 for ( String name : names )
312 {
313 if ( name != null )
314 {
315 this.names.add( name );
316 }
317 }
318 }
319 }
320
321
322
323
324
325
326
327 public String getDescription()
328 {
329 return description;
330 }
331
332
333
334
335
336
337
338 public void setDescription( String description )
339 {
340 if ( locked )
341 {
342 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
343 }
344
345 if ( !isReadOnly )
346 {
347 this.description = description;
348 }
349 }
350
351
352
353
354
355
356
357 public String getSpecification()
358 {
359 return specification;
360 }
361
362
363
364
365
366
367
368 public void setSpecification( String specification )
369 {
370 if ( locked )
371 {
372 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
373 }
374
375 if ( !isReadOnly )
376 {
377 this.specification = specification;
378 }
379 }
380
381
382
383
384
385
386
387
388 public boolean isEnabled()
389 {
390 return isEnabled;
391 }
392
393
394
395
396
397
398
399 public boolean isDisabled()
400 {
401 return !isEnabled;
402 }
403
404
405
406
407
408
409
410 public void setEnabled( boolean enabled )
411 {
412 if ( !isReadOnly )
413 {
414 isEnabled = enabled;
415 }
416 }
417
418
419
420
421
422
423
424 public boolean isReadOnly()
425 {
426 return isReadOnly;
427 }
428
429
430
431
432
433
434
435 public void setReadOnly( boolean readOnly )
436 {
437 if ( locked )
438 {
439 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
440 }
441
442 this.isReadOnly = readOnly;
443 }
444
445
446
447
448
449
450
451
452
453
454 public boolean isObsolete()
455 {
456 return isObsolete;
457 }
458
459
460
461
462
463
464
465 public void setObsolete( boolean obsolete )
466 {
467 if ( locked )
468 {
469 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
470 }
471
472 if ( !isReadOnly )
473 {
474 this.isObsolete = obsolete;
475 }
476 }
477
478
479
480
481
482 public Map<String, List<String>> getExtensions()
483 {
484 return extensions;
485 }
486
487
488
489
490
491
492
493 public void addExtension( String key, String... values )
494 {
495 if ( locked )
496 {
497 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
498 }
499
500 if ( !isReadOnly )
501 {
502 List<String> valueList = new ArrayList<String>();
503
504 for ( String value : values )
505 {
506 valueList.add( value );
507 }
508
509 extensions.put( key, valueList );
510 }
511 }
512
513
514
515
516
517
518
519 public void addExtension( String key, List<String> values )
520 {
521 if ( locked )
522 {
523 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
524 }
525
526 if ( !isReadOnly )
527 {
528 extensions.put( key, values );
529 }
530 }
531
532
533
534
535
536
537
538 public void setExtensions( Map<String, List<String>> extensions )
539 {
540 if ( locked )
541 {
542 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
543 }
544
545 if ( !isReadOnly && ( extensions != null ) )
546 {
547 this.extensions = new HashMap<String, List<String>>();
548
549 for ( Map.Entry<String, List<String>> entry : extensions.entrySet() )
550 {
551 List<String> values = new ArrayList<String>();
552
553 for ( String value : entry.getValue() )
554 {
555 values.add( value );
556 }
557
558 this.extensions.put( entry.getKey(), values );
559 }
560
561 }
562 }
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583 public SchemaObjectType getObjectType()
584 {
585 return objectType;
586 }
587
588
589
590
591
592
593
594 public String getSchemaName()
595 {
596 return schemaName;
597 }
598
599
600
601
602
603
604
605 public void setSchemaName( String schemaName )
606 {
607 if ( locked )
608 {
609 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
610 }
611
612 if ( !isReadOnly )
613 {
614 this.schemaName = schemaName;
615 }
616 }
617
618
619
620
621
622
623
624
625
626
627
628
629 @Override
630 public final int hashCode()
631 {
632 return h;
633 }
634
635
636
637
638
639 @Override
640 public boolean equals( Object o1 )
641 {
642 if ( this == o1 )
643 {
644 return true;
645 }
646
647 if ( !( o1 instanceof AbstractSchemaObject ) )
648 {
649 return false;
650 }
651
652 AbstractSchemaObject that = ( AbstractSchemaObject ) o1;
653
654
655
656
657
658 if ( !compareOid( oid, that.oid ) )
659 {
660 return false;
661 }
662
663
664 if ( names == null )
665 {
666 if ( that.names != null )
667 {
668 return false;
669 }
670 }
671 else if ( that.names == null )
672 {
673 return false;
674 }
675 else
676 {
677 int nbNames = 0;
678
679 for ( String name : names )
680 {
681 if ( !that.names.contains( name ) )
682 {
683 return false;
684 }
685
686 nbNames++;
687 }
688
689 if ( nbNames != names.size() )
690 {
691 return false;
692 }
693 }
694
695 if ( schemaName == null )
696 {
697 if ( that.schemaName != null )
698 {
699 return false;
700 }
701 }
702 else
703 {
704 if ( !schemaName.equalsIgnoreCase( that.schemaName ) )
705 {
706 return false;
707 }
708 }
709
710 if ( objectType != that.objectType )
711 {
712 return false;
713 }
714
715 if ( extensions != null )
716 {
717 if ( that.extensions == null )
718 {
719 return false;
720 }
721 else
722 {
723 for ( String key : extensions.keySet() )
724 {
725 if ( !that.extensions.containsKey( key ) )
726 {
727 return false;
728 }
729
730 List<String> thisValues = extensions.get( key );
731 List<String> thatValues = that.extensions.get( key );
732
733 if ( thisValues != null )
734 {
735 if ( thatValues == null )
736 {
737 return false;
738 }
739 else
740 {
741 if ( thisValues.size() != thatValues.size() )
742 {
743 return false;
744 }
745
746
747 }
748 }
749 else if ( thatValues != null )
750 {
751 return false;
752 }
753 }
754 }
755 }
756 else if ( that.extensions != null )
757 {
758 return false;
759 }
760
761 if ( this.isEnabled != that.isEnabled )
762 {
763 return false;
764 }
765
766 if ( this.isObsolete != that.isObsolete )
767 {
768 return false;
769 }
770
771 if ( this.isReadOnly != that.isReadOnly )
772 {
773 return false;
774 }
775
776 if ( this.description == null )
777 {
778 return that.description == null;
779 }
780 else
781 {
782 return this.description.equalsIgnoreCase( that.description );
783 }
784 }
785
786
787
788
789
790
791
792 public abstract SchemaObject copy();
793
794
795
796
797
798
799
800
801
802 protected boolean compareOid( String oid1, String oid2 )
803 {
804 if ( oid1 == null )
805 {
806 return oid2 == null;
807 }
808 else
809 {
810 return oid1.equals( oid2 );
811 }
812 }
813
814
815
816
817
818 public SchemaObject copy( SchemaObject original )
819 {
820
821 description = original.getDescription();
822
823
824 isEnabled = original.isEnabled();
825 isObsolete = original.isObsolete();
826 isReadOnly = original.isReadOnly();
827
828
829 names = new ArrayList<String>();
830
831 for ( String name : original.getNames() )
832 {
833 names.add( name );
834 }
835
836
837 extensions = new HashMap<String, List<String>>();
838
839 for ( String key : original.getExtensions().keySet() )
840 {
841 List<String> extensionValues = original.getExtensions().get( key );
842
843 List<String> cloneExtension = new ArrayList<String>();
844
845 for ( String value : extensionValues )
846 {
847 cloneExtension.add( value );
848 }
849
850 extensions.put( key, cloneExtension );
851 }
852
853
854 schemaName = original.getSchemaName();
855
856
857 specification = original.getSpecification();
858
859 return this;
860 }
861
862
863
864
865
866
867 public void clear()
868 {
869
870 for ( String extension : extensions.keySet() )
871 {
872 List<String> extensionList = extensions.get( extension );
873
874 extensionList.clear();
875 }
876
877 extensions.clear();
878
879
880 names.clear();
881 }
882
883
884 public void unlock()
885 {
886 locked = false;
887 }
888
889
890
891
892
893 public final void lock()
894 {
895 if ( locked )
896 {
897 return;
898 }
899
900 h = 37;
901
902
903 h += h * 17 + oid.hashCode();
904
905
906 h += h * 17 + objectType.getValue();
907
908
909 if ( ( names != null ) && ( names.size() != 0 ) )
910 {
911 for ( String name : names )
912 {
913 h += h * 17 + name.hashCode();
914 }
915 }
916
917
918 if ( schemaName != null )
919 {
920 h += h * 17 + schemaName.hashCode();
921 }
922
923 h += h * 17 + ( isEnabled ? 1 : 0 );
924 h += h * 17 + ( isReadOnly ? 1 : 0 );
925
926
927 if ( description != null )
928 {
929 h += h * 17 + description.hashCode();
930 }
931
932
933 for ( String key : extensions.keySet() )
934 {
935 h += h * 17 + key.hashCode();
936
937 List<String> values = extensions.get( key );
938
939 if ( values != null )
940 {
941 for ( String value : values )
942 {
943 h += h * 17 + value.hashCode();
944 }
945 }
946 }
947
948 locked = true;
949 }
950 }