Changeset 362

Show
Ignore:
Timestamp:
03/12/07 07:22:39 (2 years ago)
Author:
mikey
Message:

created org.apache.maven.artifact.versioning

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/org/apache/maven/artifact/versioning/mvnVersionRange.php

    r350 r362  
    11<?php 
    2 class MavenVersionRange 
     2/* 
     3 * Licensed to the Apache Software Foundation (ASF) under one 
     4 * or more contributor license agreements.  See the NOTICE file 
     5 * distributed with this work for additional information 
     6 * regarding copyright ownership.  The ASF licenses this file 
     7 * to you under the Apache License, Version 2.0 (the 
     8 * "License"); you may not use this file except in compliance 
     9 * with the License.  You may obtain a copy of the License at 
     10 * 
     11 *  http://www.apache.org/licenses/LICENSE-2.0 
     12 * 
     13 * Unless required by applicable law or agreed to in writing, 
     14 * software distributed under the License is distributed on an 
     15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
     16 * KIND, either express or implied.  See the License for the 
     17 * specific language governing permissions and limitations 
     18 * under the License. 
     19 */ 
     20require_once dirname(__FILE__) . '/mvnInvalidVersionSpecificationException.php'; 
     21require_once dirname(__FILE__) . '/mvnOverConstrainedVersionException.php'; 
     22require_once dirname(__FILE__) . '/mvnRestriction.php'; 
     23/** 
     24 * Constructs a version range from a specification. 
     25 * 
     26 * @author      Brett Porter <brett@apache.org> 
     27 * @author      Frank Kleine <mikey@stubbles.net> 
     28 * @package     apache_maven 
     29 * @subpackage  artifact_versioning 
     30 */ 
     31/** 
     32 * Constructs a version range from a specification. 
     33 * 
     34 * @package     apache_maven 
     35 * @subpackage  artifact_versioning 
     36 */ 
     37class mvnVersionRange 
    338{ 
    439    /** 
    540     * the recommended version 
    641     * 
    7      * @var  MavenArtifactVersion 
    8      */ 
    9     protected $recommendedVersion
     42     * @var  string 
     43     */ 
     44    protected $recommendedVersion = null
    1045    /** 
    1146     * the list of restrictions 
    1247     *  
    13      * @var  array 
    14      */ 
    15     protected $restrictions
     48     * @var  array<mvnRestriction> 
     49     */ 
     50    protected $restrictions       = array()
    1651     
    1752    /** 
    1853     * constructor 
    1954     * 
    20      * @param  MavenArtifactVersion  $recommendedVersion  optional  the recommended version 
    21      * @param  array                 $restrictions        optional  the list of restrictions 
    22      */ 
    23     private function __construct(MavenArtifactVersion $recommendedVersion = null, array $restrictions = array()) 
     55     * @param  string                  $recommendedVersion  optional  the recommended version 
     56     * @param  array<mvnRestriction>   $restrictions        optional  the list of restrictions 
     57     */ 
     58    private function __construct($recommendedVersion = null, array $restrictions = array()) 
    2459    { 
    2560        $this->recommendedVersion = $recommendedVersion; 
     
    3065     * returns the recommended version 
    3166     * 
    32      * @return  MavenArtifactVersion 
     67     * @return  string 
    3368     */ 
    3469    public function getRecommendedVersion() 
     
    4075     * returns the list of restrictions 
    4176     * 
    42      * @return  array 
     77     * @return  array<mvnRestriction> 
    4378     */ 
    4479    public function getRestrictions() 
     
    4883     
    4984    /** 
    50      * Create a version range from a string representation 
     85     * creates a version range from a string representation 
    5186     *  
    5287     * Some spec examples are 
     
    5691     *   <li><code>[1.0,2.0]</code> Versions 1.0 to 2.0 (both included)</li> 
    5792     *   <li><code>[1.5,)</code> Versions 1.5 and higher</li> 
     93     *   <li><code>[1.5],[2.3]</code> Versions 1.5 and 2.3</li> 
    5894     *   <li><code>(,1.0],[1.2,)</code> Versions up to 1.0 (included) and 1.2 or higher</li> 
    5995     * </ul> 
    6096     *  
    61      * @param   string             $spec  representation of a version or version range 
    62      * @return  MavenVersionRange 
    63      * @throws  InvalidVersionSpecificationException 
     97     * @param   string           $spec  representation of a version or version range 
     98     * @return  mvnVersionRange 
     99     * @throws  mvnInvalidVersionSpecificationException 
    64100     */ 
    65101    public static function createFromVersionSpec($spec) 
     
    77113        while (substr($process, 0, 1) == '[' || substr($process, 0, 1) == '(') { 
    78114            $index1 = strpos($process, ')'); 
     115            if (false === $index1) { 
     116                $index1 = -1; 
     117            } 
     118             
    79119            $index2 = strpos($process, ']'); 
     120            if (false === $index2) { 
     121                $index2 = -1; 
     122            } 
    80123 
    81124            $index = $index2; 
     
    87130 
    88131            if (0 > $index) { 
    89                 throw new InvalidVersionSpecificationException('Unbounded range: ' . $spec); 
     132                throw new mvnInvalidVersionSpecificationException('Unbounded range: ' . $spec); 
    90133            } 
    91134 
     
    97140            if (null != $upperBound) { 
    98141                $lowerBound = $restriction->getLowerBound(); 
    99                 if (null == $lowerBound || $lowerBound->compareTo($upperBound) < 0) { 
    100                     throw new InvalidVersionSpecificationException('Ranges overlap: ' . $spec); 
     142                if (null == $lowerBound || version_compare($lowerBound, $upperBound) < 0) { 
     143                    throw new mvnInvalidVersionSpecificationException('Ranges overlap: ' . $spec); 
    101144                } 
    102145            } 
    103146             
    104147            $restrictions[] = $restriction; 
    105             $upperBound = $restriction->getUpperBound(); 
     148            $upperBound     = $restriction->getUpperBound(); 
    106149 
    107150            $process = trim(substr($process, ($index + 1))); 
     
    114157        if (strlen($process) > 0) { 
    115158            if (count($restrictions) > 0) { 
    116                 throw new InvalidVersionSpecificationException('Only fully-qualified sets allowed in multiple set scenario: ' . $spec); 
     159                throw new mvnInvalidVersionSpecificationException('Only fully-qualified sets allowed in multiple set scenario: ' . $spec); 
    117160            } else { 
    118                 $version = new DefaultArtifactVersion($process); 
    119                 $restrictions[] = Restriction.EVERYTHING; 
     161                $version        = explode(',', $process); 
     162                $version        = str_replace(')', '', str_replace(']', '', str_replace('(', '', str_replace('[', '', $version[0])))); 
     163                $restrictions[] = mvnRestriction::everything(); 
    120164            } 
    121165        } 
     
    127171     * creates a version range from one version 
    128172     * 
    129      * @param   string             $version 
    130      * @return  MavenVersionRange 
     173     * @param   string           $version 
     174     * @return  mvnVersionRange 
    131175     */ 
    132176    public static function createFromVersion($version) 
    133177    { 
    134         return new self(new DefaultArtifactVersion($version)); 
     178        return new self($version); 
    135179    } 
    136180     
     
    139183     * 
    140184     * @param   string       $spec 
    141      * @return  Restriction 
    142      * @throws  throws InvalidVersionSpecificationException 
     185     * @return  mvnRestriction 
     186     * @throws  mvnInvalidVersionSpecificationException 
    143187     */ 
    144188    private static function parseRestriction($spec) 
     
    146190        $lowerBoundInclusive = (substr($spec, 0, 1) == '['); 
    147191        $upperBoundInclusive = (substr($spec, -1, 1) == ']'); 
    148  
    149         $process = trim(substr($spec, 1, (strlen($spec) - 1))); 
    150  
    151         $index = strpos($process, ','); 
     192        $process             = trim(substr($spec, 1, (strlen($spec) - 2))); 
     193        $index               = strpos($process, ','); 
     194        if (false === $index) { 
     195            $index = -1; 
     196        } 
    152197 
    153198        if (0 > $index) { 
    154199            if (false == $lowerBoundInclusive || false == $upperBoundInclusive) { 
    155                 throw new InvalidVersionSpecificationException('Single version must be surrounded by []: ' . $spec); 
    156             } 
    157  
    158             $version     = new DefaultArtifactVersion($process); 
    159             $restriction = new Restriction($version, $lowerBoundInclusive, $version, $upperBoundInclusive); 
    160         } else { 
    161             $lowerBound = trim(substr($process, 0, $index)); 
    162             $upperBound = trim(substr($process, ($index + 1))); 
    163             if ($lowerBound == $upperBound) { 
    164                 throw new InvalidVersionSpecificationException('Range cannot have identical boundaries: ' . $spec); 
    165             } 
    166  
    167             $lowerVersion = null; 
    168             if (strlen($lowerBound) > 0) { 
    169                 $lowerVersion = new DefaultArtifactVersion($lowerBound); 
    170             } 
    171              
    172             $upperVersion = null; 
    173             if (strlen($upperBound) > 0) { 
    174                 $upperVersion = new DefaultArtifactVersion($upperBound); 
    175             } 
    176  
    177             if (null != $upperVersion && null != $lowerVersion && $upperVersion->compareTo($lowerVersion) < 0) { 
    178                 throw new InvalidVersionSpecificationException('Range defies version ordering: ' . $spec); 
    179             } 
    180  
    181             $restriction = new Restriction($lowerVersion, $lowerBoundInclusive, $upperVersion, $upperBoundInclusive); 
    182         } 
    183  
    184         return $restriction; 
     200                throw new mvnInvalidVersionSpecificationException('Single version must be surrounded by []: ' . $spec); 
     201            } 
     202 
     203            return new mvnRestriction($process, $lowerBoundInclusive, $process, $upperBoundInclusive); 
     204        } 
     205         
     206        $lowerBound = trim(substr($process, 0, $index)); 
     207        $upperBound = trim(substr($process, ($index + 1))); 
     208        if ($lowerBound == $upperBound) { 
     209            throw new mvnInvalidVersionSpecificationException('Range can not have identical boundaries: ' . $spec); 
     210        } 
     211 
     212        $lowerVersion = null; 
     213        if (strlen($lowerBound) > 0) { 
     214            $lowerVersion = $lowerBound; 
     215        } 
     216             
     217        $upperVersion = null; 
     218        if (strlen($upperBound) > 0) { 
     219            $upperVersion = $upperBound; 
     220        } 
     221 
     222        if (null != $upperVersion && null != $lowerVersion && version_compare($upperVersion, $lowerVersion) < 0) { 
     223            throw new mvnInvalidVersionSpecificationException('Range defies version ordering: ' . $spec); 
     224        } 
     225 
     226        return new mvnRestriction($lowerVersion, $lowerBoundInclusive, $upperVersion, $upperBoundInclusive); 
    185227    } 
    186228     
     
    188230     * restricts the version range to the given range 
    189231     *  
    190      * @param   MavenVersionRange  $restriction 
    191      * @return  MavenVersionRange 
     232     * @param   mvnVersionRange  $restrictTo 
     233     * @return  mvnVersionRange 
    192234     */ 
    193235    public function restrict(self $restrictTo) 
     
    201243 
    202244        if (count($restrictions) == 0 && null == $this->recommendedVersion) { 
    203             //should throw this immediately, but need artifact 
    204             throw new OverConstrainedVersionException('Restricting incompatible version ranges'); 
     245            throw new mvnOverConstrainedVersionException('Restricting incompatible version ranges'); 
    205246        } 
    206247         
    207248        $version = null; 
    208249        if (count($restrictions) > 0) { 
    209             $found = false; 
    210250            foreach ($restrictions as $restriction) { 
    211251                if (null != $this->recommendedVersion && $restriction->containsVersion($this->recommendedVersion) == true) { 
    212252                    // if we find the original, use that 
    213253                    $version = $this->recommendedVersion ; 
    214                     $found   = true
     254                    break
    215255                } elseif (null == $version && $restrictTo->getRecommendedVersion() != null && 
    216256                    $restriction->containsVersion($restrictTo->getRecommendedVersion()) == true) { 
     
    232272     * @param   string                $artifactId 
    233273     * @return  MavenArtifactVersion 
    234      * @throws  OverConstrainedVersionException 
     274     * @throws  mvnOverConstrainedVersionException 
    235275     */ 
    236276    public function getSelectedVersion($artifactId) 
     
    241281         
    242282        if (count($this->restrictions) == 0) { 
    243             throw new OverConstrainedVersionException('The artifact has no valid ranges', $artifactId); 
     283            throw new mvnOverConstrainedVersionException('The artifact ' . $artifactId . ' has no valid ranges'); 
    244284        } 
    245285         
     
    247287        $version    = $restriction->getUpperBound(); 
    248288        if (null == $version)  { 
    249             $version = RELEASE
     289            $version = 'RELEASE'
    250290        } 
    251291         
     
    258298     * @param   string  $artifactId 
    259299     * @return  bool 
    260      * @throws  OverConstrainedVersionException 
     300     * @throws  mvnOverConstrainedVersionException 
    261301     */ 
    262302    public function isSelectedVersionKnown($artifactId) 
     
    267307         
    268308        if (count($this->restrictions) == 0) { 
    269             throw new OverConstrainedVersionException('The artifact has no valid ranges', $artifactId); 
     309            throw new mvnOverConstrainedVersionException('The artifact ' . $artifactId . 'has no valid ranges'); 
    270310        } 
    271311         
     
    286326    { 
    287327        if (null != $this->recommendedVersion) { 
    288             return $this->recommendedVersion->__toString()
     328            return $this->recommendedVersion
    289329        }  
    290330         
     
    296336            $buf .= $restriction->isLowerBoundInclusive() ? '[' : '('; 
    297337            if ($restriction->getLowerBound() != null ) { 
    298                 $buf .= $restriction->getLowerBound()->__toString()
     338                $buf .= $restriction->getLowerBound()
    299339            } 
    300340             
    301341            $buf .= ','; 
    302342            if ($restriction->getUpperBound() != null ) { 
    303                 $buf .= $restriction->getUpperBound()->__toString()
     343                $buf .= $restriction->getUpperBound()
    304344            } 
    305345             
     
    313353     * returns the version that matches 
    314354     *  
    315      * @param   array 
    316      * @return  ArtifactVersion 
     355     * @param   array<string> 
     356     * @return  string 
    317357     */ 
    318358    public function matchVersion(array $versions) 
     
    322362            if ($this->containsVersion($version) == true) { 
    323363                // valid - check if it is greater than the currently matched version 
    324                 if (null == $matched || $version->compareTo($matched) > 0) { 
     364                if (null == $matched || version_compare($matched, $version) > 0) { 
    325365                    $matched = $version; 
    326366                } 
     
    334374     * check whether the range contains the given version 
    335375     *  
    336      * @param   ArtifactVersion  $version 
     376     * @param   string  $version 
    337377     * @return  bool 
    338378     */ 
    339     public function containsVersion(ArtifactVersion $version) 
     379    public function containsVersion($version) 
    340380    { 
    341381        foreach ($this->restrictions as $restriction) { 
     
    355395    public function hasRestrictions() 
    356396    { 
    357         return count($this->restrictions) > 0 && null == $this->recommendedVersion
     397        return (count($this->restrictions) > 0 && null === $this->recommendedVersion)
    358398    } 
    359399}