From a131ac8d0a7f9563cedacc414ac15df5e717f62a Mon Sep 17 00:00:00 2001 From: Robert Scholte Date: Sun, 2 Nov 2014 13:22:12 +0100 Subject: [PATCH] [MNG-5714] Add Merger for Maven Toolchain implement equals() and hashCode() for DefaultToolchain to help the Merger --- .../maven/toolchain/DefaultToolchain.java | 103 +++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java b/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java index 11ad722037..3aeb243bb2 100644 --- a/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java +++ b/maven-core/src/main/java/org/apache/maven/toolchain/DefaultToolchain.java @@ -19,11 +19,15 @@ package org.apache.maven.toolchain; * under the License. */ +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.apache.maven.toolchain.java.DefaultJavaToolChain; import org.apache.maven.toolchain.model.ToolchainModel; import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.util.xml.Xpp3Dom; /** * @@ -57,12 +61,13 @@ public abstract class DefaultToolchain this.type = type; } + @Override public final String getType() { return type != null ? type : model.getType(); } - + @Override public final ToolchainModel getModel() { return model; @@ -73,7 +78,7 @@ public abstract class DefaultToolchain provides.put( type, matcher ); } - + @Override public boolean matchesRequirements( Map requirements ) { for ( Map.Entry requirement : requirements.entrySet() ) @@ -100,4 +105,98 @@ public abstract class DefaultToolchain { return logger; } + + @Override + public boolean equals( Object obj ) + { + if ( obj == null ) + { + return false; + } + + if ( this == obj ) + { + return true; + } + + if ( !( obj instanceof DefaultToolchain ) ) + { + return false; + } + + DefaultToolchain other = (DefaultToolchain) obj; + + if ( type == null ? other.type != null : !type.equals( other.type ) ) + { + return false; + } + + Xpp3Dom thisProvides = (Xpp3Dom) this.getModel().getProvides(); + Xpp3Dom otherProvides = (Xpp3Dom) other.getModel().getProvides(); + + if ( thisProvides == null ? otherProvides != null : otherProvides == null ) + { + return false; + } + + Xpp3Dom thisId = thisProvides.getChild( "id" ); + Xpp3Dom otherId = otherProvides.getChild( "id" ); + if ( ( thisId == null || "default".equals( thisId.getValue() ) ) + && ( otherId == null || "default".equals( otherId.getValue() ) ) ) + { + return true; + } + + List names = new ArrayList(); + + // collect names of both provides, exclude id + for ( Xpp3Dom thisChild : thisProvides.getChildren() ) + { + if ( "id".equals( thisChild.getName() ) ) + { + continue; + } + names.add( thisChild.getName() ); + } + + for ( Xpp3Dom thisChild : otherProvides.getChildren() ) + { + if ( "id".equals( thisChild.getName() ) ) + { + continue; + } + names.add( thisChild.getName() ); + } + + for ( String name : names ) + { + Xpp3Dom thisChild = thisProvides.getChild( name ); + Xpp3Dom otherChild = otherProvides.getChild( name ); + + if ( thisChild != null ? !thisChild.equals( otherChild ) : otherChild != null ) + { + return false; + } + } + + return true; + } + + @Override + public int hashCode() + { + int hashCode = ( type == null ) ? 0 : type.hashCode(); + + if ( this.getModel().getProvides() != null ) + { + Xpp3Dom providesElm = (Xpp3Dom) this.getModel().getProvides(); + + Xpp3Dom idElm = providesElm.getChild( "id" ); + + String idValue = ( idElm == null ? "default" : idElm.getValue() ); + + hashCode = 31 * hashCode + idValue.hashCode(); + } + return hashCode; + } }