You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/12/19 20:29:44 UTC

svn commit: r357761 - /directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/AttributeTypeAndValue.java

Author: elecharny
Date: Mon Dec 19 11:29:40 2005
New Revision: 357761

URL: http://svn.apache.org/viewcvs?rev=357761&view=rev
Log:
- added some logs
- fixed the compareTo methods
- added some tests in constructor to avoi dnull types

Modified:
    directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/AttributeTypeAndValue.java

Modified: directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/AttributeTypeAndValue.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/AttributeTypeAndValue.java?rev=357761&r1=357760&r2=357761&view=diff
==============================================================================
--- directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/AttributeTypeAndValue.java (original)
+++ directory/shared/ldap/branches/DN-refactoring/common/src/main/java/org/apache/ldap/common/name/AttributeTypeAndValue.java Mon Dec 19 11:29:40 2005
@@ -19,6 +19,8 @@
 import javax.naming.InvalidNameException;
 
 import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * A Attribute Type And Value, which is the basis of all RDN.
@@ -37,13 +39,18 @@
  */
 public class AttributeTypeAndValue implements Cloneable, Comparable
 {
+    /** The LoggerFactory used by this Interceptor */
+    private static Logger log = LoggerFactory.getLogger( AttributeTypeAndValue.class );
+
     /** The Name type */
     private String type;
     
     /** The name value */
     private String value;
     
+    /** Two values used for comparizon */
     private static final boolean CASE_SENSITIVE = false;
+    private static final boolean CASE_INSENSITIVE = true;
     
     /**
      * Construct an empty AttributeTypeAndValue
@@ -64,8 +71,14 @@
      */ 
     public AttributeTypeAndValue( String type, String value ) throws InvalidNameException
     {
+    	if ( StringUtils.isEmpty( type ) || StringUtils.isEmpty( type.trim() ) )
+    	{
+    		log.error( "The type cannot be empty or null" );
+    		throw new InvalidNameException( "Null or empty type is not allowed" );
+    	}
+    	
         this.type = type;
-        this.value = value;
+        this.value = StringUtils.isEmpty( value ) ? "" : value;
     }
     
     /**
@@ -161,13 +174,14 @@
     }
     
     /**
-     * Compares two NamezComponent. They are equals if :
-     * - types are equals,
-     * - values are equals
-     * - comparizon are case insensitive
+     * Compares two NameComponents. They are equals if :
+     * - types are equals, case insensitive,
+     * - values are equals, case sensitive
      * 
      * @param object
-     * @return
+     * @return 0 if both NC are equals, otherwise a positive value if
+     * the original NC is superior to the second one, a negative value 
+     * if the second NC is superior.
      */
     public int compareTo( Object object )
     {
@@ -175,11 +189,53 @@
         {
             AttributeTypeAndValue nc = (AttributeTypeAndValue)object;
             
-            return ( compareType( type, nc.type ) && compareValue( value, nc.value, CASE_SENSITIVE ) ? 0 : -1 );
+            int res = compareType( type, nc.type );
+            
+            if ( res != 0 )
+            {
+            	return res;
+            }
+            else
+            {
+            	return compareValue( value, nc.value, CASE_SENSITIVE );
+            }
         }
         else
         {
-            return -1;
+            return 1;
+        }
+    }
+    
+    /**
+     * Compares two NameComponents. They are equals if :
+     * - types are equals, case insensitive,
+     * - values are equals, case insensitive
+     * 
+     * @param object
+     * @return 0 if both NC are equals, otherwise a positive value if
+     * the original NC is superior to the second one, a negative value 
+     * if the second NC is superior.
+     */
+    public int compareToIgnoreCase( Object object )
+    {
+        if ( object instanceof AttributeTypeAndValue )
+        {
+            AttributeTypeAndValue nc = (AttributeTypeAndValue)object;
+            
+            int res = compareType( type, nc.type );
+            
+            if ( res != 0 )
+            {
+            	return res;
+            }
+            else
+            {
+            	return compareValue( value, nc.value, CASE_INSENSITIVE );
+            }
+        }
+        else
+        {
+            return 1;
         }
     }
     
@@ -189,19 +245,19 @@
      * @param val2 Second String
      * @return true if both strings are equals or null.
      */
-    private boolean compareType( String val1, String val2 )
+    private int compareType( String val1, String val2 )
     {
         if ( StringUtils.isEmpty( val1 ) )
         {
-            return StringUtils.isEmpty( val2 );
+            return StringUtils.isEmpty( val2 ) ? 0 : -1;
         }
         else if ( StringUtils.isEmpty( val2 ) )
         {
-            return false;
+            return 1;
         }
         else
         {
-            return ( StringUtils.lowerCase( StringUtils.trim( val1 ) ) ).equals(StringUtils.lowerCase( StringUtils.trim( val2 ) ) );
+            return ( StringUtils.trim( val1 ) ).compareToIgnoreCase( StringUtils.trim( val2 ) );
         }
     }
 
@@ -211,25 +267,25 @@
      * @param val2 Second String
      * @return true if both strings are equals or null.
      */
-    private boolean compareValue( String val1, String val2, boolean caseSensitive )
+    private int compareValue( String val1, String val2, boolean caseSensitive )
     {
         if ( StringUtils.isEmpty( val1 ) )
         {
-            return StringUtils.isEmpty( val2 );
+            return StringUtils.isEmpty( val2 ) ? 0 : -1;
         }
         else if ( StringUtils.isEmpty( val2 ) )
         {
-            return false;
+            return 1;
         }
         else
         {
         	if ( caseSensitive )
         	{
-        		return ( StringUtils.lowerCase( StringUtils.trim( val1 ) ) ).equals(StringUtils.lowerCase( StringUtils.trim( val2 ) ) );
+        		return ( StringUtils.trim( val1 ) ).compareToIgnoreCase( StringUtils.trim( val2 ) );
         	}
         	else
         	{
-        		return ( StringUtils.trim( val1 ) ).equals( StringUtils.trim( val2 ) );
+        		return ( StringUtils.trim( val1 ) ).compareTo( StringUtils.trim( val2 ) );
         	}
         }
     }
@@ -256,7 +312,17 @@
     {
         StringBuffer sb = new StringBuffer();
         
-        sb.append( type ).append( "=" ).append( value );
+        if ( StringUtils.isEmpty( type ) || StringUtils.isEmpty( type.trim() ) )
+        {
+        	return "";
+        }
+        
+        sb.append( type ).append( "=" );
+        
+        if ( value != null )
+        {
+        	sb.append( value );
+        }
         
         return sb.toString();
     }