View Javadoc
1   /*
2    * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and contributors.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the GNU Lesser General Public License
6    * (LGPL) version 2.1 which accompanies this distribution, and is available at
7    * http://www.gnu.org/licenses/lgpl-2.1.html
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * Contributors:
15   *     bstefanescu, jcarsique
16   */
17  package org.nuxeo.build.ant;
18  
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  /**
23   * version classifier is not took into account when comparing versions
24   *
25   * @deprecated Since 1.14. Use org.nuxeo.connect.update.Version instead
26   * @since 1.15
27   *
28   */
29  @Deprecated
30  public class OldVersion implements Comparable<OldVersion> {
31  
32      protected static final Pattern PATTERN = Pattern.compile("([0-9]+[0-9\\.]*)");
33  
34      public final static OldVersion ZERO = new OldVersion(0);
35  
36      protected int major;
37  
38      protected int minor;
39  
40      protected int patch;
41  
42      protected String classifier;
43  
44      public OldVersion(String version) {
45          Matcher m = PATTERN.matcher(version);
46          if (m.find()) {
47              if (!m.hitEnd()) {
48                  classifier = version.substring(m.end());
49                  if (classifier.startsWith("-")) {
50                      classifier = classifier.substring(1);
51                  }
52              }
53              version = m.group(1);
54              if (version.endsWith(".")) {
55                  version = version.substring(0, version.length() - 1);
56              }
57          } else {
58              throw new IllegalArgumentException("Not a version: " + version);
59          }
60          int p = version.lastIndexOf('-');
61          if (p > 0) { // classifier found
62              classifier = version.substring(p + 1) + classifier;
63              version = version.substring(0, p);
64          } else {
65              p = version.lastIndexOf('_');
66              if (p > 0) { // classifier found
67                  classifier = version.substring(p + 1) + classifier;
68                  version = version.substring(0, p);
69              }
70          }
71          p = version.indexOf('.', 0);
72          if (p > -1) {
73              major = Integer.parseInt(version.substring(0, p));
74              int q = version.indexOf('.', p + 1);
75              if (q > -1) {
76                  minor = Integer.parseInt(version.substring(p + 1, q));
77                  int r = version.indexOf('.', q + 1);
78                  if (r > 0) {
79                      patch = Integer.parseInt(version.substring(q + 1, r));
80                  } else {
81                      try {
82                          patch = Integer.parseInt(version.substring(q + 1));
83                      } catch (Exception e) {
84                          e.printStackTrace();
85                      }
86                  }
87              } else {
88                  minor = Integer.parseInt(version.substring(p + 1));
89              }
90          } else {
91              major = Integer.parseInt(version);
92          }
93      }
94  
95      public OldVersion(int major) {
96          this(major, 0, 0);
97      }
98  
99      public OldVersion(int major, int minor) {
100         this(major, minor, 0);
101     }
102 
103     public OldVersion(int major, int minor, int patch) {
104         this(major, minor, patch, null);
105     }
106 
107     public OldVersion(int major, int minor, int patch, String classifier) {
108         this.major = major;
109         this.minor = minor;
110         this.patch = patch;
111         this.classifier = classifier;
112     }
113 
114     public int major() {
115         return major;
116     }
117 
118     public int minor() {
119         return minor;
120     }
121 
122     public int patch() {
123         return patch;
124     }
125 
126     public String classifier() {
127         return classifier;
128     }
129 
130     public boolean lessThan(OldVersion v) {
131         return compareTo(v) < 0;
132     }
133 
134     public boolean lessOrEqualsThan(OldVersion v) {
135         return compareTo(v) <= 0;
136     }
137 
138     public boolean equalsTo(OldVersion v) {
139         return compareTo(v) == 0;
140     }
141 
142     public boolean greaterThan(OldVersion v) {
143         return compareTo(v) > 0;
144     }
145 
146     public boolean greaterOrEqualThan(OldVersion v) {
147         return compareTo(v) >= 0;
148     }
149 
150     @Override
151     public int compareTo(OldVersion o) {
152         int d = major - o.major;
153         if (d != 0) {
154             return d;
155         }
156         d = minor - o.minor;
157         if (d != 0) {
158             return d;
159         }
160         d = patch - o.patch;
161         if (d != 0) {
162             return d;
163         }
164         return 0;
165     }
166 
167     @Override
168     public boolean equals(Object obj) {
169         if (obj == this) {
170             return true;
171         }
172         if (obj instanceof OldVersion) {
173             OldVersion v = (OldVersion) obj;
174             return v.major == major && v.minor == minor && v.patch == patch;
175         }
176         return false;
177     }
178 
179     @Override
180     public int hashCode() {
181         return (major << 16) | (minor << 8) | patch;
182     }
183 
184     @Override
185     public String toString() {
186         if (classifier == null) {
187             return major + "." + minor + "." + patch;
188         } else {
189             return major + "." + minor + "." + patch + "-" + classifier;
190         }
191     }
192 
193 }