1 package org.apache.maven.archiva.common.utils;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.StringUtils;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
28 * Version utility methods.
30 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
33 public class VersionUtil
36 * These are the version patterns found in the filenames of the various artifact's versions IDs.
37 * These patterns are all tackling lowercase version IDs.
39 private static final String versionPatterns[] = new String[] {
42 "(g?[_.0-9ab]*(pre|rc|g|m)[_.0-9]*)",
47 // "(test[_.0-9]*)", -- omitted for MRM-681, can be reinstated as part of MRM-712
49 "(unofficial[_.0-9]*)",
60 public static final String SNAPSHOT = "SNAPSHOT";
62 public static final Pattern UNIQUE_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-([0-9]{8}\\.[0-9]{6})-([0-9]+)$" );
64 public static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "^([0-9]{8})\\.([0-9]{6})$" );
66 public static final Pattern GENERIC_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-" + SNAPSHOT );
68 private static final Pattern VERSION_MEGA_PATTERN = Pattern.compile( StringUtils.join( versionPatterns, '|' ), Pattern.CASE_INSENSITIVE );
72 * Tests if the unknown string contains elements that identify it as a version string (or not).
76 * The algorithm tests each part of the string that is delimited by a '-' (dash) character.
77 * If 75% or more of the sections are identified as 'version' strings, the result is
78 * determined to be of a high probability to be version identifier string.
81 * @param unknown the unknown string to test.
82 * @return true if the unknown string is likely a version string.
84 public static boolean isVersion( String unknown )
86 String versionParts[] = StringUtils.split( unknown, '-' );
90 int countValidParts = 0;
92 for ( int i = 0; i < versionParts.length; i++ )
94 String part = versionParts[i];
95 mat = VERSION_MEGA_PATTERN.matcher( part );
103 /* Calculate version probability as true if 3/4's of the input string has pieces of
104 * of known version identifier strings.
106 int threshold = (int) Math.floor( Math.max( (double) 1.0, (double) ( versionParts.length * 0.75 ) ) );
108 return ( countValidParts >= threshold );
113 * Tests if the identifier is a known simple version keyword.
117 * This method is different from {@link #isVersion(String)} in that it tests the whole input string in
118 * one go as a simple identifier. (eg "alpha", "1.0", "beta", "debug", "latest", "rc#", etc...)
121 * @param identifier the identifier to test.
122 * @return true if the unknown string is likely a version string.
124 public static boolean isSimpleVersionKeyword( String identifier )
126 Matcher mat = VERSION_MEGA_PATTERN.matcher( identifier );
128 return mat.matches();
131 public static boolean isSnapshot( String version )
133 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
140 return version.endsWith( SNAPSHOT );
144 public static String getBaseVersion( String version )
146 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
149 return m.group( 1 ) + "-" + SNAPSHOT;
159 * Get the release version of the snapshot version.
163 * If snapshot version is 1.0-SNAPSHOT, then release version would be 1.0
164 * And if snapshot version is 1.0-20070113.163208-1.jar, then release version would still be 1.0
167 * @param snapshotVersion
170 public static String getReleaseVersion( String snapshotVersion )
172 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( snapshotVersion );
174 if( isGenericSnapshot( snapshotVersion ) )
176 m = GENERIC_SNAPSHOT_PATTERN.matcher( snapshotVersion );
185 return snapshotVersion;
189 public static boolean isUniqueSnapshot( String version )
191 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
200 public static boolean isGenericSnapshot( String version )
202 return version.endsWith( SNAPSHOT );