001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.ldap.model.schema.comparators; 021 022 023import org.apache.directory.api.asn1.util.Oid; 024import org.apache.directory.api.ldap.model.schema.LdapComparator; 025import org.apache.directory.api.util.Chars; 026import org.apache.directory.api.util.Strings; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030 031/** 032 * A comparator for Comparators. We compare the OIDs 033 * 034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 035 */ 036public class ObjectIdentifierFirstComponentComparator extends LdapComparator<String> 037{ 038 /** The serial version UID */ 039 private static final long serialVersionUID = 2L; 040 041 /** A logger for this class */ 042 private static final Logger LOG = LoggerFactory.getLogger( ObjectIdentifierFirstComponentComparator.class ); 043 044 045 /** 046 * The ObjectIdentifierFirstComponentComparator constructor. Its OID is the 047 * ObjectIdentifierFirstComponentMatch matching rule OID. 048 */ 049 public ObjectIdentifierFirstComponentComparator( String oid ) 050 { 051 super( oid ); 052 } 053 054 055 /** 056 * Get the OID from the SchemaObject description 057 */ 058 private String getNumericOid( String s ) 059 { 060 // Get the OID from the strings now 061 int pos = 0; 062 063 if ( !Strings.isCharASCII( s, pos++, '(' ) ) 064 { 065 return null; 066 } 067 068 while ( Strings.isCharASCII( s, pos, ' ' ) ) 069 { 070 pos++; 071 } 072 073 int start = pos; 074 075 while ( Chars.isDigit( s, pos ) || Strings.isCharASCII( s, pos, '.' ) ) 076 { 077 pos++; 078 } 079 080 String numericOid = s.substring( start, pos ); 081 082 if ( Oid.isOid( numericOid ) ) 083 { 084 return numericOid; 085 } 086 else 087 { 088 return null; 089 } 090 } 091 092 093 /** 094 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 095 */ 096 public int compare( String s1, String s2 ) 097 { 098 LOG.debug( "comparing ObjectIdentifierFirstComponent objects '{}' with '{}'", s1, s2 ); 099 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}