001    package 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    
022    import org.apache.maven.artifact.versioning.ArtifactVersion;
023    import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
024    import org.codehaus.plexus.component.annotations.Component;
025    import org.codehaus.plexus.component.annotations.Configuration;
026    
027    /**
028     * @author <a href="mailto:oleg@codehaus.org">Oleg Gusakov</a>
029     *
030     */
031    @Component( role = GraphConflictResolutionPolicy.class )
032    public class DefaultGraphConflictResolutionPolicy
033        implements GraphConflictResolutionPolicy
034    {
035        /**
036         * artifact, closer to the entry point, is selected
037         */
038        @Configuration( name = "closer-first", value = "true" )
039        private boolean closerFirst = true;
040    
041        /**
042         * newer artifact is selected
043         */
044        @Configuration( name = "newer-first", value = "true" )
045        private boolean newerFirst = true;
046    
047        public MetadataGraphEdge apply( MetadataGraphEdge e1, MetadataGraphEdge e2 )
048        {
049            int depth1 = e1.getDepth();
050            int depth2 = e2.getDepth();
051    
052            if ( depth1 == depth2 )
053            {
054                ArtifactVersion v1 = new DefaultArtifactVersion( e1.getVersion() );
055                ArtifactVersion v2 = new DefaultArtifactVersion( e2.getVersion() );
056    
057                if ( newerFirst )
058                {
059                    return v1.compareTo( v2 ) > 0 ? e1 : e2;
060                }
061    
062                return v1.compareTo( v2 ) > 0 ? e2 : e1;
063            }
064    
065            if ( closerFirst )
066            {
067                return depth1 < depth2 ? e1 : e2;
068            }
069    
070            return depth1 < depth2 ? e2 : e1;
071        }
072    
073    }