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      private static final String VERSION_SEPARATOR = "/";
35  
36      private Version fromVersion;
37  
38      private Version toVersion;
39  
40      public VersionRange(String versionRange) {
41          if (versionRange.endsWith("+")) {
42              fromVersion = new Version(versionRange.substring(0, versionRange.length() - 1));
43  
44              toVersion = Version.INFINITE;
45          } else if (versionRange.indexOf(VERSION_SEPARATOR) > 0 && !versionRange.endsWith(VERSION_SEPARATOR)) {
46              int pos = versionRange.indexOf(VERSION_SEPARATOR);
47  
48              fromVersion = new Version(versionRange.substring(0, pos));
49  
50              toVersion = new Version(versionRange.substring(pos + 1));
51          } else {
52              fromVersion = new Version(versionRange);
53  
54              toVersion = new Version(versionRange);
55          }
56      }
57  
58      public VersionRange(Version version) {
59          fromVersion = version;
60  
61          toVersion = version;
62      }
63  
64      public Version getFromVersion() {
65          return fromVersion;
66      }
67  
68      public Version getToVersion() {
69          return toVersion;
70      }
71  
72      public boolean isToInfinite() {
73          return toVersion == Version.INFINITE;
74      }
75  
76      // ----------------------------------------------------------------------
77      // Object overrides
78      // ----------------------------------------------------------------------
79  
80      public int hashCode() {
81          return fromVersion.hashCode() + toVersion.hashCode();
82      }
83  
84      public boolean equals(Object obj) {
85          if (!(obj instanceof VersionRange)) {
86              return false;
87          }
88  
89          VersionRange other = (VersionRange) obj;
90  
91          return fromVersion.equals(other.fromVersion) && toVersion.equals(other.toVersion);
92      }
93  
94      public String toString() {
95          if (fromVersion.equals(toVersion)) {
96              return fromVersion.toString("", ".");
97          } else if (toVersion.equals(Version.INFINITE)) {
98              return fromVersion.toString("", ".") + "+";
99          } else {
100             return "[" + fromVersion.toString("", ".") + " => " + toVersion.toString("", ".") + "]";
101         }
102     }
103 }