001package 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
022import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
023import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
024import org.apache.maven.artifact.versioning.VersionRange;
025
026/**
027 *
028 * @author mkleint
029 */
030public 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        @Override
058        public boolean matches( String requirement )
059        {
060            return provides.equalsIgnoreCase( requirement );
061        }
062        
063        @Override
064        public String toString()
065        {
066            return provides;
067        }
068    }
069
070    private static final class VersionMatcher
071        implements RequirementMatcher
072    {
073        DefaultArtifactVersion version;
074
075        private VersionMatcher( String version )
076        {
077            this.version = new DefaultArtifactVersion( version );
078        }
079
080        @Override
081        public boolean matches( String requirement )
082        {
083            try
084            {
085                VersionRange range = VersionRange.createFromVersionSpec( requirement );
086                if ( range.hasRestrictions() )
087                {
088                    return range.containsVersion( version );
089                }
090                else
091                {
092                    return range.getRecommendedVersion().compareTo( version ) == 0;
093                }
094            }
095            catch ( InvalidVersionSpecificationException ex )
096            {
097                //TODO error reporting
098                ex.printStackTrace();
099                return false;
100            }
101        }
102        
103        @Override
104        public String toString()
105        {
106            return version.toString();
107        }
108    }
109}