1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.schema.bootstrap;
21
22
23 import java.io.Serializable;
24 import java.util.Comparator;
25
26 import javax.naming.NamingException;
27
28 import org.apache.directory.server.schema.registries.OidRegistry;
29 import org.apache.directory.server.schema.registries.Registries;
30 import org.apache.directory.shared.ldap.util.StringTools;
31
32
33
34
35
36
37
38
39
40
41 public class NameOrNumericIdComparator implements Comparator, Serializable
42 {
43 private static final long serialVersionUID = 1L;
44 private transient OidRegistry registry;
45
46
47 public NameOrNumericIdComparator( OidRegistry registry )
48 {
49 this.registry = registry;
50 }
51
52
53 public NameOrNumericIdComparator()
54 {
55 }
56
57
58
59
60
61 public int compare( Object o1, Object o2 )
62 {
63 String s1 = getNumericIdString( o1 );
64 String s2 = getNumericIdString( o2 );
65
66 if ( s1 == null && s2 == null )
67 {
68 return 0;
69 }
70
71 if ( s1 == null )
72 {
73 return -1;
74 }
75
76 if ( s2 == null )
77 {
78 return 1;
79 }
80
81 return s1.compareTo( s2 );
82 }
83
84
85 public void setRegistries( Registries registries )
86 {
87 registry = registries.getOidRegistry();
88 }
89
90
91 String getNumericIdString( Object obj )
92 {
93 String strValue;
94
95 if ( obj == null )
96 {
97 return null;
98 }
99
100 if ( obj instanceof String )
101 {
102 strValue = ( String ) obj;
103 }
104 else if ( obj instanceof byte[] )
105 {
106 strValue = StringTools.utf8ToString( ( byte[] ) obj );
107 }
108 else
109 {
110 strValue = obj.toString();
111 }
112
113 if ( strValue.length() == 0 )
114 {
115 return "";
116 }
117
118 if ( registry.hasOid( strValue ) )
119 {
120 try
121 {
122 return registry.getOid( strValue );
123 }
124 catch ( NamingException e )
125 {
126 throw new RuntimeException( "Failed to lookup OID for " + strValue, e );
127 }
128 }
129
130 return strValue;
131 }
132 }