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 * @param oid The Comparator's OID 050 */ 051 public ObjectIdentifierFirstComponentComparator( String oid ) 052 { 053 super( oid ); 054 } 055 056 057 /** 058 * Get the OID from the SchemaObject description 059 */ 060 private String getNumericOid( String s ) 061 { 062 // Get the OID from the strings now 063 int pos = 0; 064 065 if ( !Strings.isCharASCII( s, pos++, '(' ) ) 066 { 067 return null; 068 } 069 070 while ( Strings.isCharASCII( s, pos, ' ' ) ) 071 { 072 pos++; 073 } 074 075 int start = pos; 076 077 while ( Chars.isDigit( s, pos ) || Strings.isCharASCII( s, pos, '.' ) ) 078 { 079 pos++; 080 } 081 082 String numericOid = s.substring( start, pos ); 083 084 if ( Oid.isOid( numericOid ) ) 085 { 086 return numericOid; 087 } 088 else 089 { 090 return null; 091 } 092 } 093 094 095 /** 096 * {@inheritDoc} 097 */ 098 public int compare( String s1, String s2 ) 099 { 100 LOG.debug( "comparing ObjectIdentifierFirstComponent objects '{}' with '{}'", s1, s2 ); 101 102 // ------------------------------------------------------------------- 103 // Handle some basis cases 104 // ------------------------------------------------------------------- 105 if ( s1 == null ) 106 { 107 return ( s2 == null ) ? 0 : -1; 108 } 109 110 if ( s2 == null ) 111 { 112 return -1; 113 } 114 115 // Let's try to avoid a parse. 116 if ( s1.equals( s2 ) ) 117 { 118 return 0; 119 } 120 121 // Get the OID from the strings now 122 String oid1 = getNumericOid( s1 ); 123 124 if ( oid1 == null ) 125 { 126 return -1; 127 } 128 129 String oid2 = getNumericOid( s2 ); 130 131 if ( oid2 == null ) 132 { 133 return -1; 134 } 135 136 if ( oid1.equals( oid2 ) ) 137 { 138 return 0; 139 } 140 else 141 { 142 return -1; 143 } 144 } 145}