Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / DefaultVersion.java @ 35979

History | View | Annotate | Download (4.45 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, int build) {
21
                this();
22
                this.mayor = mayor;
23
                this.minor = minor;
24
                this.rev = rev;
25
                this.classifier = classifier;
26
                this.build = build;
27
        }
28
        
29
        public Version parse(String version) {
30
                // Formato: mayor.minor.rev-classifier-build
31
                
32
                String[] x = version.split("[.]");
33
                int lx = x.length;
34
                if( lx==1 ) {
35
                        this.mayor = parseIntClassifierAndBuild(x[0],version);
36
                        this.minor = 0;
37
                        this.rev = 0;
38
                        this.classifier = null;
39
                        this.build = 0;
40
                } else if( lx==2 ) {
41
                        this.mayor = Integer.parseInt(x[0]);
42
                        this.minor = parseIntClassifierAndBuild(x[1],version);
43
                        this.rev = 0;
44
                        this.classifier = null;
45
                        this.build = 0;
46
                } else if( lx==3 ) {
47
                        this.mayor = Integer.parseInt(x[0]);
48
                        this.minor = Integer.parseInt(x[1]);
49
                        this.rev = parseIntClassifierAndBuild(x[2],version);
50
                } else {
51
                        throw new InvalidParameterException(version);
52
                }
53
                return this;
54
        }
55

    
56
        private int parseIntClassifierAndBuild(String s, String fullversion) {
57
                int value;
58
                
59
                String[] y = s.split("[-]");
60
                int ly = y.length;
61
                if( ly == 1 ) {
62
                        value = Integer.parseInt(y[0]);
63
                        this.classifier = null;
64
                        this.build = 0;
65
                } else if( ly == 2 ) {
66
                        value = Integer.parseInt(y[0]);
67
                        try {
68
                                this.build = Integer.parseInt(y[1]);
69
                                this.classifier = null;
70
                        } catch( NumberFormatException e) {
71
                                this.build = 0;
72
                                this.classifier = y[1];
73
                        }
74
                } else if( ly == 3 ) {
75
                        value = Integer.parseInt(y[0]);
76
                        this.classifier = y[1];
77
                        this.build = Integer.parseInt(y[2]);
78
                } else {
79
                        throw new InvalidParameterException(fullversion);
80
                }
81
                return value;
82
        }
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
        public String toString() {
125
                if( this.classifier==null ) {
126
                        return MessageFormat.format("{0}.{1}.{2}-{3,number,####}",
127
                                        this.mayor,
128
                                        this.minor,
129
                                        this.rev,
130
                                        this.build
131
                                );
132
                } else {
133
                        return MessageFormat.format("{0}.{1}.{2}-{3}-{4,number,####}",
134
                                        this.mayor,
135
                                        this.minor,
136
                                        this.rev,
137
                                        this.classifier,
138
                                        this.build
139
                                );
140
                }
141
        }
142
        
143
        public String fullFormat() {
144
                if( this.classifier==null ) {
145
                        // classifier AAAA allows compare correctly tow versions
146
                        return MessageFormat.format("{0,number,0000}.{1,number,0000}.{2,number,0000}-AAAA-{3,number,0000}",
147
                                        this.mayor,
148
                                        this.minor,
149
                                        this.rev,
150
                                        this.build
151
                                );
152
                } else {
153
                        return MessageFormat.format("{0,number,0000}.{1,number,0000}.{2,number,0000}-{3}-{4,number,0000}",
154
                                        this.mayor,
155
                                        this.minor,
156
                                        this.rev,
157
                                        this.classifier,
158
                                        this.build
159
                                );
160
                }
161
        }
162
        
163
        public Object clone() throws CloneNotSupportedException {
164
                return super.clone();
165
        }
166
        
167
        public boolean equals(Object obj) {
168
                Version other = (Version)obj;
169
                if( this.mayor != other.getMayor() ) {
170
                        return false;
171
                }
172
                if( this.minor != other.getMinor() ) {
173
                        return false;
174
                }
175
                if( this.rev != other.getRevision() ) {
176
                        return false;
177
                }
178
                if( this.build != other.getBuild() ) {
179
                        return false;
180
                }
181
                if( this.classifier==null ) {
182
                        if( other.getClassifier()==null ) {
183
                                return true;
184
                        } else {
185
                                return false;
186
                        }
187
                }
188
                if( !this.classifier.equalsIgnoreCase(other.getClassifier()) ) {
189
                        return false;
190
                }
191
                return true;
192
        }
193

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