Statistics
| Revision:

root / tags / v2_0_0_Build_2051 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultVersion.java @ 38753

History | View | Annotate | Download (4.57 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
        public int getMinor() {
90
                return this.minor;
91
        }
92

    
93
        public int getRevision() {
94
                return this.rev;
95
        }
96

    
97
        public String getClassifier() {
98
                return this.classifier;
99
        }
100

    
101
        public int getBuild() {
102
                return this.build;
103
        }
104

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

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

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

    
152
        @Override
153
        public Object clone() throws CloneNotSupportedException {
154
                return super.clone();
155
        }
156

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

    
190
        public Version setBuild(int build) {
191
                this.build = build;
192
                return this;
193
        }
194
}