Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2052 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultVersion.java @ 38854

History | View | Annotate | Download (4.62 KB)

1
package org.gvsig.installer.lib.impl;
2

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

    
6
import org.gvsig.installer.lib.api.Version;
7

    
8
public class DefaultVersion implements Version {
9

    
10
        private int mayor = 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.mayor = mayor;
24
                this.minor = minor;
25
                this.rev = rev;
26
                this.classifier = classifier;
27
                this.build = build;
28
        }
29

    
30
        public Version parse(String version) {
31
                // Formato: mayor.minor.rev-classifier-build
32

    
33
                String[] x = version.split("[.]");
34
                int lx = x.length;
35
                if (lx == 1) {
36
                        this.mayor = parseIntClassifierAndBuild(x[0], version);
37
                        this.minor = 0;
38
                        this.rev = 0;
39
                        this.classifier = null;
40
                        this.build = 0;
41
                } else if (lx == 2) {
42
                        this.mayor = Integer.parseInt(x[0]);
43
                        this.minor = parseIntClassifierAndBuild(x[1], version);
44
                        this.rev = 0;
45
                        this.classifier = null;
46
                        this.build = 0;
47
                } else if (lx == 3) {
48
                        this.mayor = Integer.parseInt(x[0]);
49
                        this.minor = Integer.parseInt(x[1]);
50
                        this.rev = parseIntClassifierAndBuild(x[2], version);
51
                } else {
52
                        throw new InvalidParameterException(version);
53
                }
54
                return this;
55
        }
56

    
57
        private int parseIntClassifierAndBuild(String s, String fullversion) {
58
                int value;
59

    
60
                String[] y = s.split("[-]");
61
                int ly = y.length;
62
                if (ly == 1) {
63
                        value = Integer.parseInt(y[0]);
64
                        this.classifier = null;
65
                        this.build = 0;
66
                } else if (ly == 2) {
67
                        value = Integer.parseInt(y[0]);
68
                        try {
69
                                this.build = Integer.parseInt(y[1]);
70
                                this.classifier = null;
71
                        } catch (NumberFormatException e) {
72
                                this.build = 0;
73
                                this.classifier = y[1];
74
                        }
75
                } else if (ly == 3) {
76
                        value = Integer.parseInt(y[0]);
77
                        this.classifier = y[1];
78
                        this.build = Integer.parseInt(y[2]);
79
                } else {
80
                        throw new InvalidParameterException(fullversion);
81
                }
82
                return value;
83
        }
84

    
85
        public int getMayor() {
86
                return this.mayor;
87
        }
88

    
89

    
90
        public int getMajor() {
91
            return this.mayor;
92
        }
93

    
94
        public int getMinor() {
95
                return this.minor;
96
        }
97

    
98
        public int getRevision() {
99
                return this.rev;
100
        }
101

    
102
        public String getClassifier() {
103
                return this.classifier;
104
        }
105

    
106
        public int getBuild() {
107
                return this.build;
108
        }
109

    
110
        public boolean check(String op, Version other) {
111
                if ("=".equals(op) || "==".equals(op) || "-eq".equals(op)) {
112
                        return this.fullFormat().compareTo(other.fullFormat()) == 0;
113
                }
114
                if (">".equals(op) || "-gt".equals(op)) {
115
                        return this.fullFormat().compareTo(other.fullFormat()) > 0;
116
                }
117
                if (">=".equals(op) || "-ge".equals(op)) {
118
                        return this.fullFormat().compareTo(other.fullFormat()) >= 0;
119
                }
120
                if ("<".equals(op) || "-lt".equals(op)) {
121
                        return this.fullFormat().compareTo(other.fullFormat()) < 0;
122
                }
123
                if ("<=".equals(op) || "-le".equals(op)) {
124
                        return this.fullFormat().compareTo(other.fullFormat()) <= 0;
125
                }
126
                return false;
127
        }
128

    
129
        @Override
130
        public String toString() {
131
                if (this.classifier == null) {
132
                        return MessageFormat.format("{0}.{1}.{2}-{3,number,####}",
133
                                        this.mayor, this.minor, this.rev, this.build);
134
                } else {
135
                        return MessageFormat.format("{0}.{1}.{2}-{3}-{4,number,####}",
136
                                        this.mayor, this.minor, this.rev, this.classifier,
137
                                        this.build);
138
                }
139
        }
140

    
141
        public String fullFormat() {
142
                if (this.classifier == null) {
143
                        // classifier AAAA allows compare correctly tow versions
144
                        return MessageFormat
145
                                        .format(
146
                                                        "{0,number,0000}.{1,number,0000}.{2,number,0000}-AAAA-{3,number,0000}",
147
                                                        this.mayor, this.minor, this.rev, this.build);
148
                } else {
149
                        return MessageFormat
150
                                        .format(
151
                                                        "{0,number,0000}.{1,number,0000}.{2,number,0000}-{3}-{4,number,0000}",
152
                                                        this.mayor, this.minor, this.rev, this.classifier,
153
                                                        this.build);
154
                }
155
        }
156

    
157
        @Override
158
        public Object clone() throws CloneNotSupportedException {
159
                return super.clone();
160
        }
161

    
162
        @Override
163
        public boolean equals(Object obj) {
164
                Version other = (Version) obj;
165
                if (this.mayor != other.getMayor()) {
166
                        return false;
167
                }
168
                if (this.minor != other.getMinor()) {
169
                        return false;
170
                }
171
                if (this.rev != other.getRevision()) {
172
                        return false;
173
                }
174
                if (this.build != other.getBuild()) {
175
                        return false;
176
                }
177
                if (this.classifier == null) {
178
                        if (other.getClassifier() == null) {
179
                                return true;
180
                        } else {
181
                                return false;
182
                        }
183
                }
184
                if (!this.classifier.equalsIgnoreCase(other.getClassifier())) {
185
                        return false;
186
                }
187
                return true;
188
        }
189
        
190
        public int hashCode() {
191
            return (classifier == null ? 0 : classifier.hashCode()) +
192
                (mayor<<13) + (minor<<19) + (rev<<25) + build;
193
        }
194

    
195
        public Version setBuild(int build) {
196
                this.build = build;
197
                return this;
198
        }
199
}