Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / packageutils / impl / DefaultVersion.java @ 931

History | View | Annotate | Download (6.42 KB)

1
/**
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 2
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
package org.gvsig.tools.packageutils.impl;
25

    
26
import java.security.InvalidParameterException;
27
import java.text.MessageFormat;
28

    
29
import org.gvsig.tools.packageutils.Version;
30

    
31
public class DefaultVersion implements Version {
32

    
33
        private int major = 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.major = mayor;
47
                this.minor = minor;
48
                this.rev = rev;
49
                this.classifier = classifier;
50
                this.build = build;
51
        }
52

    
53
        public org.gvsig.installer.lib.api.Version parse(String version) {
54
                try {
55
                        if( version == null || version.length()==0 ) {
56
                                this.major = 0;
57
                                this.minor = 0;
58
                                this.rev = 0;
59
                                this.classifier = null;
60
                                this.build = 0;
61
                                return this;
62
                        }
63
                        // Formato: mayor.minor.rev-classifier-build
64
        
65
                        String[] x = version.split("[.]");
66
                        int lx = x.length;
67
                        if (lx == 1) {
68
                                this.major = parseIntClassifierAndBuild(x[0], version);
69
                                this.minor = 0;
70
                                this.rev = 0;
71
                                this.classifier = null;
72
                                this.build = 0;
73
                        } else if (lx == 2) {
74
                                this.major = Integer.parseInt(x[0]);
75
                                this.minor = parseIntClassifierAndBuild(x[1], version);
76
                                this.rev = 0;
77
                                this.classifier = null;
78
                                this.build = 0;
79
                        } else if (lx == 3) {
80
                                this.major = Integer.parseInt(x[0]);
81
                                this.minor = Integer.parseInt(x[1]);
82
                                this.rev = parseIntClassifierAndBuild(x[2], version);
83
                        } else {
84
                                throw new InvalidParameterException(version);
85
                        }
86
                } catch(InvalidParameterException ex) {
87
                        throw ex;
88
                } catch(Exception ex) {
89
                        InvalidParameterException e = new InvalidParameterException(version);
90
                        e.initCause(ex);
91
                        throw e;
92
                }
93
                return this;
94
        }
95

    
96
        private int parseIntClassifierAndBuild(String s, String fullversion) {
97
                int value;
98

    
99
                String[] y = s.split("[-]");
100
                int ly = y.length;
101
                if (ly == 1) {
102
                        value = Integer.parseInt(y[0]);
103
                        this.classifier = null;
104
                        this.build = 0;
105
                } else if (ly == 2) {
106
                        value = Integer.parseInt(y[0]);
107
                        try {
108
                                this.build = Integer.parseInt(y[1]);
109
                                this.classifier = null;
110
                        } catch (NumberFormatException e) {
111
                                this.build = 0;
112
                                this.classifier = y[1];
113
                        }
114
                } else if (ly == 3) {
115
                        value = Integer.parseInt(y[0]);
116
                        this.classifier = y[1];
117
                        this.build = Integer.parseInt(y[2]);
118
                } else {
119
                        throw new InvalidParameterException(fullversion);
120
                }
121
                return value;
122
        }
123

    
124
        public int getMajor() {
125
                return this.major;
126
        }
127
        
128

    
129
    public int getMayor() {
130
        return getMajor();
131
    }
132

    
133
        public int getMinor() {
134
                return this.minor;
135
        }
136

    
137
        public int getRevision() {
138
                return this.rev;
139
        }
140

    
141
        public String getClassifier() {
142
                return this.classifier;
143
        }
144

    
145
        public int getBuild() {
146
                return this.build;
147
        }
148

    
149
        public boolean check(String op, org.gvsig.installer.lib.api.Version other) {
150
                if ("=".equals(op) || "==".equals(op) || "-eq".equals(op)) {
151
                        return this.fullFormat().compareTo(other.fullFormat()) == 0;
152
                }
153
                if (">".equals(op) || "-gt".equals(op)) {
154
                        return this.fullFormat().compareTo(other.fullFormat()) > 0;
155
                }
156
                if (">=".equals(op) || "-ge".equals(op)) {
157
                        return this.fullFormat().compareTo(other.fullFormat()) >= 0;
158
                }
159
                if ("<".equals(op) || "-lt".equals(op)) {
160
                        return this.fullFormat().compareTo(other.fullFormat()) < 0;
161
                }
162
                if ("<=".equals(op) || "-le".equals(op)) {
163
                        return this.fullFormat().compareTo(other.fullFormat()) <= 0;
164
                }
165
                return false;
166
        }
167

    
168
        public String toString() {
169
                if (this.classifier == null) {
170
                        return MessageFormat.format("{0}.{1}.{2}-{3,number,####}", 
171
                                new Object[] {
172
                                        new Integer(this.major), 
173
                                        new Integer(this.minor), 
174
                                        new Integer(this.rev),
175
                                        new Integer(this.build)
176
                                }
177
                        );
178
                } else {
179
                        return MessageFormat.format("{0}.{1}.{2}-{3}-{4,number,####}",
180
                                new Object[] {
181
                                        new Integer(this.major), 
182
                                        new Integer(this.minor), 
183
                                        new Integer(this.rev),
184
                                        this.classifier,
185
                                        new Integer(this.build)
186
                                }
187
                        );
188
                }
189
        }
190

    
191
        public String fullFormat() {
192
                if (this.classifier == null) {
193
                        // classifier ZZZZ allows compare correctly tow versions
194
                        return MessageFormat.format(
195
                                "{0,number,0000}.{1,number,0000}.{2,number,0000}-ZZZZ-{3,number,0000}",
196
                                new Object[] {
197
                                                new Integer(this.major), 
198
                                                new Integer(this.minor), 
199
                                                new Integer(this.rev),
200
                                                new Integer(this.build)
201
                                        }
202
                                );
203
                } else {
204
                        return MessageFormat.format(
205
                                "{0,number,0000}.{1,number,0000}.{2,number,0000}-{3}-{4,number,0000}",
206
                                new Object[] {
207
                                                new Integer(this.major), 
208
                                                new Integer(this.minor), 
209
                                                new Integer(this.rev),
210
                                                this.classifier,
211
                                                new Integer(this.build)
212
                                        }
213
                                );
214
                }
215
        }
216

    
217
        public Object clone() throws CloneNotSupportedException {
218
                return super.clone();
219
        }
220

    
221
        public boolean equals(Object obj) {
222
                if( obj==null ) {
223
                        return false;
224
                }
225
                Version other = (Version) obj;
226
                if (this.major != other.getMajor()) {
227
                        return false;
228
                }
229
                if (this.minor != other.getMinor()) {
230
                        return false;
231
                }
232
                if (this.rev != other.getRevision()) {
233
                        return false;
234
                }
235
                if (this.build != other.getBuild()) {
236
                        return false;
237
                }
238
                if (this.classifier == null) {
239
                        if (other.getClassifier() == null) {
240
                                return true;
241
                        } else {
242
                                return false;
243
                        }
244
                }
245
                if (!this.classifier.equalsIgnoreCase(other.getClassifier())) {
246
                        return false;
247
                }
248
                return true;
249
        }
250
        
251
        public int hashCode() {
252
            return (classifier == null ? 0 : classifier.hashCode()) +
253
                (major<<13) + (minor<<19) + (rev<<25) + build;
254
        }
255

    
256
        public org.gvsig.installer.lib.api.Version setBuild(int build) {
257
                this.build = build;
258
                return this;
259
        }
260
}