View Javadoc

1   package org.codehaus.modello.model;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  /**
26   * A version range. Can be of 3 forms:<ul>
27   * <li><code>x.y.z</code>: a range of only one precise version,</li>
28   * <li><code>x.y.z<b>+</b></code>: a range starting form a precise version (included), without upper limit,</li>
29   * <li><code>x.y.z<b>/</b>i.j.k</code>: a range from one version to another (both included).</li>
30   * </ul>
31   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
32   */
33  public class VersionRange
34  {
35      private static final String VERSION_SEPARATOR = "/";
36  
37      private Version fromVersion;
38  
39      private Version toVersion;
40  
41      public VersionRange( String versionRange )
42      {
43          if ( versionRange.endsWith( "+" ) )
44          {
45              fromVersion = new Version( versionRange.substring( 0, versionRange.length() - 1 ) );
46  
47              toVersion = Version.INFINITE;
48          }
49          else if ( versionRange.indexOf( VERSION_SEPARATOR ) > 0 && ! versionRange.endsWith( VERSION_SEPARATOR ) )
50          {
51              int pos = versionRange.indexOf( VERSION_SEPARATOR );
52  
53              fromVersion = new Version( versionRange.substring( 0, pos ) );
54  
55              toVersion = new Version( versionRange.substring( pos + 1 ) );
56          }
57          else
58          {
59              fromVersion = new Version( versionRange );
60  
61              toVersion = new Version( versionRange );
62          }
63      }
64  
65      public VersionRange( Version version )
66      {
67          fromVersion = version;
68  
69          toVersion = version;
70      }
71  
72      public Version getFromVersion()
73      {
74          return fromVersion;
75      }
76  
77      public Version getToVersion()
78      {
79          return toVersion;
80      }
81  
82      public boolean isToInfinite()
83      {
84          return toVersion == Version.INFINITE;
85      }
86  
87      // ----------------------------------------------------------------------
88      // Object overrides
89      // ----------------------------------------------------------------------
90  
91      public int hashCode()
92      {
93          return fromVersion.hashCode() + toVersion.hashCode();
94      }
95  
96      public boolean equals( Object obj )
97      {
98          if ( !( obj instanceof VersionRange ) )
99          {
100             return false;
101         }
102 
103         VersionRange other = (VersionRange) obj;
104 
105         return fromVersion.equals( other.fromVersion ) && toVersion.equals( other.toVersion );
106     }
107 
108     public String toString()
109     {
110         if ( fromVersion.equals( toVersion ) )
111         {
112             return fromVersion.toString( "", "." );
113         }
114         else if ( toVersion.equals( Version.INFINITE ) )
115         {
116             return fromVersion.toString( "", "." ) + "+";
117         }
118         else
119         {
120             return "[" + fromVersion.toString( "", "." ) + " => " + toVersion.toString( "", "." ) + "]";
121         }
122     }
123 }