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 */
020 package org.apache.directory.server.schema.bootstrap;
021
022
023 import java.io.Serializable;
024 import java.util.Comparator;
025
026 import javax.naming.NamingException;
027
028 import org.apache.directory.server.schema.registries.OidRegistry;
029 import org.apache.directory.server.schema.registries.Registries;
030 import org.apache.directory.shared.ldap.util.StringTools;
031
032
033 /**
034 * A comparator that sorts OIDs based on their numeric id value. Needs a
035 * OidRegistry to properly do it's job. Public method to set the oid
036 * registry will be used by the server after instantiation in deserialization.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 * @version $Rev$
040 */
041 public class NameOrNumericIdComparator implements Comparator, Serializable
042 {
043 private static final long serialVersionUID = 1L;
044 private transient OidRegistry registry;
045
046
047 public NameOrNumericIdComparator( OidRegistry registry )
048 {
049 this.registry = registry;
050 }
051
052
053 public NameOrNumericIdComparator()
054 {
055 }
056
057
058 /* (non-Javadoc)
059 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
060 */
061 public int compare( Object o1, Object o2 )
062 {
063 String s1 = getNumericIdString( o1 );
064 String s2 = getNumericIdString( o2 );
065
066 if ( s1 == null && s2 == null )
067 {
068 return 0;
069 }
070
071 if ( s1 == null )
072 {
073 return -1;
074 }
075
076 if ( s2 == null )
077 {
078 return 1;
079 }
080
081 return s1.compareTo( s2 );
082 }
083
084
085 public void setRegistries( Registries registries )
086 {
087 registry = registries.getOidRegistry();
088 }
089
090
091 String getNumericIdString( Object obj )
092 {
093 String strValue;
094
095 if ( obj == null )
096 {
097 return null;
098 }
099
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 }