aboutsummaryrefslogtreecommitdiffstats
path: root/api/src/main/java/org/semver/Version.java
blob: 09046b8e8ebd872be172971bd8c8e7ffec3e2687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
 * Copyright 2012-2014 Julien Eluard and contributors
 * This project includes software developed by Julien Eluard: https://github.com/jeluard/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.semver;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import org.apache.commons.lang.StringUtils;

/**
 *
 * Version following semantic defined by <a href="http://semver.org/">Semantic Versioning</a> document.
 *
 */
@Immutable
public final class Version implements Comparable<Version> {

    /**
     * {@link Version} element. From most meaningful to less meaningful.
     */
    public enum Element {
        MAJOR, MINOR, PATCH, SPECIAL;
    }

    private static final String FORMAT = "(\\d+)\\.(\\d+)(?:\\.)?(\\d*)(\\.|-|\\+)?([0-9A-Za-z-.]*)?";
    private static final Pattern PATTERN = Pattern.compile(Version.FORMAT);

    private static final Pattern DIGITS_ONLY = Pattern.compile("\\d+");

    private static final String SNAPSHOT_VERSION_SUFFIX = "-SNAPSHOT";

    private final int major;
    private final int minor;
    private final int patch;
    private final String separator;
    private final Special special;

    public Version(@Nonnegative final int major, @Nonnegative final int minor, @Nonnegative final int patch) {
        this(major, minor, patch, null, null);
    }

    public Version(@Nonnegative final int major, @Nonnegative final int minor, @Nonnegative final int patch, @Nullable final String separator, @Nullable final String special) {
        if (major < 0) {
            throw new IllegalArgumentException(Element.MAJOR+" must be positive");
        }
        if (minor < 0) {
            throw new IllegalArgumentException(Element.MINOR+" must be positive");
        }
        if (patch < 0) {
            throw new IllegalArgumentException(Element.PATCH+" must be positive");
        }

        this.major = major;
        this.minor = minor;
        this.patch = patch;
        this.separator = separator;
        this.special = parseSpecial(special);
    }

    private Special parseSpecial(String specialString) {
      if (specialString == null) {
        return null;
      }
      Special special = new Special(specialString);
      if (special.ids.length == 0) {
        return null;
      }
      return special;
    }

    /**
     *
     * Creates a Version from a string representation. Must match Version#FORMAT.
     *
     * @param version
     * @return
     */
    public static Version parse(@Nonnull final String version) {
        final Matcher matcher = Version.PATTERN.matcher(version);
        if (!matcher.matches()) {
            throw new IllegalArgumentException("<"+version+"> does not match format "+Version.FORMAT);
        }

        final int major = Integer.valueOf(matcher.group(1));
        final int minor = Integer.valueOf(matcher.group(2));
        final int patch;
        final String patchMatch = matcher.group(3);
        if (StringUtils.isNotEmpty(patchMatch)) {
           patch = Integer.valueOf(patchMatch);
        } else {
            patch = 0;
        }
        final String separator = matcher.group(4);
        final String special = matcher.group(5);
        return new Version(major, minor, patch, separator, "".equals(special) ? null : special);
    }

    /**
     * @param type
     * @return next {@link Version} regarding specified {@link Version.Element}
     */
    public Version next(@Nonnull final Version.Element element) {
        if (element == null) {
            throw new IllegalArgumentException("null element");
        }

        switch (element) {
            case MAJOR:
              if (special == null || this.minor != 0 || this.patch != 0) {
                return new Version(this.major + 1, 0, 0);
              } else {
                return new Version(this.major, 0, 0);
              }
            case MINOR:
              if (special == null || this.patch != 0) {
                return new Version(this.major, this.minor + 1, 0);
              } else {
                return new Version(this.major, this.minor, 0);
              }
            case PATCH:
              if (special == null) {
                return new Version(this.major, this.minor, this.patch + 1);
              } else {
                return new Version(this.major, this.minor, this.patch);
              }
            default:
                throw new IllegalArgumentException("Unknown element <"+element+">");
        }
    }

    public boolean isInDevelopment() {
        return this.major == 0;
    }

    public boolean isStable() {
        return !isInDevelopment();
    }

    public boolean isSnapshot() {
        return this.special != null && this.special.isSnapshot();
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 43 * hash + this.major;
        hash = 43 * hash + this.minor;
        hash = 43 * hash + this.patch;
        hash = 43 * hash + (this.special != null ? this.special.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(@Nullable final Object object) {
        if (!(object instanceof Version)) {
            return false;
        }

        final Version other = (Version) object;
        if (other.major != this.major || other.minor != this.minor || other.patch != this.patch) {
            return false;
        }
        return (this.special == null) ? other.special == null : this.special.equals(other.special);
    }


    private static SpecialId parseSpecialId(String id) {
      Matcher matcher = DIGITS_ONLY.matcher(id);
      if (matcher.matches()) {
        return new IntId(Integer.parseInt(id));
      } else {
        return new StringId(id);
      }
    }

    abstract private static class SpecialId implements Comparable<SpecialId> {

      abstract public boolean isSnapshot();

      abstract public int compareTo(IntId other);
      abstract public int compareTo(StringId other);
    }

    private static class StringId extends SpecialId {
      private final String id;
      private StringId(String id) {
        this.id = id;
      }
      @Override
      public boolean isSnapshot() {
        return id.endsWith(SNAPSHOT_VERSION_SUFFIX);
      }

      @Override
      public int compareTo(SpecialId other) {
        return - other.compareTo(this);
      }

      @Override
      public String toString() {
        return id;
      }
      @Override
      public int compareTo(IntId other) {
        // Numeric identifiers always have lower precedence than non-numeric identifiers.
        return 1;
      }
      @Override
      public int compareTo(StringId other) {
        return id.compareTo(other.id);
      }
    }

    private static class IntId extends SpecialId {
      private final int id;
      public IntId(int id) {
        this.id = id;
      }
      @Override
      public boolean isSnapshot() {
        return false;
      }

      @Override
      public String toString() {
        return String.valueOf(id);
      }
      @Override
      public int compareTo(SpecialId other) {
        return - other.compareTo(this);
      }

      @Override
      public int compareTo(IntId other) {
        return id - other.id;
      }
      @Override
      public int compareTo(StringId other) {
        //Numeric identifiers always have lower precedence than non-numeric identifiers.
        return -1;
      }
    }

    private static class Special implements Comparable<Special> {
      private final SpecialId[] ids;
      Special(String s) {
        String[] split = s.split("\\.");
        ids = new SpecialId[split.length];
        for (int i = 0; i < split.length; i++) {
          ids[i] = parseSpecialId(split[i]);
        }
      }

      public SpecialId last() {
        return ids[ids.length - 1];
      }

      public boolean isSnapshot() {
        return last().isSnapshot();
      }

      @Override
      public int compareTo(Special other) {
        int min = Math.min(other.ids.length, ids.length);
        for (int i = 0; i < min; i++) {
          int c = ids[i].compareTo(other.ids[i]);
          if (c != 0) {
            return c;
          }
        }
        int max = Math.max(other.ids.length, ids.length);
        if (max != min) {
          if (ids.length > other.ids.length) {
            return 1;
          } else {
            return -1;
          }
        }
        return 0;
      }

      @Override
      public String toString() {
        final StringBuilder builder = new StringBuilder();
        for (int i = 0; i < ids.length; i++) {
          SpecialId s = ids[i];
          if (i != 0) {
            builder.append(".");
          }
          builder.append(s);
        }
        return builder.toString();
      }
    }

    @Override
    public int compareTo(final Version other) {
        if (equals(other)) {
            return 0;
        }

        if (this.major < other.major) {
            return -1;
        } else if (this.major == other.major) {
            if (this.minor < other.minor) {
                return -1;
            } else if (this.minor == other.minor) {
                if (this.patch < other.patch) {
                    return -1;
                } else if (this.patch == other.patch) {
                    if (this.special != null && other.special != null) {
                        return this.special.compareTo(other.special);
                    } else if (other.special != null) {
                        return 1;
                    } else if (this.special != null) {
                       return -1;
                    } // else handled by previous equals check
                }
            }
        }
        return 1; //if this (major, minor or patch) is > than other
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append(this.major).append(".").append(this.minor).append(".").append(this.patch);
        if (this.separator != null) {
            builder.append(this.separator);
        }
        if (this.special != null) {
            builder.append(this.special);
        }
        return builder.toString();
    }

}