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

History | View | Annotate | Download (5.22 KB)

1
package org.gvsig.tools.packageutils.impl;
2

    
3
import java.security.InvalidParameterException;
4
import java.text.MessageFormat;
5

    
6
import org.gvsig.tools.packageutils.Version;
7

    
8
public class DefaultVersion implements Version {
9

    
10
        private int major = 0;
11
        private int minor = 0;
12
        private int rev = 0;
13
        private String classifier = null;
14
        private int build = 0;
15

    
16
        public DefaultVersion() {
17
                super();
18
        }
19

    
20
        protected DefaultVersion(int mayor, int minor, int rev, String classifier,
21
                        int build) {
22
                this();
23
                this.major = mayor;
24
                this.minor = minor;
25
                this.rev = rev;
26
                this.classifier = classifier;
27
                this.build = build;
28
        }
29

    
30
        public org.gvsig.installer.lib.api.Version parse(String version) {
31
                if( version == null || version.length()==0 ) {
32
                        this.major = 0;
33
                        this.minor = 0;
34
                        this.rev = 0;
35
                        this.classifier = null;
36
                        this.build = 0;
37
                        return this;
38
                }
39
                // Formato: mayor.minor.rev-classifier-build
40

    
41
                String[] x = version.split("[.]");
42
                int lx = x.length;
43
                if (lx == 1) {
44
                        this.major = parseIntClassifierAndBuild(x[0], version);
45
                        this.minor = 0;
46
                        this.rev = 0;
47
                        this.classifier = null;
48
                        this.build = 0;
49
                } else if (lx == 2) {
50
                        this.major = Integer.parseInt(x[0]);
51
                        this.minor = parseIntClassifierAndBuild(x[1], version);
52
                        this.rev = 0;
53
                        this.classifier = null;
54
                        this.build = 0;
55
                } else if (lx == 3) {
56
                        this.major = Integer.parseInt(x[0]);
57
                        this.minor = Integer.parseInt(x[1]);
58
                        this.rev = parseIntClassifierAndBuild(x[2], version);
59
                } else {
60
                        throw new InvalidParameterException(version);
61
                }
62
                return this;
63
        }
64

    
65
        private int parseIntClassifierAndBuild(String s, String fullversion) {
66
                int value;
67

    
68
                String[] y = s.split("[-]");
69
                int ly = y.length;
70
                if (ly == 1) {
71
                        value = Integer.parseInt(y[0]);
72
                        this.classifier = null;
73
                        this.build = 0;
74
                } else if (ly == 2) {
75
                        value = Integer.parseInt(y[0]);
76
                        try {
77
                                this.build = Integer.parseInt(y[1]);
78
                                this.classifier = null;
79
                        } catch (NumberFormatException e) {
80
                                this.build = 0;
81
                                this.classifier = y[1];
82
                        }
83
                } else if (ly == 3) {
84
                        value = Integer.parseInt(y[0]);
85
                        this.classifier = y[1];
86
                        this.build = Integer.parseInt(y[2]);
87
                } else {
88
                        throw new InvalidParameterException(fullversion);
89
                }
90
                return value;
91
        }
92

    
93
        public int getMajor() {
94
                return this.major;
95
        }
96
        
97

    
98
    public int getMayor() {
99
        return getMajor();
100
    }
101

    
102
        public int getMinor() {
103
                return this.minor;
104
        }
105

    
106
        public int getRevision() {
107
                return this.rev;
108
        }
109

    
110
        public String getClassifier() {
111
                return this.classifier;
112
        }
113

    
114
        public int getBuild() {
115
                return this.build;
116
        }
117

    
118
        public boolean check(String op, org.gvsig.installer.lib.api.Version other) {
119
                if ("=".equals(op) || "==".equals(op) || "-eq".equals(op)) {
120
                        return this.fullFormat().compareTo(other.fullFormat()) == 0;
121
                }
122
                if (">".equals(op) || "-gt".equals(op)) {
123
                        return this.fullFormat().compareTo(other.fullFormat()) > 0;
124
                }
125
                if (">=".equals(op) || "-ge".equals(op)) {
126
                        return this.fullFormat().compareTo(other.fullFormat()) >= 0;
127
                }
128
                if ("<".equals(op) || "-lt".equals(op)) {
129
                        return this.fullFormat().compareTo(other.fullFormat()) < 0;
130
                }
131
                if ("<=".equals(op) || "-le".equals(op)) {
132
                        return this.fullFormat().compareTo(other.fullFormat()) <= 0;
133
                }
134
                return false;
135
        }
136

    
137
        public String toString() {
138
                if (this.classifier == null) {
139
                        return MessageFormat.format("{0}.{1}.{2}-{3,number,####}", 
140
                                new Object[] {
141
                                        new Integer(this.major), 
142
                                        new Integer(this.minor), 
143
                                        new Integer(this.rev),
144
                                        new Integer(this.build)
145
                                }
146
                        );
147
                } else {
148
                        return MessageFormat.format("{0}.{1}.{2}-{3}-{4,number,####}",
149
                                new Object[] {
150
                                        new Integer(this.major), 
151
                                        new Integer(this.minor), 
152
                                        new Integer(this.rev),
153
                                        this.classifier,
154
                                        new Integer(this.build)
155
                                }
156
                        );
157
                }
158
        }
159

    
160
        public String fullFormat() {
161
                if (this.classifier == null) {
162
                        // classifier AAAA allows compare correctly tow versions
163
                        return MessageFormat.format(
164
                                "{0,number,0000}.{1,number,0000}.{2,number,0000}-AAAA-{3,number,0000}",
165
                                new Object[] {
166
                                                new Integer(this.major), 
167
                                                new Integer(this.minor), 
168
                                                new Integer(this.rev),
169
                                                new Integer(this.build)
170
                                        }
171
                                );
172
                } else {
173
                        return MessageFormat.format(
174
                                "{0,number,0000}.{1,number,0000}.{2,number,0000}-{3}-{4,number,0000}",
175
                                new Object[] {
176
                                                new Integer(this.major), 
177
                                                new Integer(this.minor), 
178
                                                new Integer(this.rev),
179
                                                this.classifier,
180
                                                new Integer(this.build)
181
                                        }
182
                                );
183
                }
184
        }
185

    
186
        public Object clone() throws CloneNotSupportedException {
187
                return super.clone();
188
        }
189

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

    
222
        public org.gvsig.installer.lib.api.Version setBuild(int build) {
223
                this.build = build;
224
                return this;
225
        }
226
}