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 @ 40560

History | View | Annotate | Download (5.56 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 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
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
        @Override
181
        public Object clone() throws CloneNotSupportedException {
182
                return super.clone();
183
        }
184

    
185
        @Override
186
        public boolean equals(Object obj) {
187
                Version other = (Version) obj;
188
                if (this.mayor != other.getMayor()) {
189
                        return false;
190
                }
191
                if (this.minor != other.getMinor()) {
192
                        return false;
193
                }
194
                if (this.rev != other.getRevision()) {
195
                        return false;
196
                }
197
                if (this.build != other.getBuild()) {
198
                        return false;
199
                }
200
                if (this.classifier == null) {
201
                        if (other.getClassifier() == null) {
202
                                return true;
203
                        } else {
204
                                return false;
205
                        }
206
                }
207
                if (!this.classifier.equalsIgnoreCase(other.getClassifier())) {
208
                        return false;
209
                }
210
                return true;
211
        }
212
        
213
        public int hashCode() {
214
            return (classifier == null ? 0 : classifier.hashCode()) +
215
                (mayor<<13) + (minor<<19) + (rev<<25) + build;
216
        }
217

    
218
        public Version setBuild(int build) {
219
                this.build = build;
220
                return this;
221
        }
222
}