001 /**
002 * Copyright 2010-2012 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.common.util;
017
018 import org.apache.commons.lang3.StringUtils;
019
020 public class VersionUtils {
021
022 public static final String MAVEN_SNAPSHOT_TOKEN = "SNAPSHOT";
023 private static final String[] DELIMITERS = new String[] { ".", "-" };
024 private static final String SEPARATOR_CHARS = Str.toString(DELIMITERS);
025
026 /**
027 * Return true if <code>version</code> ends with <code>-SNAPSHOT</code> or <code>.SNAPSHOT</code> (case insensitive).
028 */
029 public static final boolean isSnapshot(String version) {
030 for (String delimiter : DELIMITERS) {
031 String suffix = delimiter + MAVEN_SNAPSHOT_TOKEN;
032 if (StringUtils.endsWithIgnoreCase(version, suffix)) {
033 return true;
034 }
035 }
036 return false;
037 }
038
039 /**
040 * Return <code>version</code> with <code>.SNAPSHOT</code> or <code>-SNAPSHOT</code> removed from the end (if present)
041 *
042 * <pre>
043 * 1.0.0-SNAPSHOT returns 1.0.0
044 * 1.0.0.SNAPSHOT returns 1.0.0
045 * 1.0.0 returns 1.0.0
046 * 1.0.0SNAPSHOT returns 1.0.0SNAPSHOT
047 * </pre>
048 */
049 public static final String trimSnapshot(String version) {
050 if (isSnapshot(version)) {
051 int length = MAVEN_SNAPSHOT_TOKEN.length() + 1;
052 return StringUtils.left(version, version.length() - length);
053 } else {
054 return version;
055 }
056 }
057
058 /**
059 * Parse a <code>Version</code> object from the <code>version</code> string. The logic here is crudely simple. First
060 * <code>SNAPSHOT</code> is trimmed off the end of the string (if it exists). Whatever remains is then split into tokens using
061 * <code>.</code> and <code>-</code> as delimiters. The first 3 tokens encountered are <code>major</code>, <code>minor</code>, and
062 * <code>incremental</code>, respectively. Anything after that is the <code>qualifier</code>
063 */
064 public static final Version getVersion(String version) {
065 boolean snapshot = isSnapshot(version);
066 String trimmed = trimSnapshot(version);
067 Version v = new Version();
068 v.setTrimmed(trimmed);
069 v.setSnapshot(snapshot);
070 String[] tokens = StringUtils.split(trimmed, SEPARATOR_CHARS);
071 if (tokens.length > 0) {
072 v.setMajor(tokens[0]);
073 }
074 if (tokens.length > 1) {
075 v.setMinor(tokens[1]);
076 }
077 if (tokens.length > 2) {
078 v.setIncremental(tokens[2]);
079 }
080 String qualifier = getQualifier(trimmed, tokens);
081 v.setQualifier(qualifier);
082 return v;
083 }
084
085 protected static final String getQualifier(String trimmed, String[] tokens) {
086 if (tokens.length < 4) {
087 return null;
088 }
089 int pos = tokens[0].length() + 1 + tokens[1].length() + 1 + tokens[2].length() + 1;
090 return trimmed.substring(pos);
091 }
092
093 }