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