Statistics
| Revision:

root / tags / v2_0_0_Build_2050 / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultVersion.java @ 38692

History | View | Annotate | Download (4.57 KB)

1 35979 jjdelcerro
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 37599 nfrancisco
        protected DefaultVersion(int mayor, int minor, int rev, String classifier,
21
                        int build) {
22 35979 jjdelcerro
                this();
23
                this.mayor = mayor;
24
                this.minor = minor;
25
                this.rev = rev;
26
                this.classifier = classifier;
27
                this.build = build;
28
        }
29 37599 nfrancisco
30 35979 jjdelcerro
        public Version parse(String version) {
31
                // Formato: mayor.minor.rev-classifier-build
32 37599 nfrancisco
33 35979 jjdelcerro
                String[] x = version.split("[.]");
34
                int lx = x.length;
35 37599 nfrancisco
                if (lx == 1) {
36
                        this.mayor = parseIntClassifierAndBuild(x[0], version);
37 35979 jjdelcerro
                        this.minor = 0;
38
                        this.rev = 0;
39
                        this.classifier = null;
40
                        this.build = 0;
41 37599 nfrancisco
                } else if (lx == 2) {
42 35979 jjdelcerro
                        this.mayor = Integer.parseInt(x[0]);
43 37599 nfrancisco
                        this.minor = parseIntClassifierAndBuild(x[1], version);
44 35979 jjdelcerro
                        this.rev = 0;
45
                        this.classifier = null;
46
                        this.build = 0;
47 37599 nfrancisco
                } else if (lx == 3) {
48 35979 jjdelcerro
                        this.mayor = Integer.parseInt(x[0]);
49
                        this.minor = Integer.parseInt(x[1]);
50 37599 nfrancisco
                        this.rev = parseIntClassifierAndBuild(x[2], version);
51 35979 jjdelcerro
                } else {
52
                        throw new InvalidParameterException(version);
53
                }
54
                return this;
55
        }
56
57
        private int parseIntClassifierAndBuild(String s, String fullversion) {
58
                int value;
59 37599 nfrancisco
60 35979 jjdelcerro
                String[] y = s.split("[-]");
61
                int ly = y.length;
62 37599 nfrancisco
                if (ly == 1) {
63 35979 jjdelcerro
                        value = Integer.parseInt(y[0]);
64
                        this.classifier = null;
65
                        this.build = 0;
66 37599 nfrancisco
                } else if (ly == 2) {
67 35979 jjdelcerro
                        value = Integer.parseInt(y[0]);
68
                        try {
69
                                this.build = Integer.parseInt(y[1]);
70
                                this.classifier = null;
71 37599 nfrancisco
                        } catch (NumberFormatException e) {
72 35979 jjdelcerro
                                this.build = 0;
73
                                this.classifier = y[1];
74
                        }
75 37599 nfrancisco
                } else if (ly == 3) {
76 35979 jjdelcerro
                        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 37599 nfrancisco
85 35979 jjdelcerro
        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 37599 nfrancisco
                if ("=".equals(op) || "==".equals(op) || "-eq".equals(op)) {
107
                        return this.fullFormat().compareTo(other.fullFormat()) == 0;
108 35979 jjdelcerro
                }
109 37599 nfrancisco
                if (">".equals(op) || "-gt".equals(op)) {
110
                        return this.fullFormat().compareTo(other.fullFormat()) > 0;
111 35979 jjdelcerro
                }
112 37599 nfrancisco
                if (">=".equals(op) || "-ge".equals(op)) {
113
                        return this.fullFormat().compareTo(other.fullFormat()) >= 0;
114 35979 jjdelcerro
                }
115 37599 nfrancisco
                if ("<".equals(op) || "-lt".equals(op)) {
116
                        return this.fullFormat().compareTo(other.fullFormat()) < 0;
117 35979 jjdelcerro
                }
118 37599 nfrancisco
                if ("<=".equals(op) || "-le".equals(op)) {
119
                        return this.fullFormat().compareTo(other.fullFormat()) <= 0;
120 35979 jjdelcerro
                }
121
                return false;
122
        }
123
124 37599 nfrancisco
        @Override
125 35979 jjdelcerro
        public String toString() {
126 37599 nfrancisco
                if (this.classifier == null) {
127 35979 jjdelcerro
                        return MessageFormat.format("{0}.{1}.{2}-{3,number,####}",
128 37599 nfrancisco
                                        this.mayor, this.minor, this.rev, this.build);
129 35979 jjdelcerro
                } else {
130
                        return MessageFormat.format("{0}.{1}.{2}-{3}-{4,number,####}",
131 37599 nfrancisco
                                        this.mayor, this.minor, this.rev, this.classifier,
132
                                        this.build);
133 35979 jjdelcerro
                }
134
        }
135 37599 nfrancisco
136 35979 jjdelcerro
        public String fullFormat() {
137 37599 nfrancisco
                if (this.classifier == null) {
138 35979 jjdelcerro
                        // classifier AAAA allows compare correctly tow versions
139 37599 nfrancisco
                        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 35979 jjdelcerro
                } else {
144 37599 nfrancisco
                        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 35979 jjdelcerro
                }
150
        }
151 37599 nfrancisco
152
        @Override
153 35979 jjdelcerro
        public Object clone() throws CloneNotSupportedException {
154
                return super.clone();
155
        }
156 37599 nfrancisco
157
        @Override
158 35979 jjdelcerro
        public boolean equals(Object obj) {
159 37599 nfrancisco
                Version other = (Version) obj;
160
                if (this.mayor != other.getMayor()) {
161 35979 jjdelcerro
                        return false;
162
                }
163 37599 nfrancisco
                if (this.minor != other.getMinor()) {
164 35979 jjdelcerro
                        return false;
165
                }
166 37599 nfrancisco
                if (this.rev != other.getRevision()) {
167 35979 jjdelcerro
                        return false;
168
                }
169 37599 nfrancisco
                if (this.build != other.getBuild()) {
170 35979 jjdelcerro
                        return false;
171
                }
172 37599 nfrancisco
                if (this.classifier == null) {
173
                        if (other.getClassifier() == null) {
174 35979 jjdelcerro
                                return true;
175
                        } else {
176
                                return false;
177
                        }
178
                }
179 37599 nfrancisco
                if (!this.classifier.equalsIgnoreCase(other.getClassifier())) {
180 35979 jjdelcerro
                        return false;
181
                }
182
                return true;
183
        }
184 38244 jldominguez
185
        public int hashCode() {
186
            return (classifier == null ? 0 : classifier.hashCode()) +
187
                (mayor<<13) + (minor<<19) + (rev<<25) + build;
188
        }
189 35979 jjdelcerro
190
        public Version setBuild(int build) {
191
                this.build = build;
192
                return this;
193
        }
194
}