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.shared.ldap.util;
021
022 import java.io.Serializable;
023 import java.util.Comparator;
024
025
026 /**
027 * Compares Long keys and values within a table.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 * @version $Revision: 437007 $
031 */
032 public class LongComparator implements Comparator<Object>, Serializable
033 {
034 /** A instance of this comparator */
035 public static final LongComparator INSTANCE = new LongComparator();
036
037 /**
038 * Version id for serialization.
039 */
040 static final long serialVersionUID = 1L;
041
042
043 /**
044 * Compare two objects.
045 *
046 * @param obj1 First object
047 * @param obj2 Second object
048 * @return 1 if obj1 > obj2, 0 if obj1 == obj2, -1 if obj1 < obj2
049 */
050 public int compare( Object obj1, Object obj2 )
051 {
052 try
053 {
054 Long long1 = (Long)obj1;
055 Long long2 = (Long)obj2;
056 return long1.compareTo( long2 );
057 }
058 catch ( NullPointerException npe )
059 {
060 if ( obj1 == null )
061 {
062 throw new IllegalArgumentException( "Argument 'obj1' is null" );
063 }
064 else
065 {
066 throw new IllegalArgumentException( "Argument 'obj2' is null" );
067 }
068 }
069 }
070 }