001package org.apache.maven.repository.metadata;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.artifact.ArtifactScopeEnum;
023
024/**
025 * metadata graph vertice - just a wrapper around artifact's metadata
026 *
027 * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
028 */
029public class MetadataGraphVertex
030    implements Comparable<MetadataGraphVertex>
031{
032    ArtifactMetadata md;
033
034    // indications to use these in comparrison
035    private boolean compareVersion = false;
036    private boolean compareScope   = false;
037
038    public MetadataGraphVertex( ArtifactMetadata md )
039    {
040        super();
041        this.md = md;
042    }
043
044    public MetadataGraphVertex( ArtifactMetadata md, boolean compareVersion, boolean compareScope )
045    {
046        this( md );
047        this.compareVersion = compareVersion;
048        this.compareScope = compareScope;
049    }
050
051    public ArtifactMetadata getMd()
052    {
053        return md;
054    }
055
056    public void setMd( ArtifactMetadata md )
057    {
058        this.md = md;
059    }
060
061    // ---------------------------------------------------------------------
062    public boolean isCompareVersion()
063    {
064        return compareVersion;
065    }
066
067    public void setCompareVersion( boolean compareVersion )
068    {
069        this.compareVersion = compareVersion;
070    }
071
072    public boolean isCompareScope()
073    {
074        return compareScope;
075    }
076
077    public void setCompareScope( boolean compareScope )
078    {
079        this.compareScope = compareScope;
080    }
081
082    // ---------------------------------------------------------------------
083    @Override
084    public String toString()
085    {
086        return "[" + ( md == null ? "no metadata" : md.toString() ) + "]";
087    }
088
089    // ---------------------------------------------------------------------
090    private static int compareStrings( String s1, String s2 )
091    {
092        if ( s1 == null && s2 == null )
093        {
094            return 0;
095        }
096
097        if ( s1 == null /* && s2 != null */ )
098        {
099            return -1;
100        }
101
102        if ( /* s1 != null && */ s2 == null )
103        {
104            return 1;
105        }
106
107        return s1.compareTo( s2 );
108    }
109
110    // ---------------------------------------------------------------------
111    public int compareTo( MetadataGraphVertex vertex )
112    {
113        if ( vertex == null || vertex.getMd() == null )
114        {
115            return 1;
116        }
117
118        ArtifactMetadata vmd = vertex.getMd();
119
120        if ( vmd == null )
121        {
122            if ( md == null )
123            {
124                return 0;
125            }
126            else
127            {
128                return 1;
129            }
130        }
131
132        int g = compareStrings( md.groupId, vmd.groupId );
133
134        if ( g == 0 )
135        {
136            int a = compareStrings( md.artifactId, vmd.artifactId );
137            if ( a == 0 )
138            {
139                if ( compareVersion )
140                {
141                    int v = compareStrings( md.version, vmd.version );
142                    if ( v == 0 )
143                    {
144                        if ( compareScope )
145                        {
146                            String s1 = ArtifactScopeEnum.checkScope( md.artifactScope ).getScope();
147                            String s2 = ArtifactScopeEnum.checkScope( vmd.artifactScope ).getScope();
148                            return s1.compareTo( s2 );
149                        }
150                        else
151                        {
152                            return 0;
153                        }
154                    }
155                    else
156                    {
157                        return v;
158                    }
159                }
160                else
161                {
162                    return 0;
163                }
164            }
165            else
166            {
167                return a;
168            }
169        }
170
171        return g;
172    }
173
174    // ---------------------------------------------------------------------
175    @Override
176    public boolean equals( Object vo )
177    {
178        if ( vo == null || !( vo instanceof MetadataGraphVertex ) )
179        {
180            return false;
181        }
182        return compareTo( (MetadataGraphVertex) vo ) == 0;
183    }
184
185    // ---------------------------------------------------------------------
186
187    @Override
188    public int hashCode()
189    {
190        if ( md == null )
191        {
192            return super.hashCode();
193        }
194        StringBuilder hashString = new StringBuilder( 128 );
195        hashString.append( md.groupId ).append( "|" );
196        hashString.append( md.artifactId ).append( "|" );
197
198        if ( compareVersion )
199        {
200            hashString.append( md.version ).append( "|" );
201        }
202
203        if ( compareScope )
204        {
205            hashString.append( md.getArtifactScope() ).append( "|" );
206        }
207
208        return hashString.toString().hashCode();
209
210        // BASE64Encoder b64 = new BASE64Encoder();
211        // return b64.encode( hashString.toString().getBytes() ).hashCode();
212    }
213
214    // ---------------------------------------------------------------------
215    // ---------------------------------------------------------------------
216}