Return-Path: Delivered-To: apmail-jakarta-avalon-cvs-archive@apache.org Received: (qmail 75732 invoked from network); 21 Nov 2002 23:19:23 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 21 Nov 2002 23:19:23 -0000 Received: (qmail 25895 invoked by uid 97); 21 Nov 2002 23:20:29 -0000 Delivered-To: qmlist-jakarta-archive-avalon-cvs@jakarta.apache.org Received: (qmail 25858 invoked by uid 97); 21 Nov 2002 23:20:28 -0000 Mailing-List: contact avalon-cvs-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Avalon CVS List" Reply-To: "Avalon Developers List" Delivered-To: mailing list avalon-cvs@jakarta.apache.org Received: (qmail 25846 invoked by uid 98); 21 Nov 2002 23:20:27 -0000 X-Antivirus: nagoya (v4218 created Aug 14 2002) Date: 21 Nov 2002 23:19:17 -0000 Message-ID: <20021121231917.20374.qmail@icarus.apache.org> From: proyal@apache.org To: jakarta-avalon-cvs@apache.org Subject: cvs commit: jakarta-avalon/src/java/org/apache/avalon/framework/configuration ConfigurationUtil.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N proyal 2002/11/21 15:19:17 Modified: src/java/org/apache/avalon/framework/configuration ConfigurationUtil.java Log: Merge FORREST-branch changes to fix mr Gump. Revision Changes Path 1.6 +120 -1 jakarta-avalon/src/java/org/apache/avalon/framework/configuration/ConfigurationUtil.java Index: ConfigurationUtil.java =================================================================== RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/configuration/ConfigurationUtil.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ConfigurationUtil.java 7 Nov 2002 08:35:27 -0000 1.5 +++ ConfigurationUtil.java 21 Nov 2002 23:19:17 -0000 1.6 @@ -13,12 +13,16 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; /** * This class has a bunch of utility methods to work * with configuration objects. * * @author Peter Donald + * @author Peter Royal * @version $Revision$ $Date$ */ public class ConfigurationUtil @@ -50,6 +54,121 @@ { throw new IllegalStateException( pce.toString() ); } + } + + /** + * Test to see if two Configuration's can be considered the same. Name, value, attributes + * and children are test. The order of children is not taken into consideration + * for equality. + * + * @param c1 Configuration to test + * @param c2 Configuration to test + * @return true if the configurations can be considered equals + */ + public static boolean equals( final Configuration c1, final Configuration c2 ) + { + return c1.getName().equals( c2.getName() ) + && areValuesEqual( c1, c2 ) + && areAttributesEqual( c1, c2 ) + && areChildrenEqual( c1, c2 ); + } + + /** + * Return true if the children of both configurations are equal. + * + * @param c1 configuration1 + * @param c2 configuration2 + * @return true if the children of both configurations are equal. + */ + private static boolean areChildrenEqual( final Configuration c1, + final Configuration c2 ) + { + final Configuration[] kids1 = c1.getChildren(); + final ArrayList kids2 = new ArrayList( Arrays.asList( c2.getChildren() ) ); + if( kids1.length != kids2.size() ) + { + return false; + } + + for( int i = 0; i < kids1.length; i++ ) + { + if( !findMatchingChild( kids1[ i ], kids2 ) ) + { + return false; + } + } + + return kids2.isEmpty() ? true : false; + } + + /** + * Return true if find a matching child and remove child from list. + * + * @param c the configuration + * @param matchAgainst the list of items to match against + * @return true if the found. + */ + private static boolean findMatchingChild( final Configuration c, + final ArrayList matchAgainst ) + { + final Iterator i = matchAgainst.iterator(); + while( i.hasNext() ) + { + if( equals( c, (Configuration)i.next() ) ) + { + i.remove(); + return true; + } + } + + return false; + } + + /** + * Return true if the attributes of both configurations are equal. + * + * @param c1 configuration1 + * @param c2 configuration2 + * @return true if the attributes of both configurations are equal. + */ + private static boolean areAttributesEqual( final Configuration c1, + final Configuration c2 ) + { + final String[] names1 = c1.getAttributeNames(); + final String[] names2 = c2.getAttributeNames(); + if( names1.length != names2.length ) + { + return false; + } + + for( int i = 0; i < names1.length; i++ ) + { + final String name = names1[ i ]; + final String value1 = c1.getAttribute( name, null ); + final String value2 = c2.getAttribute( name, null ); + if( !value1.equals( value2 ) ) + { + return false; + } + } + + return true; + } + + /** + * Return true if the values of two configurations are equal. + * + * @param c1 configuration1 + * @param c2 configuration2 + * @return true if the values of two configurations are equal. + */ + private static boolean areValuesEqual( final Configuration c1, + final Configuration c2 ) + { + final String value1 = c1.getValue( null ); + final String value2 = c2.getValue( null ); + return ( value1 == null && value2 == null ) || + ( value1 != null && value1.equals( value2 ) ); } /** -- To unsubscribe, e-mail: For additional commands, e-mail: