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 public boolean hasExtension( String extension )
492 {
493 return extensions.containsKey( Strings.toUpperCase( extension ) );
494 }
495
496
497
498
499
500 public List<String> getExtension( String extension )
501 {
502 String name = Strings.toUpperCase( extension );
503
504 if ( hasExtension( name ) )
505 {
506 for ( String key : extensions.keySet() )
507 {
508 if ( name.equalsIgnoreCase( key ) )
509 {
510 return extensions.get( key );
511 }
512 }
513 }
514
515 return null;
516 }
517
518
519
520
521
522
523
524 public void addExtension( String key, String... values )
525 {
526 if ( locked )
527 {
528 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
529 }
530
531 if ( !isReadOnly )
532 {
533 List<String> valueList = new ArrayList<String>();
534
535 for ( String value : values )
536 {
537 valueList.add( value );
538 }
539
540 extensions.put( Strings.toUpperCase( key ), valueList );
541 }
542 }
543
544
545
546
547
548
549
550 public void addExtension( String key, List<String> values )
551 {
552 if ( locked )
553 {
554 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
555 }
556
557 if ( !isReadOnly )
558 {
559 extensions.put( Strings.toUpperCase( key ), values );
560 }
561 }
562
563
564
565
566
567
568
569 public void setExtensions( Map<String, List<String>> extensions )
570 {
571 if ( locked )
572 {
573 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
574 }
575
576 if ( !isReadOnly && ( extensions != null ) )
577 {
578 this.extensions = new HashMap<String, List<String>>();
579
580 for ( Map.Entry<String, List<String>> entry : extensions.entrySet() )
581 {
582 List<String> values = new ArrayList<String>();
583
584 for ( String value : entry.getValue() )
585 {
586 values.add( value );
587 }
588
589 this.extensions.put( Strings.toUpperCase( entry.getKey() ), values );
590 }
591
592 }
593 }
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614 public SchemaObjectType getObjectType()
615 {
616 return objectType;
617 }
618
619
620
621
622
623
624
625 public String getSchemaName()
626 {
627 return schemaName;
628 }
629
630
631
632
633
634
635
636 public void setSchemaName( String schemaName )
637 {
638 if ( locked )
639 {
640 throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
641 }
642
643 if ( !isReadOnly )
644 {
645 this.schemaName = schemaName;
646 }
647 }
648
649
650
651
652
653
654
655
656
657
658
659
660 @Override
661 public final int hashCode()
662 {
663 return h;
664 }
665
666
667
668
669
670 @Override
671 public boolean equals( Object o1 )
672 {
673 if ( this == o1 )
674 {
675 return true;
676 }
677
678 if ( !( o1 instanceof AbstractSchemaObject ) )
679 {
680 return false;
681 }
682
683 AbstractSchemaObject that = ( AbstractSchemaObject ) o1;
684
685
686
687
688
689 if ( !compareOid( oid, that.oid ) )
690 {
691 return false;
692 }
693
694
695 if ( names == null )
696 {
697 if ( that.names != null )
698 {
699 return false;
700 }
701 }
702 else if ( that.names == null )
703 {
704 return false;
705 }
706 else
707 {
708 int nbNames = 0;
709
710 for ( String name : names )
711 {
712 if ( !that.names.contains( name ) )
713 {
714 return false;
715 }
716
717 nbNames++;
718 }
719
720 if ( nbNames != names.size() )
721 {
722 return false;
723 }
724 }
725
726 if ( schemaName == null )
727 {
728 if ( that.schemaName != null )
729 {
730 return false;
731 }
732 }
733 else
734 {
735 if ( !schemaName.equalsIgnoreCase( that.schemaName ) )
736 {
737 return false;
738 }
739 }
740
741 if ( objectType != that.objectType )
742 {
743 return false;
744 }
745
746 if ( extensions != null )
747 {
748 if ( that.extensions == null )
749 {
750 return false;
751 }
752 else
753 {
754 for ( String key : extensions.keySet() )
755 {
756 if ( !that.extensions.containsKey( key ) )
757 {
758 return false;
759 }
760
761 List<String> thisValues = extensions.get( key );
762 List<String> thatValues = that.extensions.get( key );
763
764 if ( thisValues != null )
765 {
766 if ( thatValues == null )
767 {
768 return false;
769 }
770 else
771 {
772 if ( thisValues.size() != thatValues.size() )
773 {
774 return false;
775 }
776
777
778 }
779 }
780 else if ( thatValues != null )
781 {
782 return false;
783 }
784 }
785 }
786 }
787 else if ( that.extensions != null )
788 {
789 return false;
790 }
791
792 if ( this.isEnabled != that.isEnabled )
793 {
794 return false;
795 }
796
797 if ( this.isObsolete != that.isObsolete )
798 {
799 return false;
800 }
801
802 if ( this.isReadOnly != that.isReadOnly )
803 {
804 return false;
805 }
806
807 if ( this.description == null )
808 {
809 return that.description == null;
810 }
811 else
812 {
813 return this.description.equalsIgnoreCase( that.description );
814 }
815 }
816
817
818
819
820
821
822
823 public abstract SchemaObject copy();
824
825
826
827
828
829
830
831
832
833 protected boolean compareOid( String oid1, String oid2 )
834 {
835 if ( oid1 == null )
836 {
837 return oid2 == null;
838 }
839 else
840 {
841 return oid1.equals( oid2 );
842 }
843 }
844
845
846
847
848
849 public SchemaObject copy( SchemaObject original )
850 {
851
852 description = original.getDescription();
853
854
855 isEnabled = original.isEnabled();
856 isObsolete = original.isObsolete();
857 isReadOnly = original.isReadOnly();
858
859
860 names = new ArrayList<String>();
861
862 for ( String name : original.getNames() )
863 {
864 names.add( name );
865 }
866
867
868 extensions = new HashMap<String, List<String>>();
869
870 for ( String key : original.getExtensions().keySet() )
871 {
872 List<String> extensionValues = original.getExtension( key );
873
874 List<String> cloneExtension = new ArrayList<String>();
875
876 for ( String value : extensionValues )
877 {
878 cloneExtension.add( value );
879 }
880
881 extensions.put( key, cloneExtension );
882 }
883
884
885 schemaName = original.getSchemaName();
886
887
888 specification = original.getSpecification();
889
890 return this;
891 }
892
893
894
895
896
897
898 public void clear()
899 {
900
901 for ( String extension : extensions.keySet() )
902 {
903 List<String> extensionList = extensions.get( extension );
904
905 extensionList.clear();
906 }
907
908 extensions.clear();
909
910
911 names.clear();
912 }
913
914
915 public void unlock()
916 {
917 locked = false;
918 }
919
920
921
922
923
924 public final void lock()
925 {
926 if ( locked )
927 {
928 return;
929 }
930
931 h = 37;
932
933
934 h += h * 17 + oid.hashCode();
935
936
937 h += h * 17 + objectType.getValue();
938
939
940 if ( ( names != null ) && ( names.size() != 0 ) )
941 {
942 for ( String name : names )
943 {
944 h += h * 17 + name.hashCode();
945 }
946 }
947
948
949 if ( schemaName != null )
950 {
951 h += h * 17 + schemaName.hashCode();
952 }
953
954 h += h * 17 + ( isEnabled ? 1 : 0 );
955 h += h * 17 + ( isReadOnly ? 1 : 0 );
956
957
958 if ( description != null )
959 {
960 h += h * 17 + description.hashCode();
961 }
962
963
964 for ( String key : extensions.keySet() )
965 {
966 h += h * 17 + key.hashCode();
967
968 List<String> values = extensions.get( key );
969
970 if ( values != null )
971 {
972 for ( String value : values )
973 {
974 h += h * 17 + value.hashCode();
975 }
976 }
977 }
978
979 locked = true;
980 }
981 }