1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.api.ldap.model.schema.comparators;
21
22
23 import org.apache.directory.api.asn1.util.Oid;
24 import org.apache.directory.api.ldap.model.schema.LdapComparator;
25 import org.apache.directory.api.util.Chars;
26 import org.apache.directory.api.util.Strings;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 /**
32 * A comparator for Comparators. We compare the OIDs
33 *
34 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35 */
36 public class ObjectIdentifierFirstComponentComparator extends LdapComparator<String>
37 {
38 /** The serial version UID */
39 private static final long serialVersionUID = 2L;
40
41 /** A logger for this class */
42 private static final Logger LOG = LoggerFactory.getLogger( ObjectIdentifierFirstComponentComparator.class );
43
44
45 /**
46 * The ObjectIdentifierFirstComponentComparator constructor. Its OID is the
47 * ObjectIdentifierFirstComponentMatch matching rule OID.
48 */
49 public ObjectIdentifierFirstComponentComparator( String oid )
50 {
51 super( oid );
52 }
53
54
55 /**
56 * Get the OID from the SchemaObject description
57 */
58 private String getNumericOid( String s )
59 {
60 // Get the OID from the strings now
61 int pos = 0;
62
63 if ( !Strings.isCharASCII( s, pos++, '(' ) )
64 {
65 return null;
66 }
67
68 while ( Strings.isCharASCII( s, pos, ' ' ) )
69 {
70 pos++;
71 }
72
73 int start = pos;
74
75 while ( Chars.isDigit( s, pos ) || Strings.isCharASCII( s, pos, '.' ) )
76 {
77 pos++;
78 }
79
80 String numericOid = s.substring( start, pos );
81
82 if ( Oid.isOid( numericOid ) )
83 {
84 return numericOid;
85 }
86 else
87 {
88 return null;
89 }
90 }
91
92
93 /**
94 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
95 */
96 public int compare( String s1, String s2 )
97 {
98 LOG.debug( "comparing ObjectIdentifierFirstComponent objects '{}' with '{}'", s1, s2 );
99
100 // -------------------------------------------------------------------
101 // Handle some basis cases
102 // -------------------------------------------------------------------
103 if ( s1 == null )
104 {
105 return ( s2 == null ) ? 0 : -1;
106 }
107
108 if ( s2 == null )
109 {
110 return -1;
111 }
112
113 // Let's try to avoid a parse.
114 if ( s1.equals( s2 ) )
115 {
116 return 0;
117 }
118
119 // Get the OID from the strings now
120 String oid1 = getNumericOid( s1 );
121
122 if ( oid1 == null )
123 {
124 return -1;
125 }
126
127 String oid2 = getNumericOid( s2 );
128
129 if ( oid2 == null )
130 {
131 return -1;
132 }
133
134 if ( oid1.equals( oid2 ) )
135 {
136 return 0;
137 }
138 else
139 {
140 return -1;
141 }
142 }
143 }