Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultVersion.java @ 43126

History | View | Annotate | Download (6.01 KB)

1 40560 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24 40435 jjdelcerro
package org.gvsig.installer.lib.impl;
25
26
import java.security.InvalidParameterException;
27
import java.text.MessageFormat;
28
29
import org.gvsig.installer.lib.api.Version;
30
31
public class DefaultVersion implements Version {
32
33
        private int mayor = 0;
34
        private int minor = 0;
35
        private int rev = 0;
36
        private String classifier = null;
37
        private int build = 0;
38
39
        public DefaultVersion() {
40
                super();
41
        }
42
43
        protected DefaultVersion(int mayor, int minor, int rev, String classifier,
44
                        int build) {
45
                this();
46
                this.mayor = mayor;
47
                this.minor = minor;
48
                this.rev = rev;
49
                this.classifier = classifier;
50
                this.build = build;
51
        }
52
53
        public Version parse(String version) {
54
                // Formato: mayor.minor.rev-classifier-build
55
56
                String[] x = version.split("[.]");
57
                int lx = x.length;
58
                if (lx == 1) {
59
                        this.mayor = parseIntClassifierAndBuild(x[0], version);
60
                        this.minor = 0;
61
                        this.rev = 0;
62
                        this.classifier = null;
63
                        this.build = 0;
64
                } else if (lx == 2) {
65
                        this.mayor = Integer.parseInt(x[0]);
66
                        this.minor = parseIntClassifierAndBuild(x[1], version);
67
                        this.rev = 0;
68
                        this.classifier = null;
69
                        this.build = 0;
70
                } else if (lx == 3) {
71
                        this.mayor = Integer.parseInt(x[0]);
72
                        this.minor = Integer.parseInt(x[1]);
73
                        this.rev = parseIntClassifierAndBuild(x[2], version);
74
                } else {
75
                        throw new InvalidParameterException(version);
76
                }
77
                return this;
78
        }
79
80
        private int parseIntClassifierAndBuild(String s, String fullversion) {
81
                int value;
82
83
                String[] y = s.split("[-]");
84
                int ly = y.length;
85
                if (ly == 1) {
86
                        value = Integer.parseInt(y[0]);
87
                        this.classifier = null;
88
                        this.build = 0;
89
                } else if (ly == 2) {
90
                        value = Integer.parseInt(y[0]);
91
                        try {
92
                                this.build = Integer.parseInt(y[1]);
93
                                this.classifier = null;
94
                        } catch (NumberFormatException e) {
95
                                this.build = 0;
96
                                this.classifier = y[1];
97
                        }
98
                } else if (ly == 3) {
99
                        value = Integer.parseInt(y[0]);
100
                        this.classifier = y[1];
101
                        this.build = Integer.parseInt(y[2]);
102
                } else {
103
                        throw new InvalidParameterException(fullversion);
104
                }
105
                return value;
106
        }
107
108
        public int getMayor() {
109
                return this.mayor;
110
        }
111
112
113
        public int getMajor() {
114
            return this.mayor;
115
        }
116
117
        public int getMinor() {
118
                return this.minor;
119
        }
120
121
        public int getRevision() {
122
                return this.rev;
123
        }
124
125
        public String getClassifier() {
126
                return this.classifier;
127
        }
128
129
        public int getBuild() {
130
                return this.build;
131
        }
132
133
        public boolean check(String op, Version other) {
134
                if ("=".equals(op) || "==".equals(op) || "-eq".equals(op)) {
135
                        return this.fullFormat().compareTo(other.fullFormat()) == 0;
136
                }
137
                if (">".equals(op) || "-gt".equals(op)) {
138
                        return this.fullFormat().compareTo(other.fullFormat()) > 0;
139
                }
140
                if (">=".equals(op) || "-ge".equals(op)) {
141
                        return this.fullFormat().compareTo(other.fullFormat()) >= 0;
142
                }
143
                if ("<".equals(op) || "-lt".equals(op)) {
144
                        return this.fullFormat().compareTo(other.fullFormat()) < 0;
145
                }
146
                if ("<=".equals(op) || "-le".equals(op)) {
147
                        return this.fullFormat().compareTo(other.fullFormat()) <= 0;
148
                }
149
                return false;
150
        }
151
152
        @Override
153
        public String toString() {
154
                if (this.classifier == null) {
155
                        return MessageFormat.format("{0}.{1}.{2}-{3,number,####}",
156
                                        this.mayor, this.minor, this.rev, this.build);
157
                } else {
158
                        return MessageFormat.format("{0}.{1}.{2}-{3}-{4,number,####}",
159
                                        this.mayor, this.minor, this.rev, this.classifier,
160
                                        this.build);
161
                }
162
        }
163
164
        public String fullFormat() {
165
                if (this.classifier == null) {
166
                        // classifier ZZZZ allows compare correctly tow versions
167
                        return MessageFormat
168
                                        .format(
169
                                                        "{0,number,0000}.{1,number,0000}.{2,number,0000}-ZZZZ-{3,number,0000}",
170
                                                        this.mayor, this.minor, this.rev, this.build);
171
                } else {
172
                        return MessageFormat
173
                                        .format(
174
                                                        "{0,number,0000}.{1,number,0000}.{2,number,0000}-{3}-{4,number,0000}",
175
                                                        this.mayor, this.minor, this.rev, this.classifier,
176
                                                        this.build);
177
                }
178
        }
179
180 40926 jjdelcerro
        public int compareTo(Object arg0) {
181
                Version other = (Version)arg0;
182
                return this.fullFormat().compareTo(other.fullFormat());
183
        }
184
185 40435 jjdelcerro
        @Override
186
        public Object clone() throws CloneNotSupportedException {
187
                return super.clone();
188
        }
189
190
        @Override
191
        public boolean equals(Object obj) {
192
                Version other = (Version) obj;
193
                if (this.mayor != other.getMayor()) {
194
                        return false;
195
                }
196
                if (this.minor != other.getMinor()) {
197
                        return false;
198
                }
199
                if (this.rev != other.getRevision()) {
200
                        return false;
201
                }
202
                if (this.build != other.getBuild()) {
203
                        return false;
204
                }
205
                if (this.classifier == null) {
206
                        if (other.getClassifier() == null) {
207
                                return true;
208
                        } else {
209
                                return false;
210
                        }
211
                }
212
                if (!this.classifier.equalsIgnoreCase(other.getClassifier())) {
213
                        return false;
214
                }
215
                return true;
216
        }
217
218
        public int hashCode() {
219
            return (classifier == null ? 0 : classifier.hashCode()) +
220
                (mayor<<13) + (minor<<19) + (rev<<25) + build;
221
        }
222
223
        public Version setBuild(int build) {
224
                this.build = build;
225
                return this;
226
        }
227 40893 jjdelcerro
228
        public String format(String fmt) {
229
                String s = fmt;
230
                s = s.replaceAll("%M", String.valueOf(this.mayor));
231
                s = s.replaceAll("%m", String.valueOf(this.minor));
232
                s = s.replaceAll("%r", String.valueOf(this.rev));
233
                s = s.replaceAll("%c", this.classifier);
234
                s = s.replaceAll("%b", String.valueOf(this.build));
235
                return s;
236
        }
237 40435 jjdelcerro
}