You are viewing a plain text version of this content. The canonical link for it is here.
Posted to m2-dev@maven.apache.org by tr...@apache.org on 2004/06/20 03:07:05 UTC

cvs commit: maven-components/maven-artifact/src/main/java/org/apache/maven/artifact AbstractMavenArtifact.java

trygvis     2004/06/19 18:07:05

  Modified:    maven-artifact/src/main/java/org/apache/maven/artifact
                        AbstractMavenArtifact.java
  Log:
  o Reimplemented equals(), it was erroneous.
  o Implemented hashCode() which is required when overriding equals().
  
  Revision  Changes    Path
  1.2       +15 -10    maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractMavenArtifact.java
  
  Index: AbstractMavenArtifact.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractMavenArtifact.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractMavenArtifact.java	11 Jun 2004 15:12:24 -0000	1.1
  +++ AbstractMavenArtifact.java	20 Jun 2004 01:07:05 -0000	1.2
  @@ -161,17 +161,22 @@
       {
           return dependency.getType();
       }
  -   
  +
       public boolean equals( Object obj )
       {
  -        MavenArtifact other = ( MavenArtifact ) obj;
  -                
  -        Dependency otherDependency = other.getDependency(); 
  -                
  -        //if paths are the same artifats are the same
  -        boolean retValue = getPath().equals( other.getPath() );
  -        
  -        return retValue;
  -        
  +        if( !( obj instanceof MavenArtifact) )
  +            return false;
  +
  +        MavenArtifact other = (MavenArtifact) obj;
  +
  +        return dependency.getId() == other.getDependency().getId();
  +    }
  +
  +    public long hashCode()
  +    {
  +        return dependency.getGroupId().hashCode() +
  +               dependency.getArtifactId().hashCode() +
  +               dependency.getVersion().hashCode() +
  +               dependency.getType().hashCode();
       }
   }
  
  
  

Re: cvs commit: maven-components/maven-artifact/src/main/java/org/apache/maven/artifact AbstractMavenArtifact.java

Posted by Gilles Dodinet <rh...@free.fr>.
Michal Maczka wrote:

>For example when Artifact collector is working it needs to use
><groupId>:<artifactId>:<type> as id for finding conflicts,
>
>When I have two artifacts of the same id but different version - I know
>that conflict has been found. 
>
>As I see it now for some other functions we will 
>need to use "short" version of  id: <groupId>:<artifactId>:<type>
>
>and for some others "long" version:
><groupId>:<artifactId>:<type>:<version>.
>
>The thing is that at the moment much more often 
>we need to use "short id" and "long id" can be easly generated from
>"short id" but reverse is not so easy to achieve in a clean way.
>
>Maybe in fact we can have two methods in Dependency:
>
>getShortId()  and getLongId()
>
>
>So dIon - yes you are right that apache:struts:jar:1.0 and
>apache:struts:jar:2.0 are two different artifacts but it seems to me
>that for our internal needs we more often have to put them into the same
>bucket.
>  
>
Glad to see this appears in m2. Indeed such comparison methods are 
really needed for integration, and in mevenide we use
util classes to support the exact scheme michal described. equals, 
relaxEquals and strictEquals method names make sense to me where
equals could delegate to relaxEquals or strictEquals. In addition it 
would be very nice if there were a #conflict(AbstractMavenArtifact) method.

Related i have a question about jar override. to specify override we now 
use maven.jar.<artifactId>, however what if same artifactId is shared by 
two
different artifacts, that have either the same groupId (only the type 
differs) or a completly different groupId (maybe same type) ?

-- gd


Re: cvs commit: maven-components/maven-artifact/src/main/java/org/apache/maven/artifact AbstractMavenArtifact.java

Posted by Michal Maczka <mm...@interia.pl>.
On Sun, 2004-06-20 at 04:05, Trygve Laugstøl wrote:
> On Sun, Jun 20, 2004 at 11:45:52AM +1000, Dion Gillard wrote:
> > On Sat, 19 Jun 2004 21:40:09 -0400, Trygve Laugstøl <tr...@apache.org> wrote:
> > > 
> > > The id of a artifact/dependency is
> > > 
> > >  <groupId>:<artifactId>:<type>
> > > 
> > > so I guess the version should be removed from the hashCode() then?
> > 
> > I think version should be part of the uniqeness of an artifact. If I
> > have two artifacts, e.g.
> > 
> > apache:struts:jar:1.0 and apache:struts:jar:2.0, they're definitely
> > not 'equal', right?
> 
it's not so clear as it seems. 

For example when Artifact collector is working it needs to use
<groupId>:<artifactId>:<type> as id for finding conflicts,

When I have two artifacts of the same id but different version - I know
that conflict has been found. 

As I see it now for some other functions we will 
need to use "short" version of  id: <groupId>:<artifactId>:<type>

and for some others "long" version:
<groupId>:<artifactId>:<type>:<version>.

The thing is that at the moment much more often 
we need to use "short id" and "long id" can be easly generated from
"short id" but reverse is not so easy to achieve in a clean way.

Maybe in fact we can have two methods in Dependency:

getShortId()  and getLongId()


So dIon - yes you are right that apache:struts:jar:1.0 and
apache:struts:jar:2.0 are two different artifacts but it seems to me
that for our internal needs we more often have to put them into the same
bucket.




> You are right, the version will be added to Dependency.getId() in the
> model.

This can have many side effects of existing code. I am almost sure it
will break ArtifactCollector.
> 
> I'll let the hashCode() be as it is for now and we'll add version to the
> id ASAP


Michal



Re: cvs commit: maven-components/maven-artifact/src/main/java/org/apache/maven/artifact AbstractMavenArtifact.java

Posted by Trygve Laugstøl <tr...@apache.org>.
On Sun, Jun 20, 2004 at 11:45:52AM +1000, Dion Gillard wrote:
> On Sat, 19 Jun 2004 21:40:09 -0400, Trygve Laugstøl <tr...@apache.org> wrote:
> > 
> > The id of a artifact/dependency is
> > 
> >  <groupId>:<artifactId>:<type>
> > 
> > so I guess the version should be removed from the hashCode() then?
> 
> I think version should be part of the uniqeness of an artifact. If I
> have two artifacts, e.g.
> 
> apache:struts:jar:1.0 and apache:struts:jar:2.0, they're definitely
> not 'equal', right?

You are right, the version will be added to Dependency.getId() in the
model.

I'll let the hashCode() be as it is for now and we'll add version to the
id ASAP

Re: cvs commit: maven-components/maven-artifact/src/main/java/org/apache/maven/artifact AbstractMavenArtifact.java

Posted by Dion Gillard <di...@gmail.com>.
On Sat, 19 Jun 2004 21:40:09 -0400, Trygve Laugstøl <tr...@apache.org> wrote:
> 
> The id of a artifact/dependency is
> 
>  <groupId>:<artifactId>:<type>
> 
> so I guess the version should be removed from the hashCode() then?

I think version should be part of the uniqeness of an artifact. If I
have two artifacts, e.g.

apache:struts:jar:1.0 and apache:struts:jar:2.0, they're definitely
not 'equal', right?

Re: cvs commit: maven-components/maven-artifact/src/main/java/org/apache/maven/artifact AbstractMavenArtifact.java

Posted by Trygve Laugstøl <tr...@apache.org>.
The id of a artifact/dependency is

 <groupId>:<artifactId>:<type>

so I guess the version should be removed from the hashCode() then?

On Sun, Jun 20, 2004 at 11:30:32AM +1000, Dion Gillard wrote:
> "If two objects are equal according to the equals(Object)  method,
> then calling the hashCode method on each of the two objects must
> produce the same integer result."
> 
> From what I can tell if two artifacts have the same Id, this code
> returns true for equals, but the two artifacts may have a different
> version or type, and their hashcodes will not be equal.
> 
> AFAICT this is broken.
> 
> On 20 Jun 2004 01:07:05 -0000, trygvis@apache.org <tr...@apache.org> wrote:
> > 
> > trygvis     2004/06/19 18:07:05
> > 
> >   Modified:    maven-artifact/src/main/java/org/apache/maven/artifact
> >                         AbstractMavenArtifact.java
> >   Log:
> >   o Reimplemented equals(), it was erroneous.
> >   o Implemented hashCode() which is required when overriding equals().
> > 
> >   Revision  Changes    Path
> >   1.2       +15 -10    maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractMavenArtifact.java
> > 
> >   Index: AbstractMavenArtifact.java
> >   ===================================================================
> >   RCS file: /home/cvs/maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractMavenArtifact.java,v
> >   retrieving revision 1.1
> >   retrieving revision 1.2
> >   diff -u -r1.1 -r1.2
> >   --- AbstractMavenArtifact.java        11 Jun 2004 15:12:24 -0000      1.1
> >   +++ AbstractMavenArtifact.java        20 Jun 2004 01:07:05 -0000      1.2
> >   @@ -161,17 +161,22 @@
> >        {
> >            return dependency.getType();
> >        }
> >   -
> >   +
> >        public boolean equals( Object obj )
> >        {
> >   -        MavenArtifact other = ( MavenArtifact ) obj;
> >   -
> >   -        Dependency otherDependency = other.getDependency();
> >   -
> >   -        //if paths are the same artifats are the same
> >   -        boolean retValue = getPath().equals( other.getPath() );
> >   -
> >   -        return retValue;
> >   -
> >   +        if( !( obj instanceof MavenArtifact) )
> >   +            return false;
> >   +
> >   +        MavenArtifact other = (MavenArtifact) obj;
> >   +
> >   +        return dependency.getId() == other.getDependency().getId();
> >   +    }
> >   +
> >   +    public long hashCode()
> >   +    {
> >   +        return dependency.getGroupId().hashCode() +
> >   +               dependency.getArtifactId().hashCode() +
> >   +               dependency.getVersion().hashCode() +
> >   +               dependency.getType().hashCode();
> >        }
> >    }

Re: cvs commit: maven-components/maven-artifact/src/main/java/org/apache/maven/artifact AbstractMavenArtifact.java

Posted by Dion Gillard <di...@gmail.com>.
"If two objects are equal according to the equals(Object)  method,
then calling the hashCode method on each of the two objects must
produce the same integer result."

>From what I can tell if two artifacts have the same Id, this code
returns true for equals, but the two artifacts may have a different
version or type, and their hashcodes will not be equal.

AFAICT this is broken.

On 20 Jun 2004 01:07:05 -0000, trygvis@apache.org <tr...@apache.org> wrote:
> 
> trygvis     2004/06/19 18:07:05
> 
>   Modified:    maven-artifact/src/main/java/org/apache/maven/artifact
>                         AbstractMavenArtifact.java
>   Log:
>   o Reimplemented equals(), it was erroneous.
>   o Implemented hashCode() which is required when overriding equals().
> 
>   Revision  Changes    Path
>   1.2       +15 -10    maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractMavenArtifact.java
> 
>   Index: AbstractMavenArtifact.java
>   ===================================================================
>   RCS file: /home/cvs/maven-components/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractMavenArtifact.java,v
>   retrieving revision 1.1
>   retrieving revision 1.2
>   diff -u -r1.1 -r1.2
>   --- AbstractMavenArtifact.java        11 Jun 2004 15:12:24 -0000      1.1
>   +++ AbstractMavenArtifact.java        20 Jun 2004 01:07:05 -0000      1.2
>   @@ -161,17 +161,22 @@
>        {
>            return dependency.getType();
>        }
>   -
>   +
>        public boolean equals( Object obj )
>        {
>   -        MavenArtifact other = ( MavenArtifact ) obj;
>   -
>   -        Dependency otherDependency = other.getDependency();
>   -
>   -        //if paths are the same artifats are the same
>   -        boolean retValue = getPath().equals( other.getPath() );
>   -
>   -        return retValue;
>   -
>   +        if( !( obj instanceof MavenArtifact) )
>   +            return false;
>   +
>   +        MavenArtifact other = (MavenArtifact) obj;
>   +
>   +        return dependency.getId() == other.getDependency().getId();
>   +    }
>   +
>   +    public long hashCode()
>   +    {
>   +        return dependency.getGroupId().hashCode() +
>   +               dependency.getArtifactId().hashCode() +
>   +               dependency.getVersion().hashCode() +
>   +               dependency.getType().hashCode();
>        }
>    }
> 
>