View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.common.util;
17  
18  import org.apache.commons.lang3.StringUtils;
19  
20  public class VersionUtils {
21  
22  	public static final String MAVEN_SNAPSHOT_TOKEN = "SNAPSHOT";
23  	private static final String[] DELIMITERS = new String[] { ".", "-" };
24  	private static final String SEPARATOR_CHARS = Str.toString(DELIMITERS);
25  
26  	/**
27  	 * Return true if <code>version</code> ends with <code>-SNAPSHOT</code> or <code>.SNAPSHOT</code> (case insensitive).
28  	 */
29  	public static final boolean isSnapshot(String version) {
30  		for (String delimiter : DELIMITERS) {
31  			String suffix = delimiter + MAVEN_SNAPSHOT_TOKEN;
32  			if (StringUtils.endsWithIgnoreCase(version, suffix)) {
33  				return true;
34  			}
35  		}
36  		return false;
37  	}
38  
39  	/**
40  	 * Return <code>version</code> with <code>.SNAPSHOT</code> or <code>-SNAPSHOT</code> removed from the end (if present)
41  	 *
42  	 * <pre>
43  	 * 1.0.0-SNAPSHOT returns 1.0.0
44  	 * 1.0.0.SNAPSHOT returns 1.0.0
45  	 * 1.0.0          returns 1.0.0
46  	 * 1.0.0SNAPSHOT  returns 1.0.0SNAPSHOT
47  	 * </pre>
48  	 */
49  	public static final String trimSnapshot(String version) {
50  		if (isSnapshot(version)) {
51  			int length = MAVEN_SNAPSHOT_TOKEN.length() + 1;
52  			return StringUtils.left(version, version.length() - length);
53  		} else {
54  			return version;
55  		}
56  	}
57  
58  	/**
59  	 * Parse a <code>Version</code> object from the <code>version</code> string. The logic here is crudely simple. First
60  	 * <code>SNAPSHOT</code> is trimmed off the end of the string (if it exists). Whatever remains is then split into tokens using
61  	 * <code>.</code> and <code>-</code> as delimiters. The first 3 tokens encountered are <code>major</code>, <code>minor</code>, and
62  	 * <code>incremental</code>, respectively. Anything after that is the <code>qualifier</code>
63  	 */
64  	public static final Version getVersion(String version) {
65  		boolean snapshot = isSnapshot(version);
66  		String trimmed = trimSnapshot(version);
67  		Version v = new Version();
68  		v.setTrimmed(trimmed);
69  		v.setSnapshot(snapshot);
70  		String[] tokens = StringUtils.split(trimmed, SEPARATOR_CHARS);
71  		if (tokens.length > 0) {
72  			v.setMajor(tokens[0]);
73  		}
74  		if (tokens.length > 1) {
75  			v.setMinor(tokens[1]);
76  		}
77  		if (tokens.length > 2) {
78  			v.setIncremental(tokens[2]);
79  		}
80  		String qualifier = getQualifier(trimmed, tokens);
81  		v.setQualifier(qualifier);
82  		return v;
83  	}
84  
85  	protected static final String getQualifier(String trimmed, String[] tokens) {
86  		if (tokens.length < 4) {
87  			return null;
88  		}
89  		int pos = tokens[0].length() + 1 + tokens[1].length() + 1 + tokens[2].length() + 1;
90  		return trimmed.substring(pos);
91  	}
92  
93  }