/
Version.java
176 lines (163 loc) · 4.9 KB
/
Version.java
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
/*******************************************************************************
* JetUML - A desktop application for fast UML diagramming.
*
* Copyright (C) 2020, 2021 by McGill University.
*
* See: https://github.com/prmr/JetUML
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses.
*******************************************************************************/
package ca.mcgill.cs.jetuml.application;
import java.util.Comparator;
import java.util.StringJoiner;
import ca.mcgill.cs.jetuml.annotations.Immutable;
/**
* Represents a version of JetUML.
*/
@Immutable
public final class Version implements Comparable<Version>
{
private static final Comparator<Version> COMPARATOR =
Comparator.<Version, Integer>comparing( version -> version.aMajor).
<Integer>thenComparing(version -> version.aMinor).
<Integer>thenComparing(version -> version.aPatch);
private final int aMajor;
private final int aMinor;
private final int aPatch;
private Version(int pMajor, int pMinor, int pPatch)
{
aMajor = pMajor;
aMinor = pMinor;
aPatch = pPatch;
}
@Override
public String toString()
{
StringJoiner versionString = new StringJoiner(".");
versionString.add(Integer.toString(aMajor));
versionString.add(Integer.toString(aMinor));
if( aPatch > 0 )
{
versionString.add(Integer.toString(aPatch));
}
return versionString.toString();
}
/**
* Parses a string representing a version.
*
* @param pVersionString The string to parse.
* @return A Version object
* @throws IllegalArgumentException if the string cannot be parsed into a valid version
* @pre pVersionString != null
*/
public static Version parse(String pVersionString)
{
String[] tokens = pVersionString.split("\\.");
if( tokens.length < 2)
{
throw new IllegalArgumentException("Invalid version string: " + pVersionString);
}
int major = 0;
int minor = 0;
int patch = 0;
try
{
major = Integer.parseInt(tokens[0]);
minor = Integer.parseInt(tokens[1]);
if( tokens.length == 3 )
{
patch = Integer.parseInt(tokens[2]);
}
else if( tokens.length > 3 )
{
throw new IllegalArgumentException("Invalid version string: " + pVersionString);
}
}
catch( NumberFormatException e )
{
throw new IllegalArgumentException("Invalid version string: " + pVersionString);
}
return new Version(major, minor, patch);
}
/**
* @param pMajor The major component.
* @param pMinor The minor component.
* @param pPatch The patch component.
* @return The object that corresponds to the specified version.
* @pre pMajor >= 0 && pMinor >= 0 && pPatch >=0
*/
public static Version create(int pMajor, int pMinor, int pPatch)
{
assert pMajor >= 0 && pMinor >= 0 && pPatch >=0;
return new Version(pMajor, pMinor, pPatch);
}
/**
* A convenience method to create a version without a patch number.
*
* @param pMajor The major component.
* @param pMinor The minor component.
* @return The object that corresponds to the specified version.
* @pre pMajor >= 0 && pMinor >= 0
*/
public static Version create(int pMajor, int pMinor)
{
return create(pMajor, pMinor, 0);
}
@Override
public int compareTo(Version pVersion)
{
return COMPARATOR.compare(this, pVersion);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + aMajor;
result = prime * result + aMinor;
result = prime * result + aPatch;
return result;
}
@Override
public boolean equals(Object pObject)
{
if( this == pObject )
{
return true;
}
if( pObject == null )
{
return false;
}
if( getClass() != pObject.getClass() )
{
return false;
}
Version other = (Version) pObject;
return aMajor == other.aMajor && aMinor == other.aMinor && aPatch == other.aPatch;
}
/**
* Determines if this version of the diagram is fully compatible
* with pLaterVersion of the application.
*
* @param pLaterVersion The version to load a diagram in.
* @return true if this version is fully supported by pLaterVersion.
* @pre pLaterVersion != null
*/
public boolean compatibleWith(Version pLaterVersion)
{
assert pLaterVersion != null;
return !(aMajor < 3 && pLaterVersion.aMajor >= 3);
}
}