001    package org.apache.maven.toolchain;
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.DefaultArtifactVersion;
023    import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
024    import org.apache.maven.artifact.versioning.VersionRange;
025    
026    /**
027     *
028     * @author mkleint
029     */
030    public final class RequirementMatcherFactory
031    {
032        private RequirementMatcherFactory()
033        {
034        }
035    
036        public static RequirementMatcher createExactMatcher( String provideValue )
037        {
038            return new ExactMatcher( provideValue );
039        }
040    
041        public static RequirementMatcher createVersionMatcher( String provideValue )
042        {
043            return new VersionMatcher( provideValue );
044        }
045    
046        private static final class ExactMatcher
047            implements RequirementMatcher
048        {
049    
050            private String provides;
051    
052            private ExactMatcher( String provides )
053            {
054                this.provides = provides;
055            }
056    
057            public boolean matches( String requirement )
058            {
059                return provides.equalsIgnoreCase( requirement );
060            }
061        }
062    
063        private static final class VersionMatcher
064            implements RequirementMatcher
065        {
066    
067            DefaultArtifactVersion version;
068    
069            private VersionMatcher( String version )
070            {
071                this.version = new DefaultArtifactVersion( version );
072            }
073    
074            public boolean matches( String requirement )
075            {
076                try
077                {
078                    VersionRange range = VersionRange.createFromVersionSpec( requirement );
079                    if ( range.hasRestrictions() )
080                    {
081                        return range.containsVersion( version );
082                    }
083                    else
084                    {
085                        return range.getRecommendedVersion().compareTo( version ) == 0;
086                    }
087                }
088                catch ( InvalidVersionSpecificationException ex )
089                {
090                    //TODO error reporting
091                    ex.printStackTrace();
092                    return false;
093                }
094            }
095        }
096    }