View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.theplugin.commons.crucible.api.model;
18  
19  import java.io.Serializable;
20  
21  /**
22   * @author Marek Went
23   * @author Pawel Niewiadomski
24   */
25  @SuppressWarnings("serial")
26  public class CrucibleVersionInfo implements Serializable, Comparable<CrucibleVersionInfo> {
27  	private final String buildDate;
28  
29  	private final String releaseNumber;
30  
31  	private Integer major;
32  	private Integer minor;
33  	private Integer maintanance;
34  	private String build;
35  
36  	public CrucibleVersionInfo(String releaseNumber, String buildDate) {
37  		this.buildDate = buildDate;
38  		this.releaseNumber = releaseNumber;
39  		tokenizeVersionAndSetFields(releaseNumber);
40  	}
41  
42  	public String getBuildDate() {
43  		return buildDate;
44  	}
45  
46  	public String getReleaseNumber() {
47  		return releaseNumber;
48  	}
49  
50  	public boolean isVersion2OrGreater() {
51  		return (major != null && major >= 2);
52  	}
53  
54  	public boolean isVersion21OrGreater() {
55  		if (major == null || minor == null) {
56  			return false;
57  		}
58  
59  		if (major > 2 || (major == 2 && minor >= 1)) {
60  			return true;
61  		}
62  
63  		return false;
64  	}
65  
66  	public boolean isVersion24OrGrater() {
67  
68  		if (major == null || minor == null) {
69  			return false;
70  		}
71  
72  		if (major > 2 || (major == 2 && minor >= 4)) {
73  			return true;
74  		}
75  
76  		return false;
77  	}
78  	private void tokenizeVersionAndSetFields(String number) {
79  		String[] tokens = number.split("[.]");
80  
81  		major = null;
82  		minor = null;
83  		maintanance = null;
84  		build = null;
85  
86  		try {
87  			if (tokens.length > 0) {
88  				major = Integer.valueOf(tokens[0]);
89  				if (tokens.length > 1) {
90  					minor = Integer.valueOf(tokens[1]);
91  					if (tokens.length > 2) {
92  						maintanance = Integer.valueOf(tokens[2]);
93  						if (tokens.length > 3) {
94  							build = tokens[3];
95  						}
96  					}
97  				}
98  			}
99  		} catch (NumberFormatException e) {
100 			// stop parsing
101 		}
102 	}
103 
104 	public Integer getMajor() {
105 		return major;
106 	}
107 
108 	public Integer getMinor() {
109 		return minor;
110 	}
111 
112 	public Integer getMaintanance() {
113 		return maintanance;
114 	}
115 
116 	public String getBuild() {
117 		return build;
118 	}
119 
120 	private int compareInts(Integer a, Integer b) {
121 		if (a == null && b != null) {
122 			return -1;
123 		} else if (a != null && b == null) {
124 			return 1;
125 		} else if (a == null && b == null) {
126 			return 0;
127 		} else {
128 			return a.compareTo(b);
129 		}
130 	}
131 
132 	public int compareTo(CrucibleVersionInfo o) {
133 		int r = compareInts(major, o.major);
134 		if (r == 0) {
135 			r = compareInts(minor, o.minor);
136 			if (r == 0) {
137 				return compareInts(maintanance, o.maintanance);
138 			} else {
139 				return r;
140 			}
141 		} else {
142 			return r;
143 		}
144 	}
145 
146 	@Override
147 	public String toString() {
148 		StringBuilder sb = new StringBuilder();
149 		if (major != null) {
150 			sb.append(major);
151 			if (minor != null) {
152 				sb.append(".");
153 				sb.append(minor);
154 				if (maintanance != null) {
155 					sb.append(".");
156 					sb.append(maintanance);
157 					if (build != null) {
158 						sb.append(".");
159 						sb.append(build);
160 					}
161 				}
162 			}
163 		}
164 		return sb.toString();
165 	}
166 }